代碼中包含了檢測本地安裝盤符代碼 1 一,定義下載委托事件(用於實現前臺進度條更新和下載完成後事件的回調): 2 private delegate void Action(); 3 private string diverUrl = ConfigurationManager.AppSettings[ ...
代碼中包含了檢測本地安裝盤符代碼
1 一,定義下載委托事件(用於實現前臺進度條更新和下載完成後事件的回調): 2 private delegate void Action(); 3 private string diverUrl = ConfigurationManager.AppSettings["diverUrl"];//http:形式 4 //頁面初次載入事件 5 private void frmProgressBar_Load(object sender, EventArgs e) 6 { 7 //獲取存儲路徑 8 GetRemovableDeviceId(); 9 if (File.Exists(_drivesName)) 10 { 11 //安裝包存在,則直接進行安裝 12 var process = Process.Start(_drivesName); 13 if (process != null) process.WaitForExit(); 14 //Process 15 this.Close(); 16 } 17 else 18 { 19 //安裝包不存在--則下載 20 DownloadFile(diverUrl, _drivesName,DownloadProgressChanged, downloadFileCompleted); 21 } 22 } 23 24 /// 下載 25 private void downloadFileCompleted() 26 { 27 Process.Start(_drivesName); 28 this.Close(); 29 } 30 31 /// 檢測本機盤符 32 public void GetRemovableDeviceId() 33 { 34 List<string> drivesList = new List<string>(); 35 DriveInfo[] dr = DriveInfo.GetDrives(); 36 foreach (DriveInfo dd in dr) 37 { 38 if (dd.DriveType == DriveType.CDRom || dd.DriveType == DriveType.Removable) //過濾掉是光碟機的 磁碟或移動U盤 39 { 40 break; 41 } 42 else 43 { 44 drivesList.Add(dd.Name); 45 } 46 } 47 //已驅除驅動盤 48 for (int i = 0; i < drivesList.Count; i++) 49 { 50 //其它盤符 51 if (drivesList[i].IndexOf("C") == -1) 52 { 53 _drivesName = drivesList[drivesList.Count - 1].Replace(":\\", "")+ ":Cam_Ocx.exe"; 54 return; 55 } 56 else 57 { 58 _drivesName = drivesList[i].Replace(":\\","") + ":Cam_Ocx.exe"; 59 } 60 } 61 } 62 //下載文件 63 private void DownloadFile(string url, string savefile, Action<int> downloadProgressChanged, Action downloadFileCompleted) 64 { 65 WebClient client = new WebClient(); 66 if (downloadProgressChanged != null) 67 { 68 client.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e) 69 { 70 this.Invoke(downloadProgressChanged, e.ProgressPercentage); 71 }; 72 } 73 if (downloadFileCompleted != null) 74 { 75 client.DownloadFileCompleted += delegate (object sender, AsyncCompletedEventArgs e) 76 { 77 this.Invoke(downloadFileCompleted); 78 }; 79 } 80 client.DownloadFileAsync(new Uri(url), savefile); 81 } 82 //顯示進度條 83 private void DownloadProgressChanged(int val) 84 { 85 progressBar1.Value = val; 86 lbValue.Text = val + "%"; 87 }View Code