2012-12-17 12 views
5

Tôi cố gắng để tải về tập tin như thế này:web client DownloadFileCompleted lấy tên tập tin

WebClient _downloadClient = new WebClient(); 

_downloadClient.DownloadFileCompleted += DownloadFileCompleted; 
_downloadClient.DownloadFileAsync(current.url, _filename); 

// ... 

Và sau khi tải tôi cần phải bắt đầu một quá trình với tập tin tải về, tôi cố gắng sử dụng DownloadFileCompleted sự kiện.

void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) 
{ 
    if (e.Error != null) 
    { 
     throw e.Error; 
    } 
    if (!_downloadFileVersion.Any()) 
    { 
     complited = true; 
    } 
    DownloadFile(); 
} 

Nhưng, tôi không thể biết tên của tập tin đã tải về từ AsyncCompletedEventArgs, tôi làm của riêng tôi

public class DownloadCompliteEventArgs: EventArgs 
{ 
    private string _fileName; 
    public string fileName 
    { 
     get 
     { 
      return _fileName; 
     } 
     set 
     { 
      _fileName = value; 
     } 
    } 

    public DownloadCompliteEventArgs(string name) 
    { 
     fileName = name; 
    } 
} 

Nhưng tôi không thể hiểu làm thế nào gọi sự kiện của tôi thay vì DownloadFileCompleted

Xin lỗi nếu đó là Nood câu hỏi

+0

http://msdn.microsoft.com/en-us/library/17sde2xt(v=VS.100).aspx – Leri

+0

có lẽ toàn cầu biến – VladL

+0

tôi biết cách sử dụng sự kiện =) Tôi không biết làm thế nào sử dụng sự kiện của tôi thay vì DownloadFileCompleted với eventArgs của tôi – user1644087

Trả lời

12

Một cách là tạo ra một đóng cửa.

 WebClient _downloadClient = new WebClient();   
     _downloadClient.DownloadFileCompleted += DownloadFileCompleted(_filename); 
     _downloadClient.DownloadFileAsync(current.url, _filename); 

Điều đó có nghĩa là DownloadFileCompleted của bạn cần trả lại trình xử lý sự kiện.

 public AsyncCompletedEventHandler DownloadFileCompleted(string filename) 
     { 
      Action<object,AsyncCompletedEventArgs> action = (sender,e) => 
      { 
       var _filename = filename; 

       if (e.Error != null) 
       { 
        throw e.Error; 
       } 
       if (!_downloadFileVersion.Any()) 
       { 
        complited = true; 
       } 
       DownloadFile(); 
      }; 
      return new AsyncCompletedEventHandler(action); 
     } 

Lý do tôi tạo biến được gọi là _filename để biến tên tệp được chuyển vào phương thức DownloadFileComplete được ghi lại và lưu trữ trong phần đóng. Nếu bạn không làm điều này, bạn sẽ không có quyền truy cập vào biến tên tệp trong phạm vi đóng.

+0

Cảm ơn! Đó là công việc! – user1644087

+0

Đây là lần đầu tiên tôi nhìn thấy một đóng cửa trong C#. Tôi thậm chí không nhận thức được điều đó là có thể. Cảm ơn nhiều! –

2

Tôi đã chơi khoảng DownloadFileCompleted để lấy tên tệp/đường dẫn tệp Từ sự kiện. Tôi đã thử các giải pháp trên cũng nhưng nó không giống như kỳ vọng của tôi sau đó tôi thích giải pháp bằng cách thêm giá trị Querystring, Ở đây tôi muốn chia sẻ mã với bạn.

string fileIdentifier="value to remember"; 
WebClient webClient = new WebClient(); 
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCompleted); 
webClient.QueryString.Add("file", fileIdentifier); // here you can add values 
webClient.DownloadFileAsync(new Uri((string)dyndwnldfile.path), localFilePath); 

Và sự kiện này có thể được định nghĩa là như thế này:

private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
{ 
    string fileIdentifier= ((System.Net.WebClient)(sender)).QueryString["file"]; 
    // process with fileIdentifier 
}