| 
自動更新的軟件的目的在于讓客戶不在為了尋找最新軟件花費時間。也不用去到開發(fā)商的網(wǎng)站上查找??蛻舳说能浖詣訒诔绦騿忧安檎曳?wù)器上最新的版本。和自己當(dāng)前軟件的版本比較,如果服務(wù)器的是最新版本??蛻舳藙t進行自動下載、解壓、安裝。當(dāng)然了下載是要有網(wǎng)絡(luò)的,并且用戶可以根據(jù)提示去完成操作。再也不用為找不到最新版本的軟件而頭疼。轉(zhuǎn)自:http://blog.csdn.net/lybwwp/article/details/8426022
 
 
 下面是我的大體思路,已經(jīng)得到了實現(xiàn): 1、  寫一個webservice,提供一個獲取服務(wù)器xml中版本的數(shù)據(jù)的方法。| 
 
| <?xml version="1.0"encoding="utf-8"?> |  
|   <Project id="程序名稱"Edition="1.0"> </Project> |  2、  在WinForm應(yīng)用程序啟動的時候,首先訪問webservice獲取服務(wù)器的xml中的版本號,然后獲取客戶端的xml中的版本號。將兩個版本號比較,若服務(wù)器中的版本號大,則提示提示可以在線更新應(yīng)用程序。 3、  然后客戶端自動下載網(wǎng)絡(luò)上的zip壓縮包到本機的指定位置,進行自動解壓縮到安裝的目錄進行覆蓋。解壓縮完畢之后,用進程打開所解壓過的exe文件進行軟件安裝。同時關(guān)閉客戶端軟件所在的進程。 4、注意:升級程序和應(yīng)用程序都是單獨的,應(yīng)用程序在使用時不能對本身進行升級(覆蓋會失?。?。 具體代碼如下: 第一部分 應(yīng)用程序如口Program:
 
| usingSystem.Collections.Generic; |  
| usingSystem.Windows.Forms; |  
| usingDevExpress.XtraEditors; |  
| usingAnshield_AutoFutures.BaseClase; |  
| namespaceAnshield_AutoFutures |  
|         privatestaticvoidLoadMath() |  
|             stringNewEdition = "1.0"; |  
|             stringOldEdition = "1.0"; |  
|                 NewEdition = webserverClase.getCopyRightCode(); |  
|                 String fileName = Application.StartupPath + "\\XMLEdition.xml"; |  
|                 XmlDocument xmlDoc = newXmlDocument(); |  
|                 XmlNode xn = xmlDoc.SelectSingleNode("Content"); |  
|                 XmlNodeList xnl = xn.ChildNodes; |  
|                 foreach(XmlNode xnf inxnl) |  
|                     XmlElement xe = (XmlElement)xnf; |  
|                     if(xe.GetAttribute("id") == "jigou_plz") |  
|                         OldEdition = xe.GetAttribute("Edition"); |  
|                 doublenewE = double.Parse(NewEdition); |  
|                 doubleoldE = double.Parse(OldEdition); |  
|                     DialogResult dr = XtraMessageBox.Show("發(fā)現(xiàn)新的版本是否要更新該軟件", "平浪舟現(xiàn)貨程序化交易--更新提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); |  
|                     if(dr == DialogResult.OK) |  
|                         System.Diagnostics.Process.Start(Application.StartupPath + @"\Upgrade_Form.exe"); |  
|                         anshield_Login login = newanshield_Login(); |  
|                         if(login.ShowDialog() == DialogResult.OK) |  
|                             Application.Run(newMain_AutoFutures()); |  
|                     anshield_Login login = newanshield_Login(); |  
|                     if(login.ShowDialog() == DialogResult.OK) |  
|                         Application.Run(newMain_AutoFutures()); |  
|                 XtraMessageBox.Show("網(wǎng)絡(luò)鏈接失?。?, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); |  
|             System.Threading.Mutex mutexMyapplication = newSystem.Threading.Mutex(false, "jigou_plz.exe"); |  
|             if(!mutexMyapplication.WaitOne(100, false)) |  
|                 XtraMessageBox.Show("程序"+ Application.ProductName + "已經(jīng)在運行!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);               |  
|                 Application.EnableVisualStyles(); |  
|                 Application.SetCompatibleTextRenderingDefault(false); |  
|                 hugengyong hu = newhugengyong();                |  
|                 DevExpress.UserSkins.OfficeSkins.Register(); |  
|                 DevExpress.Skins.SkinManager.EnableFormSkins(); |  
|                 DevExpress.UserSkins.BonusSkins.Register(); |   第二部分:升級程序代碼如下: 
 
|         stringpath = Application.StartupPath; |  
|  privatevoidbtnDown_Click(objectsender, EventArgs e) |  
|             stringzipFile = path + @"\jigou_plz.zip"; |  
|                 stringdownUrl = ConfigurationManager.ConnectionStrings["DownUrl"].ConnectionString.ToString().Trim(); |  
|                 if(!string.IsNullOrEmpty(downUrl)) |  
|                     DownloadFile(downUrl, zipFile, progressBar1, label1); |  
|                     MessageBox.Show("Config中的下載路徑配置錯誤!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); |  
|                 MessageBox.Show("當(dāng)前沒有網(wǎng)絡(luò)或者文件地址不正確"); |  
|                 BaseClase.Zip.UnZip(zipFile, path, "", true,true); |  
|                 MessageBox.Show(ex.Message); |  
|             if(MessageBox.Show("升級完成!,請重新登陸!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) |  
|                 FileInfo file = newFileInfo(path + @"\Anshield_AutoFutures.exe"); |  
|                     Process.Start(path + @"\Anshield_AutoFutures.exe"); |  
|         /// <param name="URL">下載文件地址</param>        |  
|         /// <param name="Filename">下載后的存放地址</param>         |  
|         /// <param name="Prog">用于顯示的進度條</param>         |  
|         publicvoidDownloadFile(stringURL, stringfilename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1) |  
|                 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); |  
|                 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); |  
|                 longtotalBytes = myrp.ContentLength; |  
|                     prog.Maximum = (int)totalBytes; |  
|                 System.IO.Stream st = myrp.GetResponseStream(); |  
|                 System.IO.Stream so = newSystem.IO.FileStream(filename, System.IO.FileMode.Create); |  
|                 longtotalDownloadedByte = 0; |  
|                 byte[] by = newbyte[1024]; |  
|                 intosize = st.Read(by, 0, (int)by.Length); |  
|                     totalDownloadedByte = osize + totalDownloadedByte; |  
|                     System.Windows.Forms.Application.DoEvents(); |  
|                         prog.Value = (int)totalDownloadedByte; |  
|                     osize = st.Read(by, 0, (int)by.Length); |  
|                     percent = (float)totalDownloadedByte / (float)totalBytes * 100; |  
|                     label1.Text = "下載進度"+ percent.ToString() + "%"; |  
|                     System.Windows.Forms.Application.DoEvents();  |  
|                 label1.Text = "下載完成。安裝中... ..."; |  
|         /// 關(guān)閉原有的應(yīng)用程序  |  
|         privatevoidkillProess() |  
|             this.label1.Text = "正在關(guān)閉程序...."; |  
|             System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("Anshield_AutoFutures"); |  
|             foreach(System.Diagnostics.Process pro inproc) |  zip壓縮文件解壓方法類 
 
| usingSystem.Collections.Generic; |  
| usingSystem.IO.Compression; |  
| usingICSharpCode.SharpZipLib.Zip; |  
| namespaceUpgrade_Form.BaseClase |  
|         /// <param name="zipedFile">zip文件路徑</param> |  
|         /// <param name="strDirectory">解壓路徑</param> |  
|         /// <param name="password">zip文件的密碼</param> |  
|         /// <param name="overWrite">是否覆蓋已存在的文件。</param> |  
|         /// <param name="delteFile">解壓后是否刪除文件</param> |  
|         publicstaticvoidUnZip(stringzipedFile, stringstrDirectory, stringpassword, booloverWrite,booldelteFile) |  
|             if(Directory.Exists(strDirectory) == false) |  
|                 Directory.CreateDirectory(strDirectory); |  
|                 strDirectory = Directory.GetCurrentDirectory(); |  
|             if(!strDirectory.EndsWith("\\")) |  
|                 strDirectory = strDirectory + "\\"; |  
|             using(ZipInputStream s = newZipInputStream(File.OpenRead(zipedFile))) |  
|                 while((theEntry = s.GetNextEntry()) != null) |  
|                     stringdirectoryName = ""; |  
|                     pathToZip = theEntry.Name; |  
|                         directoryName = Path.GetDirectoryName(pathToZip) + "\\"; |  
|                     stringfileName = Path.GetFileName(pathToZip); |  
|                     Directory.CreateDirectory(strDirectory + directoryName); |  
|                         if((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName))) |  
|                             using(FileStream streamWriter = File.Create(strDirectory + directoryName + fileName)) |  
|                                 byte[] data = newbyte[2048]; |  
|                                     size = s.Read(data, 0, data.Length); |  
|                                         streamWriter.Write(data, 0, size); |  
 |