//这是一个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);
}
}
webservices如下:
[WebMethod(Description = "取得更新版本")]
public string GetVer()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update/update.xml"));
XmlElement root = doc.DocumentElement;
return root.SelectSingleNode("version").InnerText;
}
[WebMethod(Description = "在线更新软件")]
public System.Xml.XmlDocument GetUpdateData()
{
//取得更新的xml模板内容
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update/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;
}
再建一个xml
<?xml version="1.0" encoding="utf-8" ?>
<product>
<version>1.0.1818.42821</version>
<description>修正一些Bug</description>
<filelist count="7" sourcepath="./update/">
<item name="WinQp.exe" size="">
<value />
</item>
<item name="AccessDAL.dll" size="">
<value />
</item>
<item name="LocalBLL.dll" size="">
<value />
</item>
<item name="Model.dll" size="">
<value />
</item>
<item name="MyADO.dll" size="">
<value />
</item>
<item name="Common.dll" size="">
<value />
</item>
<item name="Excel.dll" size="">
<value />
</item>
</filelist>
</product>
放在服务器上
再新建 一个升级程序,在主程序下调用这个程序,升级程序内容为
if (!File.Exists(Application.StartupPath+"\\update.xml"))
{
MessageBox.Show("升级程序不能单独运行!");
Application.Exit();
}
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in ps)
{
//MessageBox.Show(p.ProcessName.ToLower());
if (p.ProcessName.ToLower() == "qpwin")
{
if (!p.HasExited)
{
p.Kill();
p.WaitForExit();
}
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("qpwin.exe");
Close();
Application.Exit();
posted on 2006-05-16 11:17
CqRandy 阅读(469)
评论(0) 编辑 收藏 引用 所属分类:
ASP.NET 1.1