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

 

      

   

转载于:https://www.cnblogs.com/Tcwangyu/p/7482268.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值