AppLaunchTool.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Y.Utils.DataUtils.Collections;
  7. using Y.Utils.DataUtils.StringUtils;
  8. using Y.Utils.IOUtils.PathUtils;
  9. using Y.Utils.WindowsUtils.ProcessUtils;
  10. namespace Y.Utils.AppUtils
  11. {
  12. public static class AppLaunchTool
  13. {
  14. public static bool Start(string appfile, string startfilename)
  15. {
  16. if (File.Exists(appfile))
  17. {
  18. //获取程序运行目录
  19. string exePath = DirTool.GetFilePath(appfile);
  20. if (Directory.Exists(exePath))
  21. {
  22. //获取运行目录下所有文件
  23. List<string> paths = DirTool.GetPath(exePath);
  24. if (ListTool.HasElements(paths))
  25. {
  26. //解析属于版本号的文件
  27. Version version = null;
  28. string startfile = null;
  29. foreach (var path in paths)
  30. {
  31. //只解析文件名带三个点的文件夹
  32. string filename = Path.GetFileName(path);
  33. if (StringTool.SubStringCount(filename, ".") == 3)
  34. {
  35. try
  36. {
  37. Version tempVersion = new Version(filename);
  38. string tempFile = DirTool.Combine(path, startfilename);
  39. if ((version == null || tempVersion > version) && File.Exists(tempFile))
  40. {
  41. version = tempVersion;
  42. startfile = tempFile;
  43. }
  44. }
  45. catch { }
  46. }
  47. }
  48. //准备启动
  49. if (startfile != null)
  50. {
  51. return ProcessTool.Start(startfile);
  52. }
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. public static bool StartNewVersion(string appPath, string startfilename)
  59. {
  60. if (Directory.Exists(appPath))
  61. {
  62. //获取运行目录下所有文件
  63. List<string> paths = DirTool.GetPath(appPath);
  64. if (ListTool.HasElements(paths))
  65. {
  66. //解析属于版本号的文件
  67. Version version = null;
  68. string startfile = null;
  69. foreach (var path in paths)
  70. {
  71. //只解析文件名带三个点的文件夹
  72. string filename = Path.GetFileName(path);
  73. if (StringTool.SubStringCount(filename, ".") == 3)
  74. {
  75. try
  76. {
  77. Version tempVersion = new Version(filename);
  78. string tempFile = DirTool.Combine(path, startfilename);
  79. if ((version == null || tempVersion > version) && File.Exists(tempFile))
  80. {
  81. version = tempVersion;
  82. startfile = tempFile;
  83. }
  84. }
  85. catch { }
  86. }
  87. }
  88. //准备启动
  89. if (startfile != null)
  90. {
  91. return ProcessTool.Start(startfile);
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. }
  98. }