C#程序在线升级代码

//这是一个webservice

privateAppUpdate.UpdateServUpdateSvr;
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
if(LinkWebServices()==true)
{
this.label1.Text=”衔接服务器…….PASS”;
if(CheckVer()==true)
{
this.label2.Text=”查看最新版别并下载…….PASS”;
}
else
{
this.label2.Text=”查看最新版别并下载…….FAIL”;
}
}
else
{
this.label1.Text=”衔接服务器…….FAIL”;
}
}
//这是用来与晋级服务器树立衔接
privateboolLinkWebServices()
{
try
{
UpdateSvr=newUpdateServ();
returntrue;
}
catch
{
returnfalse;
}
}
//调用webservice用来查看是不是有最新的版别
privateboolCheckVer()
{
stringpath=Application.StartupPath;
try
{
VersionCheck(path);
returntrue;
}
catch(Exceptionex)
{
MessageBox.Show(ex.ToString());
returnfalse;
}
}
privatevoidVersionCheck(stringdesPath)
{
try
{
#region查看文件和目录
if(!desPath.EndsWith(@””))
desPath+=@””;
if(!System.IO.Directory.Exists(desPath))
{
System.IO.Directory.CreateDirectory(desPath);
}
stringtempPath=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.XmlDocumentupdateConfig=newSystem.Xml.XmlDocument();
updateConfig.LoadXml(@””);
updateConfig.Save(desPath+”UpdateConfig.xml”);
}
#endregion
System.Xml.XmlDocumentserverXmlDoc=UpdateSvr.AppUpdateVertion();
System.Xml.XmlDocumentlocalXmlDoc=newSystem.Xml.XmlDocument();
localXmlDoc.Load(desPath+”UpdateConfig.xml”);
boolnewVersionExist=false;
boolmoduleExist=false;
System.Xml.XmlNodeserverNode0=serverXmlDoc.ChildNodes[0];
System.Xml.XmlNodelocalNode0=localXmlDoc.ChildNodes[0];
foreach(System.Xml.XmlNodeserverNodeinserverNode0)
{
moduleExist=false;
foreach(System.Xml.XmlNodelocalNodeinlocalNode0)
{
//找到对应模块
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,”*.*”);
stringfileName;
foreach(stringdirindirs)
{
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(Exceptionex)
{
thrownewException(“晋级失利,原因是:”+ex.Message,ex);
}
}
//下载最新的文件
privatevoidDownloadFile(stringsource,stringfileName)
{
try
{
System.Net.WebClientmyWebClient=newSystem.Net.WebClient();
myWebClient.DownloadFile(source,fileName);
}
catch(Exceptionex)
{
thrownewException(“下载失利,原因是:”+ex.Message,ex);
}
}

© 版权声明
好牛新坐标
版权声明:
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com

相关文章