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

时间:2016-02-28 15:16:34

标签: c# download exe execution

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

2 个答案:

答案 0 :(得分:2)

DownloadDataAsync更改为DownloadFileAsync

wc.DownloadFileAsync(uri, filename);

答案 1 :(得分:0)

此代码帮助我更新了一个文件,所以我想我会表现出我的想法,希望其他人有与我类似的要求。

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

  1. 从服务器获取文件并将其本地存储在AppData \ Temp。
  2. 让我的用户保持最新的安装进度(下载安装程序)。
  3. 如果成功下载(请注意删除旧文件后删除其他文件),启动“daInstaller.exe”,同时终止当前正在运行的程序。
  4. 如果所述文件已存在(即旧的“daIstaller.exe”),请在将新文件复制到AppData \ Temp之前删除。
  5. 不要忘记保持文件名相同,否则你将在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!");
            }
        }