123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- 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;
- /// <summary>
- /// Interaction logic for App.xaml
- /// </summary>
- 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
- }
- }
|