LocalInfo.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Web.Hosting;
  7. using System.Xml;
  8. namespace MAutoUpdate
  9. {
  10. public class LocalInfo
  11. {
  12. /// <summary>
  13. /// 本地版本号
  14. /// </summary>
  15. public string LocalVersion { get; set; }
  16. /// <summary>
  17. /// 最后更新时间
  18. /// </summary>
  19. public string LastUdpate { get; set; }
  20. /// <summary>
  21. /// server.xml文件地址
  22. /// </summary>
  23. public string ServerUpdateUrl { get; set; }
  24. public string LocalIgnoreVersion { get; set; }
  25. private string url = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Local.xml");
  26. //private string url = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "Local.xml");
  27. public LocalInfo(string localAddress)
  28. {
  29. url = Path.Combine(localAddress, "Local.xml");
  30. }
  31. public void SaveReg(string subKey)
  32. {
  33. RegistryKey Key;
  34. Key = Registry.CurrentUser;
  35. //Key = Key.OpenSubKey("SOFTWARE\\GoodMES\\Update");
  36. Key = Key.OpenSubKey(subKey, true);
  37. foreach (var item in this.GetType().GetProperties())
  38. {
  39. Key.SetValue(item.Name.ToString(), this.GetType().GetProperty(item.Name.ToString()).GetValue(this, null).ToString());
  40. }
  41. }
  42. public void LoadReg(string subKey)
  43. {
  44. //获取本地配置文件
  45. RegistryKey Key;
  46. Key = Registry.CurrentUser;
  47. Key = Key.OpenSubKey(subKey);
  48. foreach (var item in this.GetType().GetProperties())
  49. {
  50. this.GetType().GetProperty(item.Name.ToString()).SetValue(this, Key.GetValue(item.Name.ToString()).ToString(), null);
  51. }
  52. Key.Close();
  53. }
  54. public void LoadXml(int _type)
  55. {
  56. XmlDocument xdoc = new XmlDocument();
  57. xdoc.Load(url);
  58. var root = xdoc.DocumentElement;
  59. var listNodes = root.SelectNodes("/LocalUpdate");
  60. foreach (XmlNode item in listNodes)
  61. {
  62. RemoteInfo remote = new RemoteInfo();
  63. foreach (XmlNode pItem in item.ChildNodes)
  64. {
  65. if (pItem.Name == "LocalVersion" && _type == 1)
  66. {
  67. GetType().GetProperty(pItem.Name).SetValue(this, "1.0.0.1", null);
  68. }
  69. else
  70. GetType().GetProperty(pItem.Name).SetValue(this, pItem.InnerText, null);
  71. }
  72. }
  73. }
  74. public void SaveXml()
  75. {
  76. XmlDocument xdoc = new XmlDocument();
  77. xdoc.Load(url);
  78. var root = xdoc.DocumentElement;
  79. var listNodes = root.SelectNodes("/LocalUpdate");
  80. foreach (XmlNode item in listNodes)
  81. {
  82. foreach (XmlNode pItem in item.ChildNodes)
  83. {
  84. // Key.SetValue(item.Name.ToString(), this.GetType().GetProperty(item.Name.ToString()).GetValue(this, null).ToString());
  85. pItem.InnerText = this.GetType().GetProperty(pItem.Name.ToString()).GetValue(this, null).ToString();
  86. }
  87. }
  88. xdoc.Save(url);
  89. }
  90. }
  91. }