自動(dòng)更新的軟件的目的在于讓客戶不在為了尋找最新軟件花費(fèi)時(shí)間。也不用去到開發(fā)商的網(wǎng)站上查找。客戶端的軟件自動(dòng)會(huì)在程序啟動(dòng)前查找服務(wù)器上最新的版本。和自己當(dāng)前軟件的版本比較,如果服務(wù)器的是最新版本??蛻舳藙t進(jìn)行自動(dòng)下載、解壓、安裝。當(dāng)然了下載是要有網(wǎng)絡(luò)的,并且用戶可以根據(jù)提示去完成操作。再也不用為找不到最新版本的軟件而頭疼。 轉(zhuǎn)自:http://blog.csdn.net/lybwwp/article/details/8426022
下面是我的大體思路,已經(jīng)得到了實(shí)現(xiàn): 1、 寫一個(gè)webservice,提供一個(gè)獲取服務(wù)器xml中版本的數(shù)據(jù)的方法。|
<?xml version= "1.0" encoding= "utf-8" ?> |
<Project id= "程序名稱" Edition= "1.0" > </Project> |
2、 在WinForm應(yīng)用程序啟動(dòng)的時(shí)候,首先訪問webservice獲取服務(wù)器的xml中的版本號(hào),然后獲取客戶端的xml中的版本號(hào)。將兩個(gè)版本號(hào)比較,若服務(wù)器中的版本號(hào)大,則提示提示可以在線更新應(yīng)用程序。 3、 然后客戶端自動(dòng)下載網(wǎng)絡(luò)上的zip壓縮包到本機(jī)的指定位置,進(jìn)行自動(dòng)解壓縮到安裝的目錄進(jìn)行覆蓋。解壓縮完畢之后,用進(jìn)程打開所解壓過(guò)的exe文件進(jìn)行軟件安裝。同時(shí)關(guān)閉客戶端軟件所在的進(jìn)程。 4、注意:升級(jí)程序和應(yīng)用程序都是單獨(dú)的,應(yīng)用程序在使用時(shí)不能對(duì)本身進(jìn)行升級(jí)(覆蓋會(huì)失?。?br> 具體代碼如下: 第一部分 應(yīng)用程序如口Program:
using System.Collections.Generic; |
using System.Windows.Forms; |
using DevExpress.XtraEditors; |
using Anshield_AutoFutures.BaseClase; |
namespace Anshield_AutoFutures |
private static void LoadMath() |
string NewEdition = "1.0" ; |
string OldEdition = "1.0" ; |
NewEdition = webserverClase.getCopyRightCode(); |
String fileName = Application.StartupPath + "\\XMLEdition.xml" ; |
XmlDocument xmlDoc = new XmlDocument(); |
XmlNode xn = xmlDoc.SelectSingleNode( "Content" ); |
XmlNodeList xnl = xn.ChildNodes; |
foreach (XmlNode xnf in xnl) |
XmlElement xe = (XmlElement)xnf; |
if (xe.GetAttribute( "id" ) == "jigou_plz" ) |
OldEdition = xe.GetAttribute( "Edition" ); |
double newE = double .Parse(NewEdition); |
double oldE = 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 = new anshield_Login(); |
if (login.ShowDialog() == DialogResult.OK) |
Application.Run( new Main_AutoFutures()); |
anshield_Login login = new anshield_Login(); |
if (login.ShowDialog() == DialogResult.OK) |
Application.Run( new Main_AutoFutures()); |
XtraMessageBox.Show( "網(wǎng)絡(luò)鏈接失??!" , "提示" , MessageBoxButtons.OK, MessageBoxIcon.Error); |
/// 應(yīng)用程序的主入口點(diǎn)。 |
System.Threading.Mutex mutexMyapplication = new System.Threading.Mutex( false , "jigou_plz.exe" ); |
if (!mutexMyapplication.WaitOne(100, false )) |
XtraMessageBox.Show( "程序" + Application.ProductName + "已經(jīng)在運(yùn)行!" , "警告" , MessageBoxButtons.OK, MessageBoxIcon.Warning); |
Application.EnableVisualStyles(); |
Application.SetCompatibleTextRenderingDefault( false ); |
hugengyong hu = new hugengyong(); |
DevExpress.UserSkins.OfficeSkins.Register(); |
DevExpress.Skins.SkinManager.EnableFormSkins(); |
DevExpress.UserSkins.BonusSkins.Register(); |
第二部分:升級(jí)程序代碼如下:
string path = Application.StartupPath; |
private void btnDown_Click( object sender, EventArgs e) |
string zipFile = path + @"\jigou_plz.zip" ; |
string downUrl = ConfigurationManager.ConnectionStrings[ "DownUrl" ].ConnectionString.ToString().Trim(); |
if (! string .IsNullOrEmpty(downUrl)) |
DownloadFile(downUrl, zipFile, progressBar1, label1); |
MessageBox.Show( "Config中的下載路徑配置錯(cuò)誤!" , "錯(cuò)誤" , 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( "升級(jí)完成!,請(qǐng)重新登陸!" , "提示" , MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) |
FileInfo file = new FileInfo(path + @"\Anshield_AutoFutures.exe" ); |
Process.Start(path + @"\Anshield_AutoFutures.exe" ); |
/// <param name="URL">下載文件地址</param> |
/// <param name="Filename">下載后的存放地址</param> |
/// <param name="Prog">用于顯示的進(jìn)度條</param> |
public void DownloadFile( string URL, string filename, 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(); |
long totalBytes = myrp.ContentLength; |
prog.Maximum = ( int )totalBytes; |
System.IO.Stream st = myrp.GetResponseStream(); |
System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); |
long totalDownloadedByte = 0; |
byte [] by = new byte [1024]; |
int osize = 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 = "下載進(jìn)度" + percent.ToString() + "%" ; |
System.Windows.Forms.Application.DoEvents(); |
label1.Text = "下載完成。安裝中... ..." ; |
/// 關(guān)閉原有的應(yīng)用程序 |
private void killProess() |
this .label1.Text = "正在關(guān)閉程序...." ; |
System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName( "Anshield_AutoFutures" ); |
foreach (System.Diagnostics.Process pro in proc) |
zip壓縮文件解壓方法類
using System.Collections.Generic; |
using System.IO.Compression; |
using ICSharpCode.SharpZipLib.Zip; |
namespace Upgrade_Form.BaseClase |
/// <param name="zipedFile">zip文件路徑</param> |
/// <param name="strDirectory">解壓路徑</param> |
/// <param name="password">zip文件的密碼</param> |
/// <param name="overWrite">是否覆蓋已存在的文件。</param> |
/// <param name="delteFile">解壓后是否刪除文件</param> |
public static void UnZip( string zipedFile, string strDirectory, string password, bool overWrite, bool delteFile) |
if (Directory.Exists(strDirectory) == false ) |
Directory.CreateDirectory(strDirectory); |
strDirectory = Directory.GetCurrentDirectory(); |
if (!strDirectory.EndsWith( "\\" )) |
strDirectory = strDirectory + "\\" ; |
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile))) |
while ((theEntry = s.GetNextEntry()) != null ) |
string directoryName = "" ; |
pathToZip = theEntry.Name; |
directoryName = Path.GetDirectoryName(pathToZip) + "\\" ; |
string fileName = 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 = new byte [2048]; |
size = s.Read(data, 0, data.Length); |
streamWriter.Write(data, 0, size); |
|