AppLaunchTool.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }
  59. }