App.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* ==============================================================================
  2. * 功能描述:App
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/9/16 20:39:19
  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. using SAGA.DotNetUtils;
  16. namespace Test
  17. {
  18. /// <summary>
  19. /// App
  20. /// </summary>
  21. class App
  22. {
  23. [STAThread]
  24. static void Main(string[] args)
  25. {
  26. #if DEBUG
  27. MainWindow win = new MainWindow();
  28. Application myAp = new Application();
  29. myAp.ShutdownMode = ShutdownMode.OnExplicitShutdown;
  30. myAp.Run(win);
  31. #else
  32. string fullPath = Path.Combine(AppBaseInfo.AppRunPath, "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. }