using InABox.Core; using PRSServices; using Syncfusion.Licensing; using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Security.Principal; using System.ServiceProcess; using System.Windows; namespace PRSServer; /// /// 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) { MessageBox.Show("This program must be run as an administrator! \n\n" + ex); } return AdminStartup.Cannot; } return AdminStartup.Already; } private 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) { base.OnStartup(e); SyncfusionLicenseProvider.RegisterLicense(CoreUtils.SyncfusionLicense(SyncfusionVersion.v23_2)); if (Environment.UserInteractive) { var args = Environment.GetCommandLineArgs(); if (args.Length <= 1) { if (CheckAdmin() != AdminStartup.Cannot) { var f = new Configuration(); f.ShowDialog(); Shutdown(0); } else { MessageBox.Show("This program must be run as Admin!"); Shutdown(0); } } else if (args[1].StartsWith("/service=")) { var c = new ServerConsole(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 PRSServerService(serviceName)); Shutdown(0); } } // -1 failed // 0 already admin // 1 launched admin private enum AdminStartup { Already, Done, Cannot } }