App.xaml.cs 3.3 KB

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