How to download and run a .exe file c#

谁说胖子不能爱 提交于 2020-01-04 05:41:13

问题


Before you flag this as a duplicate, yes there are questions just like this, i've looked at all of them and still couldn't get this working. I'm trying to code in a feature that downloads and runs a .exe file but it doesn't download, run or do anything. I even removed the try catches to find an error or error codes but I have non, so I have no idea where i'm going wrong, here is my code for it

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

回答1:


Change DownloadDataAsync to DownloadFileAsync.

wc.DownloadFileAsync(uri, filename);



回答2:


This code helped me out quite a bit with updating a file, so I thought I would show my twist in the hopes that someone else out there has a similar requirement as me.

I needed this code to do the following when a button was clicked:

  1. Grab a file from a sever and store it locally in AppData\Temp.
  2. Keep my user up-to-date of install progress (an installer is downloaded).
  3. If successfully downloaded (note the removal of the else after deleting old file check), launch "daInstaller.exe", whilst terminating the current running program.
  4. And if said file already exist (i.e. the old "daIstaller.exe"), delete prior to copying new file to AppData\Temp.

Don't forget to keep the file names the same, else you'll be leaving more trash in that AppData\Temp folder.

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


来源:https://stackoverflow.com/questions/35684243/how-to-download-and-run-a-exe-file-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!