C#下实现在线升级

None.gif//这是一个webservice
None.gif

None.gif
private AppUpdate.UpdateServ UpdateSvr;
None.gif
None.gif
None.gif
private void button1_Click(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif 
InBlock.gif
if(LinkWebServices()==true)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.label1.Text="连接服务器dot.gifdot.gif. PASS";
InBlock.gif
InBlock.gif
if(CheckVer()==true)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.label2.Text="检查最新版本并下载dot.gifdot.gif.PASS";
InBlock.gif 
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.label2.Text="检查最新版本并下载dot.gifdot.gif.FAIL";
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.label1.Text="连接服务器dot.gifdot.gif.FAIL";
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
//这是用来与升级服务器建立连接
None.gif
private bool LinkWebServices()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifUpdateSvr
=new UpdateServ();
InBlock.gif
return true;
ExpandedSubBlockEnd.gif}

InBlock.gif
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
//调用webservice用来检查是不是有最新的版本
None.gif
private bool CheckVer()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
string path =Application.StartupPath;
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifVersionCheck(path);
InBlock.gif
return true;
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMessageBox.Show(ex.ToString());
InBlock.gif
return false;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
private void VersionCheck(string desPath)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif
查看文件和目录#region 查看文件和目录
InBlock.gif
if(!desPath.EndsWith(@"\"))
InBlock.gifdesPath 
+= @"\";
InBlock.gif
InBlock.gif
if(!System.IO.Directory.Exists(desPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.IO.Directory.CreateDirectory(desPath);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
string tempPath = desPath + @"tempDesPathCache\";
InBlock.gif
InBlock.gif
if(System.IO.Directory.Exists(tempPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.IO.Directory.Delete(tempPath,
true);
InBlock.gifSystem.IO.Directory.CreateDirectory(tempPath);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifSystem.IO.Directory.CreateDirectory(tempPath);
InBlock.gif
InBlock.gif
if(!System.IO.File.Exists(desPath + "UpdateConfig.xml"))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.Xml.XmlDocument updateConfig 
= new System.Xml.XmlDocument();
InBlock.gifupdateConfig.LoadXml(@
"<root></root>");
InBlock.gifupdateConfig.Save(desPath 
+ "UpdateConfig.xml");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif
#endregion

InBlock.gif
InBlock.gif
InBlock.gifSystem.Xml.XmlDocument serverXmlDoc 
= UpdateSvr.AppUpdateVertion();
InBlock.gifSystem.Xml.XmlDocument localXmlDoc 
= new System.Xml.XmlDocument();
InBlock.giflocalXmlDoc.Load(desPath 
+ "UpdateConfig.xml");
InBlock.gif
bool newVersionExist = false;
InBlock.gif
bool moduleExist = false;
InBlock.gifSystem.Xml.XmlNode serverNode0 
= serverXmlDoc.ChildNodes[0];
InBlock.gifSystem.Xml.XmlNode localNode0 
= localXmlDoc.ChildNodes[0];
InBlock.gif
foreach(System.Xml.XmlNode serverNode in serverNode0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifmoduleExist 
= false;
InBlock.gif
foreach(System.Xml.XmlNode localNode in localNode0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//找到对应模块
InBlock.gif
if(localNode.ChildNodes[0].InnerText == serverNode.ChildNodes[0].InnerText)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifmoduleExist 
= true;
InBlock.gif
//版本号判断
InBlock.gif
if(localNode.ChildNodes[1].InnerText.CompareTo(serverNode.ChildNodes[1].InnerText) < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifnewVersionExist 
= true;
InBlock.gif
if(System.Configuration.ConfigurationSettings.AppSettings["NetStyle"].ToString()=="internet")
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDownloadFile(serverNode.ChildNodes[
2].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDownloadFile(serverNode.ChildNodes[
3].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
//没找到对应模块
InBlock.gif
if(false == moduleExist)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
if(System.Configuration.ConfigurationSettings.AppSettings["NetStyle"].ToString()=="internet")
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDownloadFile(serverNode.ChildNodes[
2].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDownloadFile(serverNode.ChildNodes[
3].InnerText,tempPath + serverNode.ChildNodes[0].InnerText);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
//写入新UpdateConfig.xml升级完毕后替换
InBlock.gif
if(newVersionExist)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifserverXmlDoc.Save(tempPath 
+ "UpdateConfig.xml");
InBlock.gif
if(DialogResult.Yes == MessageBox.Show("有新版本,是否更新?","提示",MessageBoxButtons.YesNo))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
string[] dirs = System.IO.Directory.GetFiles(tempPath, "*.*");
InBlock.gif
string fileName;
InBlock.gif
foreach (string dir in dirs)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffileName 
= ((dir.Split(Convert.ToChar(@"\")))[dir.Split(Convert.ToChar(@"\")).Length - 1]);
InBlock.gif
if(System.IO.File.Exists(desPath + fileName))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//TODO:可以支持备份以前版本
InBlock.gif
System.IO.File.Delete(desPath + fileName);
ExpandedSubBlockEnd.gif}

InBlock.gif
//TODO:如果系统正在运行,您得停止系统,至于如何停止,也许可以使用System.Diagnostics.Process
InBlock.gif
System.IO.File.Move(dir,desPath + fileName);
ExpandedSubBlockEnd.gif}

InBlock.gifMessageBox.Show(
"升级完毕");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//TODO:可以支持重新提示升级
ExpandedSubBlockEnd.gif
}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Exception("升级失败,原因是:" + ex.Message,ex);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
//下载最新的文件
None.gif

None.gif
private void DownloadFile(string source,string fileName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.Net.WebClient myWebClient 
= new System.Net.WebClient();
InBlock.gifmyWebClient.DownloadFile(source,fileName);
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Exception("下载失败,原因是:" + ex.Message,ex);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif 
None.gif
None.gif

转载于:https://www.cnblogs.com/ami/archive/2006/07/20/455707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值