WebClient.DownloadFileCompleted 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在非同步檔案下載作業完成時發生。
public:
event System::ComponentModel::AsyncCompletedEventHandler ^ DownloadFileCompleted;
public event System.ComponentModel.AsyncCompletedEventHandler? DownloadFileCompleted;
public event System.ComponentModel.AsyncCompletedEventHandler DownloadFileCompleted;
member this.DownloadFileCompleted : System.ComponentModel.AsyncCompletedEventHandler
Public Custom Event DownloadFileCompleted As AsyncCompletedEventHandler
Public Event DownloadFileCompleted As AsyncCompletedEventHandler
事件類型
範例
下列程式代碼範例示範如何設定 DownloadFileCompleted
事件的事件處理程式。
// Sample call : DownLoadFileInBackground2 ("http://www.contoso.com/logs/January.txt");
void DownLoadFileInBackground2( String^ address )
{
WebClient^ client = gcnew WebClient;
Uri ^uri = gcnew Uri(address);
// Call DownloadFileCallback2 when the download completes.
client->DownloadFileCompleted += gcnew AsyncCompletedEventHandler( DownloadFileCallback2 );
// Specify a progress notification handler here...
client->DownloadFileAsync( uri, "serverdata.txt" );
}
void DownloadFileCallback2( Object^ /*sender*/, System::ComponentModel::AsyncCompletedEventArgs^ e )
{
if ( e->Cancelled )
{
Console::WriteLine( "File download cancelled." );
}
if ( e->Error != nullptr )
{
Console::WriteLine( e->Error->ToString() );
}
}
// Sample call : DownLoadFileInBackground2 ("http://www.contoso.com/logs/January.txt");
public static void DownLoadFileInBackground2(string address)
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
// Call DownloadFileCallback2 when the download completes.
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback2);
// Specify a progress notification handler here ...
client.DownloadFileAsync(uri, "serverdata.txt");
}
private static void DownloadFileCallback2(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
Console.WriteLine("File download cancelled.");
}
if (e.Error != null)
{
Console.WriteLine(e.Error.ToString());
}
}
' Sample call : DownLoadFileInBackground2 ("http:' www.contoso.com/logs/January.txt")
Public Shared Sub DownLoadFileInBackground2(address As String)
Dim client As WebClient = New WebClient()
' Call DownloadFileCallback2 when the download completes.
AddHandler client.DownloadFileCompleted, AddressOf DownloadFileCallback2
' Specify a progress notification handler here...
Dim uri as Uri = New Uri(address)
client.DownloadFileAsync(uri, "serverdata.txt")
End Sub
Private Shared Sub DownloadFileCallback2(sender As Object, e As AsyncCompletedEventArgs)
If e.Cancelled = True Then
Console.WriteLine("File download cancelled.")
End If
If Not e.Error Is Nothing Then
Console.WriteLine(e.Error.ToString())
End If
End Sub
備註
每次異步檔案下載作業完成時,就會引發此事件。 異步檔案下載會藉由呼叫 DownloadFileAsync 方法來啟動。
AsyncCompletedEventHandler是這個事件的委派。 類別 AsyncCompletedEventArgs 會提供事件處理程式與事件數據。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應