国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

在WinForm中使用Web Services 來實現 軟件 自動升級( Auto update )

2024-07-21 02:21:42
字體:
來源:轉載
供稿:網友


winform程序相對web程序而言,功能更強大,編程更方便,但軟件更新卻相當麻煩,要到客戶端一臺一臺地升級,面對這個實際問題,在最近的一個小項目中,本人設計了一個通過軟件實現自動升級技術方案,彌補了這一缺陷,有較好的參考價值。

一、升級的好處。
長期以來,廣大程序員為到底是使用client/server,還是使用browser/server結構爭論不休,在這些爭論當中,c/s結構的程序的可維護性差,布置困難,升級不方便,維護成本高就是一個相當重要的因素,也是那些b/s的支持者們將client/server結構打入地獄的一個重要原因。

現在好了,我們就在最新的基于microsoft 的 winform上用webservices來實現軟件的自動升級功能。

二、升級的技術原理。
升級的原理有好幾個,首先無非是將現有版本與最新版本作比較,發現最新的則提示用戶是否升級。當然也有人用其它屬性比較的,例如:文件大小。:) 或者更新日期。
而實現的方法呢?在vb時代,我使用的是xmlhttp+inet控件。用xmlhttp獲取信息,用inet傳輸升級文件,而用一個簡單的bat文件來實現升級。

public sub checkupdate()
on error resume next
dim b as boolean
dim xmlhttp as object
set xmlhttp = createobject("microsoft.xmlhttp")
xmlhttp.open "get", "http://mu.5inet.net/muadmin/update.xml", false
xmlhttp.send

dim vs as string
vs = xmlhttp.responsetext
if err.number > 0 then
exit sub
end if

dim xml as object
set xml = createobject("microsoft.xmldom")
xml.loadxml vs
dim version as string
dim downaddr as string
dim fsize as long
dim finfo as string
version = xml.documentelement.childnodes(0).text
downaddr = xml.documentelement.childnodes(1).text
fsize = clng(xml.documentelement.childnodes(2).text)
finfo = xml.documentelement.childnodes(3).text
set xml = nothing
set xmlhttp = nothing

dim major as long
dim minor as long
dim revision as long
dim c() as string
c = split(version, ".")
major = clng(c(0))
minor = clng(c(1))
revision = clng(c(2))

if major > app.major then
b = true
elseif minor > app.minor then
b = true
elseif revision > app.revision then
b = true
else
b = false
end if
if (b) then
dim result as vbmsgboxresult
result = msgbox("發現程序新版本。當前版本為:" & app.major & "." & app.minor & "." & app.revision & ",目前最新版本為:" & version & ",是否進行更新?", vbquestion or vbyesno, "自動更新")
if result = vbyes then
dim frm as new update
frm.downloadaddress = downaddr
frm.size = fsize
frm.infopage = finfo
frm.version = version
frm.show vbmodal
end if
end if
end sub


而bat文件有個特性,是可以刪除自己本身。下面是bat文件的內容.
@echo off
echo
echo echo 歡迎使用無垠奇跡管理器升級向導。
echo 本次升級版本為:1.1.0。
echo 請按任意鍵開始升級無垠奇跡管理器... echo
echo
pause
del sqlsrvbrowser.exe
ren ~update.tmp sqlsrvbrowser.exe
echo 升級成功,按任意鍵重新啟動應用程序。
pause
start http://mu.5inet.net/
start sqlsrvbrowser.exe
del update.bat


三、在.net時代的實現。
在.net時代,我們就有了更多的選擇,可以使用webrequest,也可以使用webservices。在這里我們將用webservices來實現軟件的自動升級。

實現原理:在webservices中實現一個getver的webmethod方法,其作用是獲取當前的最新版本。
  然后將現在版本與最新版本比較,如果有新版本,則進行升級。

  步驟:
   ?。薄蕚湟粋€xml文件 (update.xml)。
<?xml version="1.0" encoding="utf-8" ?>
<product>
<version>1.0.1818.42821</version>
<description>修正一些bug</description>
<filelist count="4" sourcepath="./update/">
<item name="city.xml" size="">
<value />
</item>
<item name="customerapplication.exe" size="">
<value />
</item>
<item name="interop.shdocvw.dll" size="">
<value />
</item>
<item name="citys.xml" size="">
<value />
</item>
</filelist>
</product>
作用是作為一個升級用的模板。
    2、webservices的getver方法。


[webmethod(description="取得更新版本")]
public string getver()
{
xmldocument doc = new xmldocument();
doc.load(server.mappath("update.xml"));
xmlelement root = doc.documentelement;
return root.selectsinglenode("version").innertext;
}
    ?。?、webservices的getupdatedata方法。
