如何下载和运行C#的.exe文件

5
在您将此标记为重复之前,请注意,确实有类似的问题,我已经查看了所有相关内容但仍无法使其正常工作。 我正在尝试编写一个功能,下载并运行.exe文件,但它不会下载、运行或做任何事情。 我甚至删除了try-catch语句以查找错误或错误代码,但我没有发现任何错误,所以我不知道哪里出了问题,这是我的代码。
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!");
        }
2个回答

4

谢谢!没想到问题修复起来这么容易。 - Pavilion Sahota

0

这段代码帮助我更新文件,所以我想展示我的方法,希望其他有类似需求的人也能受益。

当点击按钮时,我需要这段代码执行以下操作:

  1. 从服务器获取文件并将其存储在 AppData\Temp 中。
  2. 保持用户安装进度更新(下载安装程序)。
  3. 如果下载成功(注意删除旧文件检查后删除 else),启动 "daInstaller.exe",同时终止当前运行的程序。
  4. 如果该文件已存在(即旧的 "daIstaller.exe"),则在将新文件复制到 AppData\Temp 之前将其删除。

不要忘记保持文件名相同,否则您将在 AppData\Temp 文件夹中留下更多垃圾。

 private void button1_Click(object sender, EventArgs e)
    {
        Uri uri = new Uri("http://example.com/files/example.exe");
        filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/example.exe");

        try
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            WebClient wc = new WebClient();
            wc.DownloadFileAsync(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)
        {
            Process.Start(filename);
            Close();
            Application.Exit();
        }
        else
        {
            MessageBox.Show("Unable to download exe, please check your connection", "Download failed!");
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接