志在指尖
用双手敲打未来

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);
}
}

未经允许不得转载:IT技术网站 » C#程序在线升级代码
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载