App.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* ==============================================================================
  2. * 功能描述:App
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/11/8 9:23:25
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. namespace LRH
  16. {
  17. /// <summary>
  18. /// App
  19. /// </summary>
  20. public class App
  21. {
  22. [STAThread]
  23. static void Main(string[] args)
  24. {
  25. #if DEBUG
  26. MainWindow win = new MainWindow();
  27. Application myAp = new Application();
  28. myAp.ShutdownMode = ShutdownMode.OnExplicitShutdown;
  29. myAp.Run(win);
  30. #else
  31. var runPath=Directory.GetParent(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)).ToString();
  32. string fullPath = Path.Combine(runPath, "Updater", "Update.exe");
  33. //有参数时直接启动
  34. if (args != null && args.Length > 0 || !File.Exists(fullPath))
  35. {
  36. MainWindow win = new MainWindow();
  37. Application myAp = new Application();
  38. myAp.ShutdownMode = ShutdownMode.OnExplicitShutdown;
  39. myAp.Run(win);
  40. }
  41. else
  42. {
  43. try
  44. {
  45. //无参数可看做双击启动,启动前检测版本
  46. //arguments参数用空格分开,"Program Files"使用时被拆分成两个参数,使用时请注意
  47. //设置启动动作,确保以管理员身份运行
  48. ProcessStartInfo startInfo = new ProcessStartInfo(fullPath)
  49. {
  50. UseShellExecute = true,
  51. Verb = "runas",
  52. WindowStyle = ProcessWindowStyle.Normal,
  53. Arguments = Assembly.GetExecutingAssembly().Location,
  54. CreateNoWindow = true
  55. };
  56. Process.Start(startInfo);
  57. }
  58. catch (Exception e)
  59. {
  60. MessageBox.Show(e.StackTrace);
  61. }
  62. }
  63. #endif
  64. }
  65. }
  66. }