2013-04-18 54 views
0

好吧,所以我是編程新手,我想製作帶有進度條的工具欄,以顯示從互聯網獲取文件的進度並打開了,這裏是代碼snipplet http://pastebin.com/3EKrFb4K它並沒有給出錯誤的代碼,但是當我調試它說這個時,我試圖下載並打開文件(一旦它甚至不做)http://gyazo.com/44634914669d81b1c20d3d26f2dd3ad8我想知道如果它只是簡單的事情,或者如果我寫的代碼不會工作?它是一個工具條進度條,所以我必須做一些特別的或什麼?請提前幫助和感謝。C#錯誤顯示進度條顯示從互聯網上下載並運行文件的進度

回答

0

的問題是,您正在打開文件之前它已被下載:

Web客戶端的Web =新的Web客戶端(); web.DownloadFileAsync(new Uri(direct_exe_from_url),filepath); //這個方法是異步的 web.DownloadProgressChanged + = new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
Process.Start(filepath); //您在下載文件時打開該文件。

您需要執行downloadFileCompleted處理程序內的進程啓動。

這應該工作:

private void startBotToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      string directoryPath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\"; 
      string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe"; 
      if (!Directory.Exists(directoryPath)) 
      { 
       Directory.CreateDirectory(directoryPath); 
      } 

      string direct_exe_from_url = "http://rs542p2.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=1764003915&filename=Minecraft.exe&cookie=F2CB284BDC9920808D8494CA4EB46F0935AB22D79EC69D6D130C21FB6AD2A0A1EB413347302A46C5FB1A39599DF740D6&directstart=1"; 

      WebClient web = new WebClient(); 
      web.DownloadFileAsync(new Uri(direct_exe_from_url), filepath); 
      web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);  
      web.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback); 
     } 

     void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
     { 
      ProgressBar1.Value = e.ProgressPercentage; 
     } 

     void DownloadFileCallback(object sender, AsyncCompletedEventArgs e) 
     { 
      string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe"; 
      Process.Start(filepath); 

     } 
+0

謝謝!如果我希望它在進度條的文本infront中更新時更新,該怎麼辦? – D4rk