HostConfig.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. private static string m_DownLoadURL;
  17. public static string DownLoadURL
  18. {
  19. get
  20. {
  21. if(m_DownLoadURL==null)
  22. m_DownLoadURL= HttpUtils.GetDownloadUrl(Const.Key);
  23. return m_DownLoadURL;
  24. }
  25. }
  26. private static string m_ExecutablePath;
  27. /// <summary>
  28. /// 获取目标文件路径 格式:D:\xx系统\wfy.exe
  29. /// </summary>
  30. public static string ExecutablePath
  31. {
  32. get { return m_ExecutablePath; }
  33. }
  34. private static string m_ExecutableDirectory;
  35. /// <summary>
  36. /// 获取目标文件的父文件夹 格式:D:\xx系统\
  37. /// </summary>
  38. public static string ExecutableDirectory
  39. {
  40. get
  41. {
  42. return m_ExecutableDirectory ?? (m_ExecutableDirectory = FilePathUtil.GetDirectoryName(ExecutablePath));
  43. }
  44. }
  45. private static string m_ExecutableName;
  46. /// <summary>
  47. /// 获取目标文件的短文件名 格式:wfy.exe
  48. /// </summary>
  49. public static string ExecutableName
  50. {
  51. get { return m_ExecutableName ?? (m_ExecutableName = Path.GetFileName(ExecutablePath)); }
  52. }
  53. private static Version m_CurrentVersion;
  54. /// <summary>
  55. /// 获取目标进程文件的文件版本
  56. /// </summary>
  57. public static Version CurrentVersion
  58. {
  59. get
  60. {
  61. if (m_CurrentVersion == null)
  62. {
  63. //FileInfo fileInfo=new FileInfo(Assembly.GetExecutingAssembly().Location);
  64. //var dirPath = fileInfo.DirectoryName;
  65. //Program Files文件夹, Copy 需要管理员权限,更改为临时目录
  66. var dirPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  67. string copyDestPath = Path.Combine(dirPath, ExecutableName);
  68. //由于加载的程序集无法卸载,所以将dll,Copy出来
  69. try
  70. {
  71. File.Copy(ExecutablePath, copyDestPath, true);
  72. }
  73. catch (Exception e)
  74. {
  75. //MessageBox.Show("Copy Fail"+e.StackTrace);
  76. }
  77. if (File.Exists(copyDestPath))
  78. m_CurrentVersion = Assembly.ReflectionOnlyLoadFrom(copyDestPath).GetName().Version;
  79. }
  80. return m_CurrentVersion;
  81. }
  82. }
  83. /// <summary>
  84. /// 初始化目标文件配置
  85. /// </summary>
  86. /// <param name="executablePath">目标文件路径</param>
  87. public static void Init(string executablePath)
  88. {
  89. m_ExecutablePath = executablePath;
  90. m_ExecutableDirectory = null;
  91. m_ExecutableName = null;
  92. m_CurrentVersion = null;
  93. }
  94. /// <summary>
  95. /// 刷新目标程序版本
  96. /// </summary>
  97. public static void RefreshVersion()
  98. {
  99. m_CurrentVersion = null;
  100. }
  101. }
  102. }