/// <summary> /// Try to launch the program as admin, return result /// </summary> private void LaunchCurrentAppAsAdmin() { ProcessStartInfo info = new ProcessStartInfo { FileName = PermissionChecker.GetExe(), UseShellExecute = true, CreateNoWindow = true }; //Required by UAC ;( info.UseShellExecute = true; // Provides Run as Administrator info.Verb = "runas"; // Custom argument so we know we manually elevated the process info.Arguments = "/elevated"; try { Process process = new Process { StartInfo = info }; process.Start(); } catch (Exception e) { Debug.WriteLine(e.Message); } }
protected override void OnStartup(StartupEventArgs e) { #if DEBUGELEVATED if (IsElevated()) { if (Debugger.IsAttached) { Debugger.Break(); } else { Debugger.Launch(); } } #endif if (OsInformation.GetWindowsReleaseVersion() < 1703) { // Version too low to use Bluetooth, skip check. return; } string exeName = Path.GetFileName(PermissionChecker.GetExe()); Guid guid = Guid.NewGuid(); Debug.WriteLine(@"Is Admin: " + UserIsAdmin()); var result = PermissionChecker.EnsureAppPermissionsSet(exeName, guid); if (result == PermissionCheckResult.ElevationRequired) { if (IsElevated() || UserIsAdmin()) { MessageBox.Show("Failed to set BLE Permissions!", "Failed", MessageBoxButton.OK, MessageBoxImage.Error); Environment.Exit(1); } else { if (MessageBox.Show("BLE permissions requried. Restart now with elevated privilegues?", "Register BLE now?", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { LaunchCurrentAppAsAdmin(); Environment.Exit(2); } } } if (!IsElevated()) { return; } LaunchCurrentAppAgain(); Environment.Exit((int)result); }
private void LaunchCurrentAppAgain() { ProcessStartInfo info = new ProcessStartInfo { FileName = PermissionChecker.GetExe(), UseShellExecute = true }; try { Process process = new Process { StartInfo = info }; process.Start(); } catch (Exception e) { Debug.WriteLine(e.Message); } }