2016-02-28 31 views
1

在您將其標記爲重複項之前,是的,有這樣的問題,我已經查看了所有這些問題,但仍然無法正常工作。我試圖編寫一個下載和運行.exe文件的功能,但它不會下載,運行或執行任何操作。我甚至刪除的嘗試捕獲找到一個錯誤或錯誤代碼,但我不,我不知道我要去哪裏錯了,這是我爲它如何下載並運行.exe文件c#

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

回答

0

這段代碼幫助我更新了一個文件,所以我認爲我會展示我的想法,希望別人在那裏和我有類似的要求。

我需要這個代碼做以下被點擊時,一個按鈕:

  1. 抓鬥從服務器文件和應用程序數據中的\ 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!"); 
     } 
    }