求大神指点在线升级应该怎么做?

w2362233 2017-12-21 09:38:59
找人外包做了个程序,做到一半人跑了,给了个半成品的demo的源码,升级部分也没完成,下面是升级的部分,求大神帮忙看下我服务端的XML应该怎么写呢,当前现有的程序能否完成升级呢

using demo.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Windows.Forms;

namespace demo.Controller
{
public class UpdateChecker
{

private const string UpdateURL = "http://www.domain.com/update.xml";

public string LatestVersionNumber;
public string LatestVersionURL;
public event EventHandler NewVersionFound;


public const string Name = "demo";

public const string Copyright = "Copyright demo NetWork 2017.";
public const string Version = "4.6.1";
#if !_DOTNET_4_0
public const string NetVer = "2.0";
#elif !_CONSOLE
public const string NetVer = "4.0";
#else
public const string NetVer = "";
#endif
public const string FullVersion = Version +
#if DEBUG
" Debug";
#else
/*
" Alpha";
/*/
"";
//*/
#endif

private static bool UseProxy = true;

public void CheckUpdate(Configuration config)
{
try
{
WebClient http = new WebClient();
http.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36");
if (UseProxy)
{
WebProxy proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort);
if (!string.IsNullOrEmpty(config.authPass))
{
proxy.Credentials = new NetworkCredential(config.authUser, config.authPass);
}
http.Proxy = proxy;
}
else
{
http.Proxy = null;
}
http.DownloadStringCompleted += http_DownloadStringCompleted;
http.DownloadStringAsync(new Uri(UpdateURL + "?rnd=" + Util.Utils.RandUInt32().ToString()));
}
catch (Exception e)
{
Logging.LogUsefulException(e);
}
}

public static int CompareVersion(string l, string r)
{
var ls = l.Split('.');
var rs = r.Split('.');
for (int i = 0; i < Math.Max(ls.Length, rs.Length); i++)
{
int lp = (i < ls.Length) ? int.Parse(ls[i]) : 0;
int rp = (i < rs.Length) ? int.Parse(rs[i]) : 0;
if (lp != rp)
{
return lp - rp;
}
}
return 0;
}

public class VersionComparer : IComparer<string>
{

public int Compare(string x, string y)
{
return CompareVersion(ParseVersionFromURL(x), ParseVersionFromURL(y));
}

}

private static string ParseVersionFromURL(string url)
{
Match match = Regex.Match(url, @".*" + Name + @"-win.*?-([\d\.]+)\.\w+", RegexOptions.IgnoreCase);
if (match.Success)
{
if (match.Groups.Count == 2)
{
return match.Groups[1].Value;
}
}
return null;
}

private void SortVersions(List<string> versions)
{
versions.Sort(new VersionComparer());
}

private bool IsNewVersion(string url)
{
if (url.IndexOf("prerelease") >= 0)
{
return false;
}
// check dotnet 4.0
AssemblyName[] references = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
Version dotNetVersion = Environment.Version;
foreach (AssemblyName reference in references)
{
if (reference.Name == "mscorlib")
{
dotNetVersion = reference.Version;
}
}
if (dotNetVersion.Major >= 4)
{
if (url.IndexOf("dotnet4.0") < 0)
{
return false;
}
}
else
{
if (url.IndexOf("dotnet4.0") >= 0)
{
return false;
}
}
string version = ParseVersionFromURL(url);
if (version == null)
{
return false;
}
string currentVersion = Version;

if (url.IndexOf("banned") > 0 && CompareVersion(version, currentVersion) == 0
|| url.IndexOf("deprecated") > 0 && CompareVersion(version, currentVersion) > 0)
{
Application.Exit();
return false;
}
return CompareVersion(version, currentVersion) > 0;
}

private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
string response = e.Result;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(response);
XmlNodeList elements = xmlDoc.GetElementsByTagName("media:content");
List<string> versions = new List<string>();
foreach (XmlNode el in elements)
{
foreach (XmlAttribute attr in el.Attributes)
{
if (attr.Name == "url")
{
if (IsNewVersion(attr.Value))
{
versions.Add(attr.Value);
}
}
}
}
if (versions.Count == 0)
{
return;
}
// sort versions
SortVersions(versions);
LatestVersionURL = versions[versions.Count - 1];
LatestVersionNumber = ParseVersionFromURL(LatestVersionURL);
if (NewVersionFound != null)
{
NewVersionFound(this, new EventArgs());
}
}
catch (Exception ex)
{
if (e.Error != null)
{
Logging.Debug(e.Error.ToString());
}
Logging.Debug(ex.ToString());
if (NewVersionFound != null)
{
NewVersionFound(this, new EventArgs());
}
return;
}
}
}
}
...全文
462 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
worldy 2017-12-26
  • 打赏
  • 举报
回复
因为程序一旦运行后,就无法改写;因此在线升级一般需要一个中间程序,你的系统由中间程序启动运行;中间程序开始运行时检查远程版本是否大于本地版本,如果是,则先下载覆盖你的程序,否则就不下载,然后,启动运行你的程序。
向立天 2017-12-25
  • 打赏
  • 举报
回复
问错地方了吧,看你这个代码是C#的
赵4老师 2017-12-24
  • 打赏
  • 举报
回复
引用 3 楼 smwhotjay 的回复:
升级 主要是目标程序必须停止,不然被使用,无法覆盖文件的。 文件传输,打包格式。下载,解压,更新目标文件
使用Rename功能修改正运行的exe的文件,使用API WrtieProcessMemory修改内存中正运行exe的汇编指令,完全可以实现正运行程序的热升级。
smwhotjay 2017-12-23
  • 打赏
  • 举报
回复
升级 主要是目标程序必须停止,不然被使用,无法覆盖文件的。 文件传输,打包格式。下载,解压,更新目标文件
oyljerry 2017-12-22
  • 打赏
  • 举报
回复
服务端xml就是配置一个版本和新程序的路径等吧
赵4老师 2017-12-22
  • 打赏
  • 举报
回复
百度搜相关关键字。

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