c# 软件自动在线更新代码(二)
来源:互联网 发布:淘宝服装照片拍摄技巧 编辑:程序博客网 时间:2024/05/06 22:24
/// <summary> /// 获取文件列表并下载 /// </summary> private static void UpdateList() { string xmlPath = dirPath + "AutoUpdater/AutoUpdater.xml"; WebClient wc = new WebClient(); DataSet ds = new DataSet(); ds.Locale = CultureInfo.InvariantCulture; try { Stream sm = wc.OpenRead(xmlPath); ds.ReadXml(sm); DataTable dt = ds.Tables["UpdateFileList"]; StringBuilder sb = new StringBuilder(); count = dt.Rows.Count; for (int i = 0; i < dt.Rows.Count; i++) { if (i == 0) { sb.Append(dt.Rows[i]["UpdateFile"].ToString()); } else { sb.Append("," + dt.Rows[i]["UpdateFile"].ToString()); } } fileNames = sb.ToString().Split(','); sm.Close(); } catch (WebException ex) { MeBox(ex.Message); } } /// <summary> /// 下载文件 /// </summary> /// <param name="arry">下载序号</param> private void DownloadFile(int arry) { try { num++; fileName = fileNames[arry]; this.label1.Text = String.Format( CultureInfo.InvariantCulture, "更新进度 {0}/{1} [ {2} ]", num, count, ConvertSize(size)); this.progressBar2.Value = 0; this.downWebClient.DownloadFileAsync( new Uri(dirPath + "AutoUpdater/" + fileName), Application.StartupPath + "\\AutoUpdater\\" + fileName); } catch (WebException ex) { MeBox(ex.Message); } } /// <summary> /// 转换字节大小 /// </summary> /// <param name="byteSize">输入字节数</param> /// <returns>返回值</returns> private static string ConvertSize(long byteSize) { string str = ""; float tempf = (float)byteSize; if (tempf / 1024 > 1) { if ((tempf / 1024) / 1024 > 1) { str = ((tempf / 1024) / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "MB"; } else { str = (tempf / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "KB"; } } else { str = tempf.ToString(CultureInfo.InvariantCulture) + "B"; } return str; } /// <summary> /// 弹出提示框 /// </summary> /// <param name="txt">输入提示信息</param> private static void MeBox(string txt) { MessageBox.Show( txt, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } /// <summary> /// 关闭程序 /// </summary> private static void UpdaterClose() { try { System.Diagnostics.Process.Start(Application.StartupPath + "\\ComCir.exe"); } catch (Win32Exception ex) { MeBox(ex.Message); } Application.Exit(); } /// <summary> /// 读取.exe.config的值 /// </summary> /// <param name="path">.exe.config文件的路径</param> /// <param name="appKey">"key"的值</param> /// <returns>返回"value"的值</returns> internal static string GetConfigValue(string path, string appKey) { XmlDocument xDoc = new XmlDocument(); XmlNode xNode; XmlElement xElem = null; try { xDoc.Load(path); xNode = xDoc.SelectSingleNode("//appSettings"); xElem = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]"); } catch (XmlException ex) { MeBox(ex.Message); } if (xElem != null) return xElem.GetAttribute("value"); else return ""; } /// <summary> /// 设置.exe.config的值 /// </summary> /// <param name="path">.exe.config文件的路径</param> /// <param name="appKey">"key"的值</param> /// <param name="appValue">"value"的值</param> internal static void SetConfigValue(string path, string appKey, string appValue) { XmlDocument xDoc = new XmlDocument(); try { xDoc.Load(path); XmlNode xNode; XmlElement xElem1; XmlElement xElem2; xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]"); if (xElem1 != null) xElem1.SetAttribute("value", appValue); else { xElem2 = xDoc.CreateElement("add"); xElem2.SetAttribute("key", appKey); xElem2.SetAttribute("value", appValue); xNode.AppendChild(xElem2); } xDoc.Save(Application.StartupPath + "\\" + path); } catch (XmlException ex) { MeBox(ex.Message); } } /// <summary> /// 判断软件的更新日期 /// </summary> /// <param name="Dir">服务器地址</param> /// <returns>返回日期</returns> private static string GetTheLastUpdateTime(string Dir) { string LastUpdateTime = ""; string AutoUpdaterFileName = Dir + "AutoUpdater/AutoUpdater.xml"; try { WebClient wc = new WebClient(); Stream sm = wc.OpenRead(AutoUpdaterFileName); XmlTextReader xml = new XmlTextReader(sm); while (xml.Read()) { if (xml.Name == "UpdateTime") { LastUpdateTime = xml.GetAttribute("Date"); break; } } xml.Close(); sm.Close(); } catch (WebException ex) { MeBox(ex.Message); } return LastUpdateTime; } } }
//更新程序 if(this._Update) { //关闭原有的应用程序 this.labDownFile.Text = "正在关闭程序...."; System.Diagnostics.Process[]proc=System.Diagnostics.Process.GetProcessesByName("TIMS"); //关闭原有应用程序的所有进程 foreach(System.Diagnostics.Process pro in proc) { pro.Kill(); } DirectoryInfo theFolder=new DirectoryInfo(Path.GetTempPath()+"JurassicUpdate");if(theFolder.Exists){ foreach(FileInfo theFile in theFolder.GetFiles()) { //如果临时文件夹下存在与应用程序所在目录下的文件同名的文件,则删除应用程序目录下的文件 if(File.Exists(Application.StartupPath + \\"+Path.GetFileName(theFile.FullName))) File.Delete(Application.StartupPath + "\\"+Path.GetFileName(theFile.FullName)); //将临时文件夹的文件移到应用程序所在的目录下 File.Move(theFile.FullName,Application.StartupPath + \\"+Path.GetFileName(theFile.FullName)); } } //启动安装程序 this.labDownFile.Text = "正在启动程序...."; System.Diagnostics.Process.Start(Application.StartupPath + "\\" + "TIMS.exe"); this.Close(); } }
这段程序是实现在线升级的关键代码,步骤有点复杂:首先用Invoke方法同步调用状态改变进程,然后调用动态链接库中批量下载 (BatchDownloader.cs)类启动批量下载,接着判断原有的主应用程序有没有关闭,如果没关闭,则用Process.Kill()来关闭主 程序。接下来,判断临时文件夹下(即下载升级程序所在的目录)是否存在与应用程序所在目录下的文件同名的文件,如果存在同名文件,则删除应用程序目录下的 文件,然后将临时文件夹的文件移到应用程序所在的目录下。最后重新启动主应用程序。这样更新就完成了。
- c# 软件自动在线更新代码(二)
- c# 软件自动在线更新代码(二)
- c# 软件自动在线更新代码
- C# 实现在线软件自动升级自动下载更新文件
- C#软体自动在线更新代码(一)
- C# 实现在线软件自动升级程序
- C# 如何实现软件自动在线升级
- C# 实现在线软件自动升级程序 .
- C# 如何实现软件自动在线升级
- C#编写软件自动升级更新程序
- [.NET源码] C#制作的一套在线更新软件系统
- C#实现在线软件自动升级程序-转
- 用C# 实现CS模式下软件自动在线升级
- C# C/S模式下实现软件自动在线升级
- C# C/S模式下实现软件自动在线升级
- 用C# 实现CS模式下软件自动在线升级
- C#中实现软件自动在线升级的方法思考
- C#在线更新
- Bit Mask技术
- c# 软件自动在线更新代码(二)
- spring手工装配 条件
- .net播放flash
- vs2010的sdf文件和ipch文件夹
- c# 软件自动在线更新代码(二)
- BOJ1293 小马过河 dp
- Android4: Write Storage权限问题
- Delphi加载网络图片
- TCP/IP三次握手,四次分手
- 字符串结束字符的整理
- java5 多线程编程
- SQL TRACE和TKPROF的使用步骤
- AfxBeginThread和AfxGetApp基本用法