Winform 下载服务器安装包并安装

 代码中包含了检测本地安装盘符代码

 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

 

      

   

posted @ 2017-09-05 22:14  凌乱忘语  阅读(1121)  评论(0编辑  收藏  举报