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

本文关键字:文件 exe 运行 下载和 | 更新日期: 2023-09-27 18:36:32

在你将其标记为重复之前,是的,有这样的问题,我已经看了所有这些问题,但仍然无法让它工作。我正在尝试编写一个下载并运行.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!");
        }

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

DownloadDataAsync更改为DownloadFileAsync

wc.DownloadFileAsync(uri, filename);

这段代码在更新文件方面帮助了我很多,所以我想我会展示我的扭曲,希望其他人有与我类似的要求。

单击按钮时,我需要此代码执行以下操作:

  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!");
        }
    }