簡體   English   中英

使用帶有 c# 的 windows 窗體應用程序下載任何文件

[英]Download any file using windows form application with c#

我將嘗試使用此代碼下載文件。但文件大小為 0 KB。下載文件的正確和有效方法是什么。

private void DownloadFile()
{
    using (WebClient Client = new WebClient())
    {
        Client.DownloadFileAsync(
            new Uri("http://localhost/sn/userSelect.Designer.cs", UriKind.Absolute),
            @"C:\xampp\htdocs\sn\test1.txt");
        Client.Dispose();
    }
}

誰能給我一個C#windows窗體程序下載文件的方法謝謝

class Program
{
    private static WebClient wc = new WebClient();
    private static ManualResetEvent handle = new ManualResetEvent(true);
    private static void Main(string[] args)
    {

        wc.DownloadProgressChanged += WcOnDownloadProgressChanged;
        wc.DownloadFileCompleted += WcOnDownloadFileCompleted;
        wc.DownloadFileAsync(new Uri(@"http://www.nattyware.com/bin/pixie.exe"), @"C:\\pixie.exe");
        handle.WaitOne(); // wait for the async event to complete

    }

    private static void WcOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        if (!e.Cancelled && e.Error == null)
        {
            //async download completed successfully
        }
        handle.Set(); // in both the case let the void main() know that async event had finished so that i can quit
    }

    private static void WcOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        // handle the progres in case of async
        //e.ProgressPercentage
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM