using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
namespace RunUpdateExe.Lib
{
public class CommonHelper
{
#region 方法 -> 获得更新服务区
///
/// 获得需要更新的服务区列表
///
///
///
public static string[] getUpadateServerPart()
{
ClassMessage classMsg = new Lib.ClassMessage();
ClassLog classlog = new Lib.ClassLog();
string[] getUpdateServerPart = new string[0];
string AutoUpdaterFileName = strUpdateURL + ConfigHelper.strUpdateListXmlPath;
try
{
WebClient wc = new WebClient();
Stream sm = wc.OpenRead(AutoUpdaterFileName);
XmlTextReader xml = new XmlTextReader(sm);
while (xml.Read())
{
if (xml.Name == "UpdateServerPart")
{
getUpdateServerPart = xml.GetAttribute("Code").Split('|');
break;
}
}
xml.Close();
sm.Close();
}
catch (WebException ex)
{
throw ex;
}
return getUpdateServerPart;
}
#endregion
#region 方法 -> 获得最后一次更新时间
///
/// 获得服务器更新时间
///
///
///
public static string getServerUpdateTime()
{
ClassMessage classMsg = new ClassMessage();
ClassLog classLog = new ClassLog();
string LastUpdateTime = "";
string AutoUpdaterFileName = strUpdateURL + ConfigHelper.strUpdateListXmlPath;
try
{
WebClient wc = new WebClient();
Stream sm = wc.OpenRead(AutoUpdaterFileName);
XmlTextReader xml = new XmlTextReader(sm);
while (xml.Read())
{
if (xml.Name == "UpdateTime")
{
LastUpdateTime = xml.GetAttribute("Date");
break;
}
}
xml.Close();
sm.Close();
}
catch (WebException ex)
{
throw ex;
}
return LastUpdateTime;
}
#endregion
#region 方法 -> 根据配置获得值
///
/// 获得要运行的程序名称
///
public static string strApplicationName = getConfigValue(ConfigHelper.strUpdateXmlPath, "MainApplicationName");
public static string strStartApp = getConfigValue(ConfigHelper.strUpdateXmlPath, "StartApp");
///
/// 获得最后一次更新时间
///
public static string strLastUpdateDate = getConfigValue(ConfigHelper.strUpdateXmlPath, "UpDate");
///
/// 根据配置获得Url值
///
public static string strUpdateURL = "http://" + getConfigValue(ConfigHelper.strUpdateXmlPath, "IP") + ":" + getConfigValue(ConfigHelper.strUpdateXmlPath, "Port") + "/";
public static string strIP = getConfigValue(ConfigHelper.strUpdateXmlPath, "IP");
///
/// 根据配置获得值
///
/// 路径
/// 键值
///
private static string getConfigValue(string path, string appKey)
{
ClassMessage classMsg = new ClassMessage();
ClassLog classLog = new ClassLog();
XmlDocument xDoc = new XmlDocument();
XmlNode xNode;
XmlElement xElem = null;
try
{
xDoc.Load(path);
xNode = xDoc.SelectSingleNode("//appSettings");
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]");
}
catch (XmlException ex)
{
classMsg.messageInfoBox(ex.Message);
classLog.WriteLog(ClassLog.LogType.Error, "Source:{" + ex.Source + "}" + " StackTrace:{" + ex.StackTrace + "}" +
" Message:{" + ex.Message + "}");
}
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
#endregion
public static string convertSize(long byteSize)
{
string str = "";
float tempf = (float)byteSize;
if (tempf / 1024 > 1)
{
if ((tempf / 1024) / 1024 > 1)
{
str = ((tempf / 1024) / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "MB";
}
else
{
str = (tempf / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "KB";
}
}
else
{
str = tempf.ToString(CultureInfo.InvariantCulture) + "B";
}
return str;
}
///
/// 获取更新文件大小统计
///
/// 更新文件数据XML
/// 返回值
public static long getUpdateSize(string filePath)
{
long len = 0;
try
{
WebClient wc = new WebClient();
Stream sm = wc.OpenRead(filePath);
XmlTextReader xr = new XmlTextReader(sm);
while (xr.Read())
{
if (xr.Name == "UpdateSize")
{
len = Convert.ToInt64(xr.GetAttribute("Size"), CultureInfo.InvariantCulture);
break;
}
}
xr.Close();
sm.Close();
}
catch (WebException ex)
{
throw ex;
}
return len;
}
///
/// 创建多层文件夹
///
/// 弹出文件浏览窗口选中的符文件路径
/// 需要创建的多层文件夹路径
public static void createMultipleFolders(string strParentPath, string strPath)
{
try
{
if (strParentPath != "")
{
Directory.CreateDirectory(strParentPath + @strPath);
}
}
catch (IOException ex)
{
throw ex;
}
}
public static string[] GetKillAppList = getConfigValue(ConfigHelper.strUpdateXmlPath, "KillApp").Split('|');
public static string CheckUpdate = getConfigValue(ConfigHelper.strUpdateXmlPath, "CheckUpdate");
///
/// 程序更新完后,要更新update.xml
///
/// update.xml文件的路径
/// "key"的值
/// "value"的值
internal static void setConfigValue(string strXmlPath, string appKey, string appValue)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(strXmlPath);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]");
if (xElem1 != null) xElem1.SetAttribute("value", appValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", appKey);
xElem2.SetAttribute("value", appValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(strXmlPath);
}
catch (XmlException ex)
{
throw ex;
}
}
}
}