//这是一个webservice
private appupdate.updateserv updatesvr;
private void button1_click(object sender, system.eventargs e)
{
if(linkwebservices()==true)
{
this.label1.text="连接服务器....... pass";
if(checkver()==true)
{
this.label2.text="检查最新版本并下载.......pass";
}
else
{
this.label2.text="检查最新版本并下载.......fail";
}
}
else
{
this.label1.text="连接服务器.......fail";
}
}
//这是用来与升级服务器建立连接
private bool linkwebservices()
{
try
{
updatesvr=new updateserv();
return true;
}
catch
{
return false;
}
}
//调用webservice用来检查是不是有最新的版本
private bool checkver()
{
string path =application.startuppath;
try
{
versioncheck(path);
return true;
}
catch(exception ex)
{
messagebox.show(ex.tostring());
return false;
}
}
private void versioncheck(string despath)
{
try
{
#region 查看文件和目录
if(!despath.endswith(@"/"))
despath += @"/";
if(!system.io.directory.exists(despath))
{
system.io.directory.createdirectory(despath);
}
string temppath = despath + @"tempdespathcache/";
if(system.io.directory.exists(temppath))
{
system.io.directory.delete(temppath,true);
system.io.directory.createdirectory(temppath);
}
else
system.io.directory.createdirectory(temppath);
if(!system.io.file.exists(despath + "updateconfig.xml"))
{
system.xml.xmldocument updateconfig = new system.xml.xmldocument();
updateconfig.loadxml(@"<root></root>");
updateconfig.save(despath + "updateconfig.xml");
}
#endregion
system.xml.xmldocument serverxmldoc = updatesvr.appupdatevertion();
system.xml.xmldocument localxmldoc = new system.xml.xmldocument();
localxmldoc.load(despath + "updateconfig.xml");
bool newversionexist = false;
bool moduleexist = false;
system.xml.xmlnode servernode0 = serverxmldoc.childnodes[0];
system.xml.xmlnode localnode0 = localxmldoc.childnodes[0];
foreach(system.xml.xmlnode servernode in servernode0)
{
moduleexist = false;
foreach(system.xml.xmlnode localnode in localnode0)
{
//找到对应模块
if(localnode.childnodes[0].innertext == servernode.childnodes[0].innertext)
{
moduleexist = true;
//版本号判断
if(localnode.childnodes[1].innertext.compareto(servernode.childnodes[1].innertext) < 0)
{
newversionexist = true;
if(system.configuration.configurationsettings.appsettings["netstyle"].tostring()=="internet")
{
downloadfile(servernode.childnodes[2].innertext,temppath + servernode.childnodes[0].innertext);
}
else
{
downloadfile(servernode.childnodes[3].innertext,temppath + servernode.childnodes[0].innertext);
}
}
break;
}
}
//没找到对应模块
if(false == moduleexist)
{
if(system.configuration.configurationsettings.appsettings["netstyle"].tostring()=="internet")
{
downloadfile(servernode.childnodes[2].innertext,temppath + servernode.childnodes[0].innertext);
}
else
{
downloadfile(servernode.childnodes[3].innertext,temppath + servernode.childnodes[0].innertext);
}
}
}
//写入新updateconfig.xml升级完毕后替换
if(newversionexist)
{
serverxmldoc.save(temppath + "updateconfig.xml");
if(dialogresult.yes == messagebox.show("有新版本,是否更新?","提示",messageboxbuttons.yesno))
{
string[] dirs = system.io.directory.getfiles(temppath, "*.*");
string filename;
foreach (string dir in dirs)
{
filename = ((dir.split(convert.tochar(@"/")))[dir.split(convert.tochar(@"/")).length - 1]);
if(system.io.file.exists(despath + filename))
{
//todo:可以支持备份以前版本
system.io.file.delete(despath + filename);
}
//todo:如果系统正在运行,您得停止系统,至于如何停止,也许可以使用system.diagnostics.process
system.io.file.move(dir,despath + filename);
}
messagebox.show("升级完毕");
}
else
{
//todo:可以支持重新提示升级
}
}
}
catch(exception ex)
{
throw new exception("升级失败,原因是:" + ex.message,ex);
}
}
//下载最新的文件
private void downloadfile(string source,string filename)
{
try
{
system.net.webclient mywebclient = new system.net.webclient();
mywebclient.downloadfile(source,filename);
}
catch(exception ex)
{
throw new exception("下载失败,原因是:" + ex.message,ex);
}
}