資源簡介
對C#不是很熟,擅長C語音,網上找的不能自己用,就按照自己需求改的,因為程序不大,所有就直接控制升級,不需要點擊任何按鈕。大致流程:主程序開機后初始化里面加載升級程序,升級程序讀取本地的XML和從服務器下載的XML看是否需要升級,如果升級就下載高版本升級程序,然后關閉當前低版本程序,再打開高版本升級的程序,接在刪除第版本程序文件,最好下載服務器的XML文檔覆蓋本地是的!升級程序已經寫好備注,有點語法基礎應該都可以成功!使用時需要吧升級程序的EXE和主程序EXE還有XML放在一個文件夾里面,XML文件需要修改你服務器存放的升級文件路徑及程序名。
自己的主程序初始化里面添加這么一段:
string str = System.Environment.CurrentDirectory + "\\" + "Check_SoftwareV1.1.exe";//獲得當前運行程序的路徑
try
{
Process.Start(str);//打開升級程序
}
catch
{
MessageBox.Show("檢查程序運行出錯,請注意文件是否存在.", "錯誤");//錯誤處理
}
因為網上找了很多都不能直接用,所有寫了這么詳細一個教程,希望幫助大家!如果遇到問題歡迎回復詢問,我也是個業余搞C#的,有錯誤也歡迎大家指出!

