AppStartConfig.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Xml;
  6. namespace Microsoft.Samples.AppUpdater
  7. {
  8. public class AppStartConfig
  9. {
  10. public enum LaunchModes
  11. {
  12. AppDomain,
  13. Process
  14. }
  15. private string ConfigPath;
  16. private string _AppFolderName;
  17. private string _AppExeName;
  18. private string _SetupVersion;
  19. private LaunchModes _AppLaunchMode;
  20. public string AppFolderName
  21. {
  22. get
  23. {
  24. return _AppFolderName;
  25. }
  26. set
  27. {
  28. _AppFolderName = value;
  29. }
  30. }
  31. public string AppExeName
  32. {
  33. get
  34. {
  35. return _AppExeName;
  36. }
  37. set
  38. {
  39. _AppExeName = value;
  40. }
  41. }
  42. public string SetupVersion
  43. {
  44. get
  45. {
  46. return _SetupVersion;
  47. }
  48. set
  49. {
  50. _SetupVersion = value;
  51. }
  52. }
  53. public LaunchModes AppLaunchMode
  54. {
  55. get
  56. {
  57. return _AppLaunchMode;
  58. }
  59. set
  60. {
  61. _AppLaunchMode = value;
  62. }
  63. }
  64. public string AppExePath
  65. {
  66. get
  67. {
  68. return Path.Combine(Path.GetDirectoryName(ConfigPath), AppFolderName) + "\\" + AppExeName;
  69. }
  70. set
  71. {
  72. }
  73. }
  74. public string AppPath
  75. {
  76. get
  77. {
  78. return Path.Combine(Path.GetDirectoryName(ConfigPath), AppFolderName) + "\\";
  79. }
  80. set
  81. {
  82. }
  83. }
  84. public AppStartConfig(string filePath)
  85. {
  86. ConfigPath = filePath;
  87. }
  88. public static AppStartConfig Load(string filePath)
  89. {
  90. AppStartConfig appStartConfig = new AppStartConfig(filePath);
  91. try
  92. {
  93. XmlDocument xmlDocument = new XmlDocument();
  94. try
  95. {
  96. xmlDocument.Load(filePath);
  97. }
  98. catch (Exception innerException)
  99. {
  100. throw new ConfigFileMissingException("Config file '" + filePath + "' is missing.", innerException);
  101. }
  102. string text = "";
  103. try
  104. {
  105. XmlNode xmlNode = xmlDocument.SelectSingleNode("//AppRedirectionKey");
  106. text = xmlNode.InnerText;
  107. }
  108. catch (Exception)
  109. {
  110. text = "";
  111. }
  112. if (text != "")
  113. {
  114. return LoadRemoteConfigFile(text, filePath);
  115. }
  116. XmlNode xmlNode2 = xmlDocument.SelectSingleNode("//AppFolderName");
  117. appStartConfig.AppFolderName = xmlNode2.InnerText;
  118. XmlNode xmlNode3 = xmlDocument.SelectSingleNode("//AppExeName");
  119. appStartConfig.AppExeName = xmlNode3.InnerText;
  120. XmlNode xmlNode4 = xmlDocument.SelectSingleNode("//AppLaunchMode");
  121. if (xmlNode4 == null)
  122. {
  123. appStartConfig.AppLaunchMode = LaunchModes.Process;
  124. }
  125. else if (xmlNode4.InnerText.ToLower(new CultureInfo("en-US")) == "appdomain")
  126. {
  127. appStartConfig.AppLaunchMode = LaunchModes.AppDomain;
  128. }
  129. else
  130. {
  131. appStartConfig.AppLaunchMode = LaunchModes.Process;
  132. }
  133. XmlNode xmlNode5 = xmlDocument.SelectSingleNode("//SetupVersion");
  134. appStartConfig.SetupVersion = xmlNode5.InnerText;
  135. return appStartConfig;
  136. }
  137. catch (Exception ex2)
  138. {
  139. throw ex2;
  140. }
  141. }
  142. public void Udpate()
  143. {
  144. try
  145. {
  146. XmlDocument xmlDocument = new XmlDocument();
  147. XmlElement xmlElement = xmlDocument.CreateElement("Config");
  148. xmlDocument.AppendChild(xmlElement);
  149. XmlElement xmlElement2 = xmlDocument.CreateElement("AppFolderName");
  150. xmlElement2.InnerText = AppFolderName;
  151. xmlElement.AppendChild(xmlElement2);
  152. xmlElement2 = xmlDocument.CreateElement("AppExeName");
  153. xmlElement2.InnerText = AppExeName;
  154. xmlElement.AppendChild(xmlElement2);
  155. xmlElement2 = xmlDocument.CreateElement("SetupVersion");
  156. xmlElement2.InnerText = SetupVersion;
  157. xmlElement.AppendChild(xmlElement2);
  158. xmlElement2 = xmlDocument.CreateElement("AppLaunchMode");
  159. if (AppLaunchMode == LaunchModes.AppDomain)
  160. {
  161. xmlElement2.InnerText = "appdomain";
  162. }
  163. else
  164. {
  165. xmlElement2.InnerText = "process";
  166. }
  167. xmlElement.AppendChild(xmlElement2);
  168. xmlDocument.Save(ConfigPath);
  169. }
  170. catch (Exception ex)
  171. {
  172. throw ex;
  173. }
  174. }
  175. private static AppStartConfig LoadRemoteConfigFile(string key, string orgConfigPath)
  176. {
  177. string text = "";
  178. try
  179. {
  180. RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key);
  181. text = (string)registryKey.GetValue("InstallDir");
  182. text = Path.Combine(text, Path.GetFileName(orgConfigPath));
  183. return Load(text);
  184. }
  185. catch (Exception ex)
  186. {
  187. throw ex;
  188. }
  189. }
  190. }
  191. }