App.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using InABox.Core;
  2. using Syncfusion.Licensing;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Security.Principal;
  9. using System.ServiceProcess;
  10. using System.Windows;
  11. namespace PRSServer
  12. {
  13. /// <summary>
  14. /// Interaction logic for App.xaml
  15. /// </summary>
  16. public partial class App : Application
  17. {
  18. private AdminStartup CheckAdmin()
  19. {
  20. if (!IsRunAsAdmin())
  21. {
  22. var proc = new ProcessStartInfo();
  23. proc.UseShellExecute = true;
  24. proc.WorkingDirectory = Environment.CurrentDirectory;
  25. proc.FileName =
  26. Path.ChangeExtension(Assembly.GetEntryAssembly().Location, "exe"); // Required to change to exe because .NET produces a .dll
  27. proc.Verb = "runas";
  28. try
  29. {
  30. Process.Start(proc);
  31. Shutdown(0);
  32. return AdminStartup.Done;
  33. }
  34. catch (Exception ex)
  35. {
  36. MessageBox.Show("This program must be run as an administrator! \n\n" + ex);
  37. }
  38. return AdminStartup.Cannot;
  39. }
  40. return AdminStartup.Already;
  41. }
  42. private bool IsRunAsAdmin()
  43. {
  44. try
  45. {
  46. var id = WindowsIdentity.GetCurrent();
  47. var principal = new WindowsPrincipal(id);
  48. return principal.IsInRole(WindowsBuiltInRole.Administrator);
  49. }
  50. catch (Exception)
  51. {
  52. return false;
  53. }
  54. }
  55. protected override void OnStartup(StartupEventArgs e)
  56. {
  57. base.OnStartup(e);
  58. SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v23_2));
  59. if (Environment.UserInteractive)
  60. {
  61. var args = Environment.GetCommandLineArgs();
  62. if (args.Length <= 1)
  63. {
  64. if (CheckAdmin() != AdminStartup.Cannot)
  65. {
  66. var f = new Configuration();
  67. f.ShowDialog();
  68. Shutdown(0);
  69. }
  70. else
  71. {
  72. MessageBox.Show("This program must be run as Admin!");
  73. Shutdown(0);
  74. }
  75. }
  76. else if (args[1].StartsWith("/service="))
  77. {
  78. var c = new ServerConsole(args[1].Split('=').Last(), args[1].Split('=').Last(), false);
  79. c.ShowDialog();
  80. Shutdown(0);
  81. }
  82. else
  83. {
  84. Shutdown(0);
  85. }
  86. }
  87. else
  88. {
  89. var args = Environment.GetCommandLineArgs();
  90. var serviceName = "";
  91. if (args.Length > 1 && args[1].StartsWith("/service=")) serviceName = args[1].Split('=').Last();
  92. ServiceBase.Run(new PRSService(serviceName, this));
  93. Shutdown(0);
  94. }
  95. }
  96. // -1 failed
  97. // 0 already admin
  98. // 1 launched admin
  99. private enum AdminStartup
  100. {
  101. Already,
  102. Done,
  103. Cannot
  104. }
  105. }
  106. }