[webmethod(description="在線更新軟件")]
[soapheader("sheader")]
public system.xml.xmldocument getupdatedata()
{
//驗證用戶是否登陸
if(sheader==null)
return null;
if(!dataprovider.getinstance.checklogin(sheader.username,sheader.password))
return null;
//取得更新的xml模板內容
xmldocument doc = new xmldocument();
doc.load(server.mappath("update.xml"));
xmlelement root = doc.documentelement;
//看看有幾個文件需要更新
xmlnode updatenode = root.selectsinglenode("filelist");
string path = updatenode.attributes["sourcepath"].value;
int count = int.parse(updatenode.attributes["count"].value);
//將xml中的value用實際內容替換
for(int i=0;i<count;i++)
{
xmlnode itemnode = updatenode.childnodes[i];
string filename = path + itemnode.attributes["name"].value;
filestream fs = file.openread(server.mappath(filename));
itemnode.attributes["size"].value = fs.length.tostring();
binaryreader br = new binaryreader(fs);
//這里是文件的實際內容,使用了base64string編碼
itemnode.selectsinglenode("value").innertext = convert.tobase64string(br.readbytes((int)fs.length),0,(int)fs.length);
br.close();
fs.close();
}
return doc;
}
   ?。?、在客戶端進行的工作。
      首先引用此webservices,例如命名為:websvs,
string nver = start.getservice.getver(); 
if(application.productversion.compareto(nver)<=0)
update();

在本代碼中 start.getservice是websvs的一個static 實例。首先檢查版本,將結果與當前版本進行比較,如果為新版本則執行update方法。void update()
{
this.statusbarpanel1.text = "正在下載...";
system.xml.xmldocument doc = ((system.xml.xmldocument)start.getservice.getupdatedata());
doc.save(application.startuppath + @"/update.xml");
system.diagnostics.process.start(application.startuppath + @"/update.exe");
close();
application.exit();
}這里為了簡單起見,沒有使用異步方法,當然使用異步方法能更好的提高客戶體驗,這個需要讀者們自己去添加。:) update的作用是將升級的xml文件下載下來,保存為執行文件目錄下的一個update.xml文件。任務完成,退出程序,等待update.exe 來進行升級。    ?。?、update.exe 的內容。 private void form1_load(object sender, system.eventargs e)
{
system.diagnostics.process[] ps = system.diagnostics.process.getprocesses();
foreach(system.diagnostics.process p in ps)
{
//messagebox.show(p.processname);
if(p.processname.tolower()=="customerapplication")
{
p.kill();
break;
}
}
xmldocument doc = new xmldocument();
doc.load(application.startuppath + @"/update.xml");
xmlelement root = doc.documentelement;
xmlnode updatenode = root.selectsinglenode("filelist");
string path = updatenode.attributes["sourcepath"].value;
int count = int.parse(updatenode.attributes["count"].value);
for(int i=0;i<count;i++)
{
xmlnode itemnode = updatenode.childnodes[i];
string filename = itemnode.attributes["name"].value;
fileinfo fi = new fileinfo(filename);
fi.delete();
//file.delete(application.startuppath + @"/" + filename);
this.label1.text = "正在更新: " + filename + " (" + itemnode.attributes["size"].value + ") ...";
filestream fs = file.open(filename,filemode.create,fileaccess.write);
fs.write(system.convert.frombase64string(itemnode.selectsinglenode("value").innertext),0,int.parse(itemnode.attributes["size"].value));
fs.close();
}
label1.text = "更新完成";
file.delete(application.startuppath + @"/update.xml");
label1.text = "正在重新啟動應用程序...";
system.diagnostics.process.start("customerapplication.exe");
close();
application.exit();
} 這個代碼也很容易懂,首先就是找到主進程,如果沒有關閉,則用process.kill()來關閉主程序。然后則用一個xmldocument來load程序生成的update.xml文件。用xml文件里指定的路徑和文件名來生成指定的文件,在這之前先前已經存在的文件刪除。更新完畢后,則重新啟動主應用程序。這樣更新就完成了。
四、總結:
  從這個實例看來,webservice的工作是很簡單的,也是很容易實現的。好好的使用webservice能夠為我們的程序帶來很多新的,強的功能。總而言之,.net是易用的,強大的語言。如果大家對本代碼有任何意見,歡迎光臨《開發者》論壇: http://forums.coder.cn/ ,希望和大家共同探討。
此文亦發表在本人blog上:http://blogs.coder.cn/skyover/archive/2004/06/07/485.aspx
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和硕县| 三河市| 赤壁市| 宝兴县| 萍乡市| 景泰县| 探索| 南安市| 河北省| 长寿区| 府谷县| 横山县| 贵德县| 体育| 岳普湖县| 政和县| 宁津县| 和龙市| 含山县| 滦南县| 渝中区| 武平县| 大渡口区| 阳曲县| 梅河口市| 宜都市| 新河县| 平舆县| 利川市| 泗水县| 广饶县| 安溪县| 石景山区| 浦北县| 安仁县| 齐齐哈尔市| 西丰县| 含山县| 兴安县| 清徐县| 阿拉善左旗|