C#实现在线更新软件
通过某些手段后台更新软件。首先你要有一个放置新版本信息的网站
UpdateSoftwareForm.cs
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingCCWin; usingSystem.Net; usingSystem.Collections; usingSystem.IO; usingSystem.Xml; usingSystem.Diagnostics; usingSystem.Threading; namespaceWriteBook { publicpartialclassUpdateSoftwareForm:Skin_Metro { publicUpdateSoftwareForm() { InitializeComponent(); } #region一些对象和变量 //使用WebClient下载 WebClientclient=newWebClient(); ArrayListdownlist=newArrayList(); //当前版本 stringnowversion=null; //最新版本 stringlatesversion=null; #endregion #region获取版本号 ///<summary> ///从服务器上获取最新的版本号 ///</summary> publicvoidDownloadCheckUpdateXml() { try { //第一个参数是文件的地址,第二个参数是文件保存的路径文件名 client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WriteBook2.xml","WriteBook2.xml"); } catch { MessageBox.Show("没有检测到更新。","提示"); this.Close(); } } ///<summary> ///获取本地软件的版本号 ///</summary> privatevoidNowVersion() { nowversion=System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()+"\n"; LocalText.Text=nowversion; } ///<summary> ///读取从服务器获取的最新版本号 ///</summary> publicvoidLatestVersion() { try { if(File.Exists("WriteBook2.xml.xml")) { XmlDocumentdoc=newXmlDocument(); //加载要读取的XML doc.Load("WriteBook2.xml.xml"); //获得根节点 XmlElementWriteBook=doc.DocumentElement; //获得子节点返回节点的集合 XmlNodeListUpdate=WriteBook.ChildNodes; foreach(XmlNodeiteminUpdate) { latesversion=item.InnerText; } LatestText.Text=latesversion; } else { MessageBox.Show("没有检测到更新。","提示"); this.Close(); } } catch { this.Close(); } } #endregion #region初始化程序 ///<summary> ///初始化程序 ///</summary> privatevoidInitializeandInstall() { UpdateProgressBar.Value=20; DownloadCheckUpdateXml(); UpdateProgressBar.Value=40; NowVersion(); UpdateProgressBar.Value=60; LatestVersion(); UpdateProgressBar.Value=80; DownloadInstall(); UpdateProgressBar.Value=100; } #endregion #region安装and删除 ///<summary> ///下载安装包 ///</summary> publicvoidDownloadInstall() { try { if(nowversion==latesversion) { MessageBox.Show("您已经是最新版本。","提示"); } elseif(nowversion!=latesversion&&File.Exists("WriteBook2.xml")) { MessageBox.Show("发现新版本,即将下载更新补丁。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe","WBsetup.exe"); if(File.Exists("Setup.exe")) { InstallandDelete(); } else { for(inti=1;i<3;i++) { client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe","WBsetup.exe"); } MessageBox.Show("下载失败,请检查您的网络连接是否正常。","提示"); this.Close(); } } } catch { MessageBox.Show("更新失败,没有发现新版本。","提示"); this.Close(); } } ///<summary> ///安装及删除 ///</summary> privatevoidInstallandDelete() { try { DialogResultdr=MessageBox.Show("下载更新成功,是否安装新更新?","提示",MessageBoxButtons.YesNoCancel); if(dr==System.Windows.Forms.DialogResult.Yes) { //启动安装程序 System.Diagnostics.Process.Start("WBsetup.exe"); Threadtd=newThread(JudgeInstall); td.Start(); } else{} } catch { MessageBox.Show("发生未知错误,更新失败。","提示"); this.Close(); } } ///<summary> ///判断安装进程是否存在 ///</summary> publicvoidJudgeInstall() { while(true) { Process[]processList=Process.GetProcesses(); foreach(ProcessprocessinprocessList) { if(process.ProcessName=="WBsetup.exe"){} else { DialogResultdr=MessageBox.Show("更新成功,是否删除安装包?","提示",MessageBoxButtons.YesNo); if(dr==System.Windows.Forms.DialogResult.Yes) { File.Delete("WBsetup.exe"); File.Delete("WriteBook2.xml"); } } } } } #endregion ///<summary> ///点击初始化程序 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidUpdateButton_Click(objectsender,EventArgse) { InitializeandInstall(); } } }
以上所述就是本文的全部内容了,希望大家能够喜欢。