简体   繁体   中英

Download any file using windows form application with c#

I'll try to download file using this code.But file size is 0 KB.What is the correct and efficiency way to download the file.

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();
    }
}

Any one can give me the method for download the file over windows form program in C#.thanks

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM