不是那种基础问题。我正在开发一个应用程序,遇到这样的情况:文件将被下载,然后上传到FTP服务器,然后删除本地副本,最后在字典中为该文件名添加一个条目。因此,以下是代码:
public void download_This_WebPage(string url, string cookies_String, string local_Saving_File_Name_With_Path)
{
WebClient wb = new WebClient();
wb.Headers.Add(HttpRequestHeader.Cookie, cookies_String);
// Below I want to pass this local_File _Path to the event handler
wb.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wb,);
wb.DownloadFileAsync(new Uri(url), local_Saving_File_Name_With_Path + ".html");
}
public void data_Download_Completed(Object sender, System.ComponentModel.AsyncCompletedEventArgs args)
{
//use the file name to upload the file to FTP
}
public FTP_Completed
{
// Delete the file
}
但是,我不知道如何将文件名传递给download_Completed事件处理程序。有人能指导我吗?
编辑: 感谢“Darin”和“Frederic”的答案。是否有一种通用的方式可以像下面这样将自定义数据传递给(已定义的)事件处理程序
void main_Fn()
{
string my_Data = "Data";
some_object a = new some_object();
some_Object.click_event += new eventHandler(click_Happened);
(Assume that the event passes two ints, I also want to pass the string "my_Data"
to "click_Happened")
some_object.start();
}
void click_Happened(int a, int b)
{
// I want to get the string "my_Data" here.
}
简而言之,如何欺骗签名?
userToken
中传递任何类型,包括任何class
或struct
的实例,并且它将在UserState
中可用。 - Frédéric Hamidiint
参数,则必须提供两个int
参数,不能多也不能少。您可以尝试以另一种方式将my_Data
与some_Object
关联起来,例如使用与类本身关联的静态映射。 - Frédéric Hamidi