HostConfig.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Configuration;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Windows.Forms;
  8. using Update.Util;
  9. namespace Update.Config
  10. {
  11. /// <summary>
  12. /// 目标配置
  13. /// </summary>
  14. public static class HostConfig
  15. {
  16. /// <summary>
  17. /// 默认目标文件名
  18. /// </summary>
  19. public const string DEFAULT_NAME = "OutputDll/OutputDll/SAGA.MBI.exe";
  20. private static string m_ExecutablePath;
  21. /// <summary>
  22. /// 获取目标文件路径 格式:D:\xx系统\wfy.exe
  23. /// </summary>
  24. public static string ExecutablePath
  25. {
  26. get
  27. {
  28. return m_ExecutablePath ?? (m_ExecutablePath = AppConfig.ExecutablePath);
  29. }
  30. }
  31. private static string m_ExecutableConfigPath;
  32. /// <summary>
  33. /// 获取目标配置文件路径 格式:D:\xx系统\wfy.exe.config
  34. /// </summary>
  35. public static string ExecutableConfigPath
  36. {
  37. get
  38. {
  39. return m_ExecutableConfigPath ?? (m_ExecutableConfigPath = ExecutablePath + ".config");
  40. }
  41. }
  42. private static string m_ExecutableDirectory;
  43. /// <summary>
  44. /// 获取目标文件的父文件夹 格式:D:\xx系统\
  45. /// </summary>
  46. public static string ExecutableDirectory
  47. {
  48. get
  49. {
  50. return m_ExecutableDirectory ?? (m_ExecutableDirectory = FilePathUtil.GetDirectoryName(ExecutablePath));
  51. }
  52. }
  53. private static string m_ExecutableName;
  54. /// <summary>
  55. /// 获取目标文件的短文件名 格式:wfy.exe
  56. /// </summary>
  57. public static string ExecutableName
  58. {
  59. get { return m_ExecutableName ?? (m_ExecutableName = Path.GetFileName(ExecutablePath)); }
  60. }
  61. private static Version m_CurrentVersion;
  62. /// <summary>
  63. /// 获取目标进程文件的文件版本
  64. /// </summary>
  65. public static Version CurrentVersion
  66. {
  67. get
  68. {
  69. if (m_CurrentVersion == null)
  70. {
  71. //FileInfo fileInfo=new FileInfo(Assembly.GetExecutingAssembly().Location);
  72. //var dirPath = fileInfo.DirectoryName;
  73. //Program Files文件夹, Copy 需要管理员权限,更改为临时目录
  74. var dirPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  75. string copyDestPath = Path.Combine(dirPath, ExecutableName);
  76. //由于加载的程序集无法卸载,所以将dll,Copy出来
  77. try
  78. {
  79. File.Copy(ExecutablePath, copyDestPath, true);
  80. }
  81. catch (Exception e)
  82. {
  83. //MessageBox.Show("Copy Fail"+e.StackTrace);
  84. }
  85. if (File.Exists(copyDestPath))
  86. m_CurrentVersion = Assembly.ReflectionOnlyLoadFrom(copyDestPath).GetName().Version;
  87. }
  88. return m_CurrentVersion;
  89. }
  90. }
  91. private static string m_UpdateUrl;
  92. /// <summary>
  93. /// 获取目标进程配置文件中的 UpdateUrl 配置项,获取后会自动处理末尾,保证返回时以 '/' 结尾(如果没配置将返回 null)
  94. /// </summary>
  95. public static string UpdateUrl
  96. {
  97. get
  98. {
  99. if (m_UpdateUrl == null)
  100. {
  101. string url = ReadHostConfig("UpdateUrl");
  102. if (string.IsNullOrWhiteSpace(url))
  103. return null;
  104. url = url.Trim();
  105. m_UpdateUrl = url.EndsWith("/") ? url : (url + "/");
  106. }
  107. return m_UpdateUrl;
  108. }
  109. }
  110. public static string BaseUrl = Const.URL;
  111. /// <summary>
  112. /// 初始化目标文件配置
  113. /// </summary>
  114. /// <param name="executablePath">目标文件路径</param>
  115. public static void Init(string executablePath)
  116. {
  117. m_ExecutablePath = executablePath;
  118. m_ExecutableConfigPath = null;
  119. m_ExecutableDirectory = null;
  120. m_ExecutableName = null;
  121. m_CurrentVersion = null;
  122. m_UpdateUrl = null;
  123. }
  124. /// <summary>
  125. /// 刷新目标程序版本
  126. /// </summary>
  127. public static void RefreshVersion()
  128. {
  129. m_CurrentVersion = null;
  130. }
  131. //读取配置
  132. private static string ReadHostConfig(string key)
  133. {
  134. try
  135. {
  136. //Configuration config = ConfigurationManager.OpenExeConfiguration(ExecutablePath);
  137. //KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
  138. //KeyValueConfigurationElement add = appSettings.Cast<KeyValueConfigurationElement>().FirstOrDefault(element => string.Equals(element.Key, key, StringComparison.OrdinalIgnoreCase));
  139. //return add == null ? null : add.Value;
  140. return "http://localhost:8888/AutoUpdate/";
  141. }
  142. catch
  143. {
  144. return null;
  145. }
  146. }
  147. }
  148. }