260 lines
8.5 KiB
C#
260 lines
8.5 KiB
C#
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 ProvinceUpdateExe.Lib
|
||
{
|
||
public class CommonHelper
|
||
{
|
||
#region 方法 -> 获得更新服务区
|
||
/// <summary>
|
||
/// 获得需要更新的服务区列表
|
||
/// </summary>
|
||
/// <param name="Dir"></param>
|
||
/// <returns></returns>
|
||
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 方法 -> 获得最后一次更新时间
|
||
/// <summary>
|
||
/// 获得服务器更新时间
|
||
/// </summary>
|
||
/// <param name="Dir"></param>
|
||
/// <returns></returns>
|
||
public static string getServerUpdateTime(string url)
|
||
{
|
||
ClassMessage classMsg = new ClassMessage();
|
||
ClassLog classLog = new ClassLog();
|
||
string LastUpdateTime = "";
|
||
string AutoUpdaterFileName = url;
|
||
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 方法 -> 根据配置获得值
|
||
|
||
/// <summary>
|
||
/// 获得要运行的程序名称
|
||
/// </summary>
|
||
public static string strApplicationName = getConfigValue(ConfigHelper.strUpdateXmlPath, "MainApplicationName");
|
||
|
||
public static string strStartApp = getConfigValue(ConfigHelper.strUpdateXmlPath, "StartApp");
|
||
|
||
/// <summary>
|
||
/// 获得最后一次更新时间
|
||
/// </summary>
|
||
public static string strLastUpdateDate = getConfigValue(ConfigHelper.strUpdateXmlPath, "UpDate");
|
||
|
||
|
||
/// <summary>
|
||
/// 根据配置获得Url值
|
||
/// </summary>
|
||
public static string strUpdateURL = "http://" + getConfigValue(ConfigHelper.strUpdateXmlPath, "IP") + ":" + getConfigValue(ConfigHelper.strUpdateXmlPath, "Port") + "/";
|
||
|
||
public static string strIP = getConfigValue(ConfigHelper.strUpdateXmlPath, "IP");
|
||
|
||
public static string strProvince = getConfigValue(ConfigHelper.strUpdateXmlPath, "Province");
|
||
|
||
/// <summary>
|
||
/// 根据配置获得值
|
||
/// </summary>
|
||
/// <param name="path">路径</param>
|
||
/// <param name="appKey">键值</param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取更新文件大小统计
|
||
/// </summary>
|
||
/// <param name="filePath">更新文件数据XML</param>
|
||
/// <returns>返回值</returns>
|
||
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;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 创建多层文件夹
|
||
/// </summary>
|
||
/// <param name="strDialogPath">弹出文件浏览窗口选中的符文件路径</param>
|
||
/// <param name="strPath">需要创建的多层文件夹路径</param>
|
||
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");
|
||
|
||
/// <summary>
|
||
/// 程序更新完后,要更新update.xml
|
||
/// </summary>
|
||
/// <param name="path">update.xml文件的路径</param>
|
||
/// <param name="appKey">"key"的值</param>
|
||
/// <param name="appValue">"value"的值</param>
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
}
|