在做C#程序的时候 一定会涉及到 程序升级的问题 下边有大概讲一下 自动升级的过程
首先 要把 新程序上传到服务器上
然后再打开运行程序的时候 去到当前版本号 然后在数据库中查 这个版本号对应的id
然后 执行select count 语句 如果返回值大于0 就证明有新版本存在 然后弹出下载页面
下载成功后 更新客户端的版本号 然后从新启动系统
思路大致这样 下边 是代码 供参考
系统登陆页面中代码
//检查版本号
/// <summary>
/// 检查是否需要升级
/// </summary>
/// <returns>是否需要升级</returns>
private bool CheckNeedUpdate()
{
string sysVersion = ConfigHelper.GetConfigString("UpgradeNO");
this.dtUpdateInfo = emrFileClient.GetDataTable_PBS_UPGRADE("id>" + sysVersion + "");//获取升级信息
return dtUpdateInfo.Rows.Count > 0 ? true : false;
}
/// <summary>
/// 开始升级
/// </summary>
private void StartToUpdate()
{
if (this.dtUpdateInfo != null)
{
ClientUpdate cu = new ClientUpdate(this.dtUpdateInfo);
cu.MinimizeBox = false;
cu.ShowDialog();
this.Close();
}
}
/// <summary>
/// 异步升级
/// </summary>
private void fileDown()
{
try
{
//判断是否能连接服务器
ulc.GET_CONN();
string filename = dt.Rows[downfilenum]["文件"].ToString();
string filepath = dt.Rows[downfilenum]["客户端路径"].ToString();
string serverpath = "http://" + ConfigHelper.GetConfigString("ServerIP") + "/upgrade/";
WebClient wc = new WebClient();
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
gridView1.SetRowCellValue(downfilenum, "下载状态", "正在下载...");
wc.DownloadFileAsync(new Uri(serverpath + filename), Application.StartupPath + "\\update\\" + filename);
}
catch (Exception ex)
{
Directory.Delete(Application.StartupPath + @"\update", true);
Directory.CreateDirectory(Application.StartupPath + @"\update");
MessageHelper.Show("连接不上服务器!", MessageType.Information);
updateFlaglbl.Text = "下载失败";
startbtn.Enabled = true;
}
}
//修改配置文件 备份更新文件
private void updatefile()
{
if (File.Exists(Application.StartupPath + @"\update\update.xml"))
{
string version = "";
labelControl1.Text = "正在升级...";
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + @"\update\update.xml");
XmlNodeList xElement = doc.GetElementsByTagName("files");
XmlNodeList xChild = doc.GetElementsByTagName("file");
version = xElement.Item(0).Attributes["version"].Value.ToString();
string no = xElement.Item(0).Attributes["upgradeno"].Value.ToString();
for (int i = 0; i < int.Parse(xElement.Item(0).Attributes["count"].Value.ToString()); i++)
{
string filename = xChild.Item(i).Attributes["filename"].Value.ToString();
string filepath = xChild.Item(i).Attributes["filepath"].Value.ToString();
if (File.Exists(Application.StartupPath + filepath + filename))
{
if (!Directory.Exists(Application.StartupPath + @"\backup"))
{
Directory.CreateDirectory(Application.StartupPath + @"\backup");
}
File.Copy(Application.StartupPath + filepath + filename, Application.StartupPath + @"\backup\" + filename, true);//复制文件
File.Copy(Application.StartupPath + @"\update\" + filename, Application.StartupPath + filepath + filename, true);//复制文件
File.Delete(Application.StartupPath + @"\update\" + filename);//删除文件
}
else
{
File.Move(Application.StartupPath + @"\update\" + filename, Application.StartupPath + filepath + filename);//移动文件
}
progressBarControl1.EditValue = int.Parse(progressBarControl1.EditValue.ToString()) + 1;
}
File.Delete(Application.StartupPath + @"\update\update.xml");
labelControl1.Text = "升级成功!";
XmlDocument ac = new XmlDocument();
ac.Load("ChangeEMR.exe.config");
XmlNode node = ac.SelectSingleNode("//appSettings");
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='SysVersion']");
element.SetAttribute("value", version);//修改客户端版本号
XmlElement e = (XmlElement)node.SelectSingleNode("//add[@key='UpgradeNO']");
e.SetAttribute("value", no);
ac.Save("ChangeEMR.exe.config");
}
else
{
this.Hide();
MessageBox.Show("此处不是程序入口点!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.Close();
}
}
}