给家医院做了个小软件,给各个科室医生使用的CS版的小程序,开发时就考虑到在线升级的问题,软件功能会不断增加和完善,一升级就到每台电脑上去更新很不现实,就写了个升级程序放在目录里,将版本控制和升级文件放在服务器上,需要升级时,在服务器修改版本号和放上升级文件,医生电脑上程序已启动就会自动升级,升级完之后,更新本机的版本号。
string s_newverson = "";
string s_mode = "手动升级";
try
{
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(updateUrl);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stream);
XmlNode list = xmlDoc.SelectSingleNode("Update");
foreach (XmlNode node in list)
{
if (node.Name == "Soft")
{
foreach (XmlNode xml in node)
{
if (xml.Name == "Verson")
{
s_newverson = xml.InnerText;
}
if (xml.Name == "Mode")
{
s_mode = xml.InnerText;
}
}
}
}
int tm = s_newverson.CompareTo(ls_oldverson); //版本号比较
if (tm > 0)
{
if (MessageBox.Show("检查到新版本,是否更新?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
file.IniWriteValue("config", "版本号", s_newverson);
file.IniWriteValue("config", "升级模式", s_mode);
string fileName = Application.StartupPath + @"\update.exe"; //启动升级程序
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = fileName;
p.StartInfo.CreateNoWindow = true;
//p.StartInfo.Arguments = "欢迎你!";//参数以空格分隔,如果某个参数为空,可以传入””
p.Start();
System.Environment.Exit(System.Environment.ExitCode);
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, "升级错误:" + ex.ToString());
}
}
//升级程序代码
private void UpdateDownLoad()
{
WebClient wc = new WebClient();
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
wc.DownloadFileAsync(new Uri(ls_urlfile), ls_updatepath);//要下载文件的路径,下载之后的命名
}
void wc_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
Action act = () =>
{
this.progressBar1.Value = e.ProgressPercentage;
this.label1.Text = e.ProgressPercentage + "%";
};
this.Invoke(act);
if (e.ProgressPercentage == 100)
{
//下载完成之后开始覆盖
ZipHelper.Unzip();//调用解压的类
this.Dispose();
}
}
下载地址:
https://download.csdn.net/download/bluepb/11344419