/// <summary>
/// 获取文件列表并下载
/// </summary>
private static void UpdateList()
/// <summary> /// 获取文件列表并下载 /// </summary> private static void UpdateList() { string xmlPath = dirPath + "AutoUpdater/AutoUpdater.xml"; WebClient wc = new WebClient(); DataSet ds = new DataSet(); ds.Locale = CultureInfo.InvariantCulture; try { Stream sm = wc.OpenRead(xmlPath); ds.ReadXml(sm); DataTable dt = ds.Tables["UpdateFileList"]; StringBuilder sb = new StringBuilder(); count = dt.Rows.Count; for (int i = 0; i < dt.Rows.Count; i++) { if (i == 0) { sb.Append(dt.Rows[i]["UpdateFile"].ToString()); } else { sb.Append("," + dt.Rows[i]["UpdateFile"].ToString()); } } fileNames = sb.ToString().Split(','); sm.Close(); } catch (WebException ex) { MeBox(ex.Message); } }
/// <summary> /// 下载文件 /// </summary> /// <param name="arry">下载序号</param> private void DownloadFile(int arry) { try { num++; fileName = fileNames[arry]; this.label1.Text = String.Format( CultureInfo.InvariantCulture, "更新进度 {0}/{1} [ {2} ]", num, count, ConvertSize(size));
this.progressBar2.Value = 0; this.downWebClient.DownloadFileAsync( new Uri(dirPath + "AutoUpdater/" + fileName), Application.StartupPath + "\\AutoUpdater\\" + fileName); } catch (WebException ex) { MeBox(ex.Message); } }
/// <summary> /// 转换字节大小 /// </summary> /// <param name="byteSize">输入字节数</param> /// <returns>返回值</returns> private 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="txt">输入提示信息</param> private static void MeBox(string txt) { MessageBox.Show( txt, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); }
/// <summary> /// 关闭程序 /// </summary> private static void UpdaterClose() { try { System.Diagnostics.Process.Start(Application.StartupPath + "\\ComCir.exe"); } catch (Win32Exception ex) { MeBox(ex.Message); } Application.Exit(); }
/// <summary> /// 读取.exe.config的值 /// </summary> /// <param name="path">.exe.config文件的路径</param> /// <param name="appKey">"key"的值</param> /// <returns>返回"value"的值</returns> internal static string GetConfigValue(string path, string appKey) { 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) { MeBox(ex.Message); } if (xElem != null) return xElem.GetAttribute("value"); else return ""; }
/// <summary> /// 设置.exe.config的值 /// </summary> /// <param name="path">.exe.config文件的路径</param> /// <param name="appKey">"key"的值</param> /// <param name="appValue">"value"的值</param> internal static void SetConfigValue(string path, string appKey, string appValue) { XmlDocument xDoc = new XmlDocument(); try { xDoc.Load(path);
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(Application.StartupPath + "\\" + path); } catch (XmlException ex) { MeBox(ex.Message); } }
/// <summary> /// 判断软件的更新日期 /// </summary> /// <param name="Dir">服务器地址</param> /// <returns>返回日期</returns> private static string GetTheLastUpdateTime(string Dir) { string LastUpdateTime = ""; string AutoUpdaterFileName = Dir + "AutoUpdater/AutoUpdater.xml"; 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) { MeBox(ex.Message); } return LastUpdateTime; } } }
|
{
string xmlPath = dirPath + "AutoUpdater/AutoUpdater.xml";
WebClient wc = new WebClient();
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
try
{
Stream sm = wc.OpenRead(xmlPath);
ds.ReadXml(sm);
DataTable dt = ds.Tables["UpdateFileList"];
StringBuilder sb = new StringBuilder();
count = dt.Rows.Count;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i == 0)
{
sb.Append(dt.Rows[i]["UpdateFile"].ToString());
}
else
{
sb.Append("," + dt.Rows[i]["UpdateFile"].ToString());
}
}
fileNames = sb.ToString().Split(',');
sm.Close();
}
catch (WebException ex)
{
MeBox(ex.Message);
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="arry">下载序号</param>
private void DownloadFile(int arry)
{
try
{
num++;
fileName = fileNames[arry];
this.label1.Text = String.Format(
CultureInfo.InvariantCulture,
"更新进度 {0}/{1} [ {2} ]",
num,
count,
ConvertSize(size));
this.progressBar2.Value = 0;
this.downWebClient.DownloadFileAsync(
new Uri(dirPath + "AutoUpdater/" + fileName),
Application.StartupPath + "\\AutoUpdater\\" + fileName);
}
catch (WebException ex)
{
MeBox(ex.Message);
}
}
/// <summary>
/// 转换字节大小
/// </summary>
/// <param name="byteSize">输入字节数</param>
/// <returns>返回值</returns>
private 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="txt">输入提示信息</param>
private static void MeBox(string txt)
{
MessageBox.Show(
txt,
"提示信息",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
}
/// <summary>
/// 关闭程序
/// </summary>
private static void UpdaterClose()
{
try
{
System.Diagnostics.Process.Start(Application.StartupPath + "\\ComCir.exe");
}
catch (Win32Exception ex)
{
MeBox(ex.Message);
}
Application.Exit();
}
/// <summary>
/// 读取.exe.config的值
/// </summary>
/// <param name="path">.exe.config文件的路径</param>
/// <param name="appKey">"key"的值</param>
/// <returns>返回"value"的值</returns>
internal static string GetConfigValue(string path, string appKey)
{
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)
{
MeBox(ex.Message);
}
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
/// <summary>
/// 设置.exe.config的值
/// </summary>
/// <param name="path">.exe.config文件的路径</param>
/// <param name="appKey">"key"的值</param>
/// <param name="appValue">"value"的值</param>
internal static void SetConfigValue(string path, string appKey, string appValue)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(path);
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(Application.StartupPath + "\\" + path);
}
catch (XmlException ex)
{
MeBox(ex.Message);
}
}
/// <summary>
/// 判断软件的更新日期
/// </summary>
/// <param name="Dir">服务器地址</param>
/// <returns>返回日期</returns>
private static string GetTheLastUpdateTime(string Dir)
{
string LastUpdateTime = "";
string AutoUpdaterFileName = Dir + "AutoUpdater/AutoUpdater.xml";
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)
{
MeBox(ex.Message);
}
return LastUpdateTime;
}
}
}