給家醫院做了個小軟件,給各個科室醫生使用的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