App.xaml.cs 3.1 KB

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