using InABox.Configuration; using InABox.Core; using InABox.Wpf; using PRSServices; using Syncfusion.Licensing; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Security.Principal; using System.ServiceProcess; using System.Threading.Tasks; using System.Windows; namespace PRSLicensing; /// /// Interaction logic for App.xaml /// public partial class App : Application { static App() { PRSServiceInstaller.Assembly = typeof(App).Assembly; } private AdminStartup CheckAdmin() { if (!IsRunAsAdmin()) { var proc = new ProcessStartInfo(); proc.UseShellExecute = true; proc.WorkingDirectory = Environment.CurrentDirectory; proc.FileName = Path.ChangeExtension(Assembly.GetEntryAssembly().Location, "exe"); // Required to change to exe because .NET produces a .dll proc.Verb = "runas"; try { Process.Start(proc); Shutdown(0); return AdminStartup.Done; } catch (Exception ex) { MessageWindow.ShowMessage("This program must be run as an administrator! \n\n" + ex, "Administrator required"); } return AdminStartup.Cannot; } return AdminStartup.Already; } private static bool IsRunAsAdmin() { try { var id = WindowsIdentity.GetCurrent(); var principal = new WindowsPrincipal(id); return principal.IsInRole(WindowsBuiltInRole.Administrator); } catch (Exception) { return false; } } protected override void OnStartup(StartupEventArgs e) { SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v29_x)); if (Environment.UserInteractive) { var args = Environment.GetCommandLineArgs(); if (args.Length <= 1) { if (CheckAdmin() != AdminStartup.Cannot) { var f = new Console(); f.ShowDialog(); Shutdown(0); } else { MessageWindow.ShowMessage("This program must be run as Admin!", "Administrator required"); Shutdown(0); } } else if (args[1].StartsWith("/service=")) { var c = new LicensingConsole(args[1].Split('=').Last(), args[1].Split('=').Last(), false); c.ShowDialog(); Shutdown(0); } else { Shutdown(0); } } else { var args = Environment.GetCommandLineArgs(); var serviceName = ""; if (args.Length > 1 && args[1].StartsWith("/service=")) serviceName = args[1].Split('=').Last(); ServiceBase.Run(new PRSLicensingService(serviceName)); Shutdown(0); } } private enum AdminStartup { Already, Done, Cannot } }