AppLaunchTool.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2017.10.12 - 2017.10.12
  4. // desc: 客户端启动器
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.DataUtils.CollectionUtils;
  8. using Azylee.Core.DataUtils.StringUtils;
  9. using Azylee.Core.IOUtils.DirUtils;
  10. using Azylee.Core.IOUtils.FileUtils;
  11. using Azylee.Core.ProcessUtils;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. namespace Azylee.Core.AppUtils
  16. {
  17. public static class AppLaunchTool
  18. {
  19. /// <summary>
  20. /// 启动最新版本程序
  21. /// </summary>
  22. /// <param name="route">路径:程序版本文件夹路径</param>
  23. /// <param name="startfilename">可执行文件名</param>
  24. /// <returns></returns>
  25. public static bool StartNewVersion(string route, string startfilename)
  26. {
  27. //判断路径是文件还是文件夹,并统一处理为文件夹
  28. string appPath = route;
  29. if (FileTool.IsFile(route))
  30. appPath = DirTool.GetFilePath(route);
  31. if (Directory.Exists(appPath))
  32. {
  33. //获取运行目录下所有文件
  34. List<string> paths = DirTool.GetPath(appPath);
  35. if (ListTool.HasElements(paths))
  36. {
  37. //解析属于版本号的文件
  38. Version version = null;
  39. string startfile = null;
  40. foreach (var path in paths)
  41. {
  42. //只解析文件名带三个点的文件夹
  43. string filename = Path.GetFileName(path);
  44. if (StringTool.SubStringCount(filename, ".") == 3)
  45. {
  46. try
  47. {
  48. Version tempVersion = new Version(filename);
  49. string tempFile = DirTool.Combine(path, startfilename);
  50. if ((version == null || tempVersion > version) && File.Exists(tempFile))
  51. {
  52. version = tempVersion;
  53. startfile = tempFile;
  54. }
  55. }
  56. catch { }
  57. }
  58. }
  59. //准备启动
  60. if (startfile != null)
  61. {
  62. return ProcessTool.Start(startfile);
  63. }
  64. }
  65. }
  66. return false;
  67. }
  68. /// <summary>
  69. /// 查询是否有最新版本程序可以执行
  70. /// </summary>
  71. /// <param name="route">路径:程序版本文件夹路径</param>
  72. /// <param name="startfilename">可执行文件名</param>
  73. /// <returns></returns>
  74. public static bool HasNewVersion(string route, string startfilename)
  75. {
  76. //判断路径是文件还是文件夹,并统一处理为文件夹
  77. string appPath = route;
  78. if (FileTool.IsFile(route))
  79. appPath = DirTool.GetFilePath(route);
  80. if (Directory.Exists(appPath))
  81. {
  82. //获取运行目录下所有文件
  83. List<string> paths = DirTool.GetPath(appPath);
  84. if (ListTool.HasElements(paths))
  85. {
  86. //解析属于版本号的文件
  87. foreach (var path in paths)
  88. {
  89. //只解析文件名带三个点的文件夹
  90. string filename = Path.GetFileName(path);
  91. if (StringTool.SubStringCount(filename, ".") == 3)
  92. {
  93. try
  94. {
  95. //有版本命名的文件,且文件中有exe程序
  96. Version tempVersion = new Version(filename);
  97. string tempFile = DirTool.Combine(path, startfilename);
  98. if (File.Exists(tempFile)) return true;
  99. }
  100. catch { }
  101. }
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. /// <summary>
  108. /// 获取最新版本号
  109. /// </summary>
  110. /// <param name="route">路径:程序版本文件夹路径</param>
  111. /// <param name="startfilename">可执行文件名</param>
  112. /// <returns></returns>
  113. public static Version GetNewVersion(string route, string startfilename)
  114. {
  115. //解析属于版本号的文件
  116. Version version = null;
  117. string startfile = null;
  118. //判断路径是文件还是文件夹,并统一处理为文件夹
  119. string appPath = route;
  120. if (FileTool.IsFile(route))
  121. appPath = DirTool.GetFilePath(route);
  122. if (Directory.Exists(appPath))
  123. {
  124. //获取运行目录下所有文件
  125. List<string> paths = DirTool.GetPath(appPath);
  126. if (ListTool.HasElements(paths))
  127. {
  128. foreach (var path in paths)
  129. {
  130. //只解析文件名带三个点的文件夹
  131. string filename = Path.GetFileName(path);
  132. if (StringTool.SubStringCount(filename, ".") == 3)
  133. {
  134. try
  135. {
  136. Version tempVersion = new Version(filename);
  137. string tempFile = DirTool.Combine(path, startfilename);
  138. if ((version == null || tempVersion > version) && File.Exists(tempFile))
  139. {
  140. version = tempVersion;
  141. startfile = tempFile;
  142. }
  143. }
  144. catch { }
  145. }
  146. }
  147. }
  148. }
  149. return version;
  150. }
  151. }
  152. }