AppLaunchTool.cs 4.9 KB

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