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

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

 一,定义下载委托事件(用于实现前台进度条更新和下载完成后事件的回调):
private delegate void Action();
private string diverUrl = ConfigurationManager.AppSettings["diverUrl"];//http:形式
//页面初次加载事件
private void frmProgressBar_Load(object sender, EventArgs e)
{
//获取存储路径
GetRemovableDeviceId();
if (File.Exists(_drivesName))
{
//安装包存在,则直接进行安装
var process = Process.Start(_drivesName);
if (process != null) process.WaitForExit();
//Process
this.Close();
}
else
{
//安装包不存在--则下载
DownloadFile(diverUrl, _drivesName,DownloadProgressChanged, downloadFileCompleted);
}
} /// 下载
private void downloadFileCompleted()
{
Process.Start(_drivesName);
this.Close();
} /// 检测本机盘符
public void GetRemovableDeviceId()
{
List<string> drivesList = new List<string>();
DriveInfo[] dr = DriveInfo.GetDrives();
foreach (DriveInfo dd in dr)
{
if (dd.DriveType == DriveType.CDRom || dd.DriveType == DriveType.Removable) //过滤掉是光驱的 磁盘或移动U盘
{
break;
}
else
{
drivesList.Add(dd.Name);
}
}
//已驱除驱动盘
for (int i = ; i < drivesList.Count; i++)
{
//其它盘符
if (drivesList[i].IndexOf("C") == -)
{
_drivesName = drivesList[drivesList.Count - ].Replace(":\\", "")+ ":Cam_Ocx.exe";
return;
}
else
{
_drivesName = drivesList[i].Replace(":\\","") + ":Cam_Ocx.exe";
}
}
}
//下载文件
private void DownloadFile(string url, string savefile, Action<int> downloadProgressChanged, Action downloadFileCompleted)
{
WebClient client = new WebClient();
if (downloadProgressChanged != null)
{
client.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e)
{
this.Invoke(downloadProgressChanged, e.ProgressPercentage);
};
}
if (downloadFileCompleted != null)
{
client.DownloadFileCompleted += delegate (object sender, AsyncCompletedEventArgs e)
{
this.Invoke(downloadFileCompleted);
};
}
client.DownloadFileAsync(new Uri(url), savefile);
}
//显示进度条
private void DownloadProgressChanged(int val)
{
progressBar1.Value = val;
lbValue.Text = val + "%";
}
上一篇:Android Calendar获取年月日时分秒毫秒


下一篇:Java Web项目部署