如何下载并运行.exe文件c#

2024-01-24

在您将其标记为重复之前,是的,存在这样的问题,我已经查看了所有这些问题,但仍然无法解决这个问题。我正在尝试编写一个下载并运行 .exe 文件的功能,但它不会下载、运行或执行任何操作。我什至删除了尝试捕获以查找错误或错误代码,但我没有,所以我不知道我哪里出错了,这是我的代码

public test_Configuration()
    {
        InitializeComponent();
    }

    Uri uri = new Uri("http://example.com/files/example.exe");
    string filename = @"C:\Users\**\AppData\Local\Temp\example.exe";

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if(File.Exists(filename))
            {
                File.Delete(filename);
            }
            else
            {
                WebClient wc = new WebClient();
                wc.DownloadDataAsync(uri, filename);
                wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
                wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }      
    }
    private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        if (progressBar1.Value == progressBar1.Maximum)
        {
            progressBar1.Value = 0;
        }
    }
    private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        if(e.Error == null)
        {
            MessageBox.Show("Download complete!, running exe", "Completed!");
            Process.Start(filename);
        }
        else
        {
            MessageBox.Show("Unable to download exe, please check your connection", "Download failed!");
        }

Change DownloadDataAsync https://msdn.microsoft.com/en-us/library/ms144191(v=vs.110).aspx to DownloadFileAsync https://msdn.microsoft.com/en-us/library/ms144196(v=vs.110).aspx.

wc.DownloadFileAsync(uri, filename);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何下载并运行.exe文件c# 的相关文章

随机推荐