如何从网站下载 .EXE 文件? [英] How to download an .EXE file from a website?

查看:121
本文介绍了如何从网站下载 .EXE 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,我需要从网站下载一个 exe 文件.我使用的是 Visual Studio Express 2008.

I am writing an application where i required to download an exe file from a website. I am using Visual studio express 2008.

我正在使用以下代码.

private void button1_Click(object sender, EventArgs e)
{
    WebClient webClient = new WebClient();
    webClient.DownloadFileAsync(new   Uri("http://example.com/images/def.exe"), @"d:\ac\def.exe");           
}

def.exe 下载到 D 盘的 Ac 文件夹中,但为 0 字节.

The def.exe get downloaded in Ac folder of D drive but is of 0 byte.

我无法理解为什么会这样.

I am not able to understand that why it is happening.

请帮帮我.

推荐答案

您可以稍微更改代码以使其正常工作.DownloadFileAsync 是异步调用,因此执行它的线程可能会在下载完成之前结束.

You can alter your code a bit to get it working. DownloadFileAsync is asynchronous call so may be your thread executing it ending before your download has completed.

如果您使用异步,您需要附加一个处理程序来确定下载是否已完成

You need to attach a handler to find out if the download has completed if you are using async

    [TestMethod]
    public void TestDownload()
    {
        var webClient = new WebClient();
        webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;
        webClient.DownloadFileAsync(new Uri("https://www.telerik.com/downloads/productfiles/btmba/TelerikJustDecompileSetup_2014.3.1021.0.exe"), @"c:\temp\justdecompile.exe");

        // just to show in a Unit Test.. Not required in actual code
        Thread.Sleep(10000); 

        var info = new FileInfo(@"c:\temp\justdecompile.exe");
        Assert.IsTrue(info.Length > 0);
    }

你的处理程序看起来像,

Your handler can look like,

    void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        Debug.WriteLine(String.Format("{0}    downloaded {1} of {2} bytes. {3} % complete...",
                    (string)e.UserState,
                    e.BytesReceived,
                    e.TotalBytesToReceive,
                    e.ProgressPercentage));
    }

这篇关于如何从网站下载 .EXE 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