AppUpdater.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Web;
  3. using System.IO;
  4. using System.Net;
  5. using System.Xml;
  6. using System.Collections;
  7. using System.Windows.Forms;
  8. using System.ComponentModel;
  9. namespace AutoUpdate
  10. {
  11. /// <summary>
  12. /// updater 的摘要说明。
  13. /// </summary>
  14. public class AppUpdater:IDisposable
  15. {
  16. #region 成员与字段属性
  17. private string _updaterUrl;
  18. private string _updaterUrl1;
  19. private bool disposed = false;
  20. private IntPtr handle;
  21. private Component component = new Component();
  22. [System.Runtime.InteropServices.DllImport("Kernel32")]
  23. private extern static Boolean CloseHandle(IntPtr handle);
  24. public string UpdaterUrl
  25. {
  26. set{_updaterUrl = value;}
  27. get{return this._updaterUrl;}
  28. }
  29. public string UpdaterUrl1
  30. {
  31. set { _updaterUrl1 = value; }
  32. get { return this._updaterUrl1; }
  33. }
  34. #endregion
  35. /// <summary>
  36. /// AppUpdater构造函数
  37. /// </summary>
  38. public void Dispose()
  39. {
  40. Dispose(true);
  41. GC.SuppressFinalize(this);
  42. }
  43. private void Dispose(bool disposing)
  44. {
  45. if(!this.disposed)
  46. {
  47. if(disposing)
  48. {
  49. component.Dispose();
  50. }
  51. CloseHandle(handle);
  52. handle = IntPtr.Zero;
  53. }
  54. disposed = true;
  55. }
  56. ~AppUpdater()
  57. {
  58. Dispose(false);
  59. }
  60. /// <summary>
  61. /// 检查更新文件
  62. /// </summary>
  63. /// <param name="serverXmlFile"></param>
  64. /// <param name="localXmlFile"></param>
  65. /// <param name="updateFileList"></param>
  66. /// <returns></returns>
  67. public int CheckForUpdate(string serverXmlFile,string localXmlFile,out Hashtable updateFileList)
  68. {
  69. updateFileList = new Hashtable();
  70. if(!File.Exists(localXmlFile) || !File.Exists(serverXmlFile))
  71. {
  72. return -1;
  73. }
  74. XmlFiles serverXmlFiles = new XmlFiles(serverXmlFile);
  75. XmlFiles localXmlFiles = new XmlFiles(localXmlFile);
  76. XmlNodeList newNodeList = serverXmlFiles.GetNodeList("AutoUpdater/Files");
  77. XmlNodeList oldNodeList = localXmlFiles.GetNodeList("AutoUpdater/Files");//5+1+a+s+p+x
  78. int k = 0;
  79. for(int i = 0;i < newNodeList.Count;i++)
  80. {
  81. string [] fileList = new string[4];
  82. string newFileName = newNodeList.Item(i).Attributes["Name"].Value.Trim();
  83. string newVer = newNodeList.Item(i).Attributes["Ver"].Value.Trim();
  84. string newUpdateTxt = newNodeList.Item(i).Attributes["UpdateTxt"].Value.Trim();
  85. ArrayList oldFileAl = new ArrayList();
  86. for(int j = 0;j < oldNodeList.Count;j++)
  87. {
  88. string oldFileName = oldNodeList.Item(j).Attributes["Name"].Value.Trim();
  89. string oldVer = oldNodeList.Item(j).Attributes["Ver"].Value.Trim();
  90. string oldUpdateTxt = oldNodeList.Item(j).Attributes["UpdateTxt"].Value.Trim();
  91. oldFileAl.Add(oldFileName);
  92. oldFileAl.Add(oldVer);
  93. }
  94. int pos = oldFileAl.IndexOf(newFileName);
  95. if(pos == -1)
  96. {
  97. fileList[0] = newFileName;
  98. fileList[1] = newVer;
  99. fileList[3] = newUpdateTxt;
  100. updateFileList.Add(k,fileList);
  101. k++;
  102. }
  103. else if(pos > -1 && newVer.CompareTo(oldFileAl[pos+1].ToString())>0 )
  104. {
  105. fileList[0] = newFileName;
  106. fileList[1] = newVer;
  107. fileList[3] = newUpdateTxt;
  108. updateFileList.Add(k,fileList);
  109. k++;
  110. }
  111. }
  112. return k;
  113. }
  114. /// <summary>
  115. /// 检查更新文件
  116. /// </summary>
  117. /// <param name="serverXmlFile"></param>
  118. /// <param name="localXmlFile"></param>
  119. /// <param name="updateFileList"></param>
  120. /// <returns></returns>
  121. public int CheckForUpdate()
  122. {
  123. string localXmlFile = Application.StartupPath + "\\UpdateList.xml";
  124. if(!File.Exists(localXmlFile))
  125. {
  126. return -1;
  127. }
  128. XmlFiles updaterXmlFiles = new XmlFiles(localXmlFile );
  129. string tempUpdatePath = Environment.GetEnvironmentVariable("Temp") + "\\"+ "_"+ updaterXmlFiles.FindNode("//Application").Attributes["applicationId"].Value+"_"+"y"+"_"+"x"+"_"+"m"+"_"+"\\";
  130. this.UpdaterUrl = updaterXmlFiles.GetNodeValue("//Url") + "/UpdateList.xml";
  131. this.DownAutoUpdateFile(tempUpdatePath);
  132. string serverXmlFile = tempUpdatePath +"\\UpdateList.xml";
  133. if(!File.Exists(serverXmlFile))
  134. {
  135. return -1;
  136. }
  137. XmlFiles serverXmlFiles = new XmlFiles(serverXmlFile);
  138. XmlFiles localXmlFiles = new XmlFiles(localXmlFile);
  139. XmlNodeList newNodeList = serverXmlFiles.GetNodeList("AutoUpdater/Files");
  140. XmlNodeList oldNodeList = localXmlFiles.GetNodeList("AutoUpdater/Files");
  141. int k = 0;
  142. for(int i = 0;i < newNodeList.Count;i++)
  143. {
  144. string [] fileList = new string[3];
  145. string newFileName = newNodeList.Item(i).Attributes["Name"].Value.Trim();
  146. string newVer = newNodeList.Item(i).Attributes["Ver"].Value.Trim();
  147. ArrayList oldFileAl = new ArrayList();
  148. for(int j = 0;j < oldNodeList.Count;j++)
  149. {
  150. string oldFileName = oldNodeList.Item(j).Attributes["Name"].Value.Trim();
  151. string oldVer = oldNodeList.Item(j).Attributes["Ver"].Value.Trim();
  152. oldFileAl.Add(oldFileName);
  153. oldFileAl.Add(oldVer);
  154. }
  155. int pos = oldFileAl.IndexOf(newFileName);
  156. if(pos == -1)
  157. {
  158. fileList[0] = newFileName;
  159. fileList[1] = newVer;
  160. k++;
  161. }
  162. else if(pos > -1 && newVer.CompareTo(oldFileAl[pos+1].ToString())>0 )
  163. {
  164. fileList[0] = newFileName;
  165. fileList[1] = newVer;
  166. k++;
  167. }
  168. }
  169. return k;
  170. }
  171. /// <summary>
  172. /// 返回下载更新文件的临时目录
  173. /// </summary>
  174. /// <returns></returns>
  175. public void DownAutoUpdateFile(string downpath)
  176. {
  177. if(!System.IO.Directory.Exists(downpath))
  178. System.IO.Directory.CreateDirectory(downpath);
  179. string serverXmlFile = downpath+@"/UpdateList.xml";
  180. string serverXmlFile1 = downpath + @"/Update.xml";
  181. try
  182. {
  183. WebRequest req = WebRequest.Create(this.UpdaterUrl);
  184. WebResponse res = req.GetResponse();
  185. if(res.ContentLength >0)
  186. {
  187. try
  188. {
  189. WebClient wClient = new WebClient();
  190. wClient.DownloadFile(this.UpdaterUrl,serverXmlFile);
  191. wClient.DownloadFile(this.UpdaterUrl1, serverXmlFile1);
  192. }
  193. catch
  194. {
  195. return;
  196. }
  197. }
  198. }
  199. catch
  200. {
  201. return;
  202. }
  203. //return tempPath;
  204. }
  205. }
  206. }