代碼片段和文件信息
using?System;
using?System.Web;
using?System.IO;
using?System.Net;
using?System.xml;
using?System.Collections;
using?System.Windows.Forms;
using?System.ComponentModel;
namespace?AutoUpdate
{
///?
///?updater?的摘要說明。
///?
public?class?AppUpdater:IDisposable
{
#region?成員與字段屬性
private?string?_updaterUrl;
private?bool?disposed?=?false;
private?IntPtr?handle;
private?Component?component?=?new?Component();
[System.Runtime.InteropServices.DllImport(“Kernel32“)]
private?extern?static?Boolean?CloseHandle(IntPtr?handle);
public?string?UpdaterUrl
{
set{_updaterUrl?=?value;}
get{return?this._updaterUrl;}
}
#endregion
///?
///?AppUpdater構造函數
///?
public?AppUpdater()
{
this.handle?=?handle;
}
public?void?Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private?void?Dispose(bool?disposing)
{
if(!this.disposed)
{
if(disposing)
{
component.Dispose();
}
CloseHandle(handle);
handle?=?IntPtr.Zero;????????????
}
disposed?=?true;?????????
}
~AppUpdater()??????
{
Dispose(false);
}
///?
///?檢查更新文件
///?
///?lFile“>
///?lFile“>
///?
///?
public?int?CheckForUpdate(string?serverxmlFilestring?localxmlFileout?Hashtable?updateFileList)
{
updateFileList?=?new?Hashtable();
if(!File.Exists(localxmlFile)?||?!File.Exists(serverxmlFile))
{
return?-1;
}
xmlFiles?serverxmlFiles?=?new?xmlFiles(serverxmlFile);
xmlFiles?localxmlFiles?=?new?xmlFiles(localxmlFile);
xmlNodeList?newNodeList?=?serverxmlFiles.GetNodeList(“AutoUpdater/Files“);
xmlNodeList?oldNodeList?=?localxmlFiles.GetNodeList(“AutoUpdater/Files“);
int?k?=?0;
for(int?i?=?0;i? {
string?[]?fileList?=?new?string[3];
string?newFileName?=?newNodeList.Item(i).Attributes[“Name“].Value.Trim();
string?newVer?=?newNodeList.Item(i).Attributes[“Ver“].Value.Trim();
ArrayList?oldFileAl?=?new?ArrayList();
for(int?j?=?0;j? {
string?oldFileName?=?oldNodeList.Item(j).Attributes[“Name“].Value.Trim();
string?oldVer?=?oldNodeList.Item(j).Attributes[“Ver“].Value.Trim();
oldFileAl.Add(oldFileName);
oldFileAl.Add(oldVer);
}
int?pos?=?oldFileAl.IndexOf(newFileName);
if(pos?==?-1)
{
fileList[0]?=?newFileName;
fileList[1]?=?newVer;
updateFileList.Add(kfileList);
k++;
}
else?if(pos?>?-1?&&?newVer.CompareTo(oldFileAl[pos+1].ToString())>0?)
{
fileList[0]?=?newFileName;
fileList[1]?=?newVer;
updateFileList.Add(kfileList);
k++;
}
}
return?k;
}
///?
///?檢查更新文件
///?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-01-10?15:21??C#升級程序源碼\
?????文件???????25214??2004-01-26?00:38??C#升級程序源碼\App.ico
?????文件????????5821??2005-09-23?09:30??C#升級程序源碼\AppUpdater.cs
?????文件????????1932??2018-01-10?11:30??C#升級程序源碼\AssemblyInfo.cs
?????文件????????4358??2018-01-10?11:30??C#升級程序源碼\AutoUpdate.csproj
?????文件????????1812??2017-12-29?15:17??C#升級程序源碼\AutoUpdate.csproj.user
?????文件?????????904??2017-12-29?15:17??C#升級程序源碼\AutoUpdate.sln
?????文件????????8704??2006-02-15?09:31??C#升級程序源碼\AutoUpdate.suo
?????文件???????53248??2018-01-10?15:35??C#升級程序源碼\AutoUpdate.v11.suo
?????目錄???????????0??2018-01-10?15:21??C#升級程序源碼\Backup\
?????文件???????25214??2004-01-26?00:38??C#升級程序源碼\Backup\App.ico
?????文件????????5821??2005-09-23?09:30??C#升級程序源碼\Backup\AppUpdater.cs
?????文件????????1859??2005-09-04?21:30??C#升級程序源碼\Backup\AssemblyInfo.cs
?????文件????????5452??2005-10-05?00:06??C#升級程序源碼\Backup\AutoUpdate.csproj
?????文件????????1803??2006-02-15?09:31??C#升級程序源碼\Backup\AutoUpdate.csproj.user
?????文件?????????905??2005-09-04?21:30??C#升級程序源碼\Backup\AutoUpdate.sln
?????文件????????8704??2006-02-15?09:31??C#升級程序源碼\Backup\AutoUpdate.v11.suo
?????文件???????19038??2006-02-14?10:01??C#升級程序源碼\Backup\FrmUpdate.cs
?????文件??????163588??2005-09-22?16:05??C#升級程序源碼\Backup\FrmUpdate.resx
?????文件????????1334??2005-09-05?15:49??C#升級程序源碼\Backup\IniFiles.cs
?????文件????????1303??2005-09-21?16:18??C#升級程序源碼\Backup\xm
?????目錄???????????0??2018-01-10?15:21??C#升級程序源碼\bin\
?????目錄???????????0??2018-01-10?15:35??C#升級程序源碼\bin\Debug\
?????文件??????167936??2005-10-04?23:48??C#升級程序源碼\bin\Debug\AutoUpdate.dll
?????文件???????40448??2017-12-29?17:21??C#升級程序源碼\bin\Debug\AutoUpdate.pdb
?????文件?????????490??2015-06-05?02:48??C#升級程序源碼\bin\Debug\AutoUpdate.vshost.exe.manifest
?????文件???????49152??2018-01-10?14:58??C#升級程序源碼\bin\Debug\Check_SoftwareV1.1.exe
?????文件???????36352??2018-01-10?14:58??C#升級程序源碼\bin\Debug\Check_SoftwareV1.1.pdb
?????文件???????21464??2018-01-10?15:34??C#升級程序源碼\bin\Debug\Check_SoftwareV1.1.vshost.exe
?????文件?????????490??2015-06-05?02:48??C#升級程序源碼\bin\Debug\Check_SoftwareV1.1.vshost.exe.manifest
?????文件?????????480??2018-01-10?15:39??C#升級程序源碼\bin\Debug\UpdateList.xm
............此處省略30個文件信息
- 上一篇:WinForm窗體及其控件的自適應
- 下一篇:Modbus Slave設置文件
評論
共有 條評論