Skip to content

Commit

Permalink
Added an option to the File menu to restart dnSpy with administrato…
Browse files Browse the repository at this point in the history
…r privileges (refs #291)
  • Loading branch information
ElektroKill committed Feb 13, 2024
1 parent 1f0c150 commit 006e76c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
7 changes: 1 addition & 6 deletions dnSpy/dnSpy/MainApp/AppWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public Window InitializeMainWindow() {
#if DEBUG
AddTitleInfo("Debug Build");
#endif
if (IsAdministrator())
if (Constants.IsRunningAsAdministrator)
AddTitleInfo(dnSpy_Resources.User_Administrator);
wpfCommandService.Add(ControlConstants.GUID_MAINWINDOW, mainWindow);
new SavedWindowStateRestorer(mainWindow, uiSettings.SavedWindowState, DefaultWindowLocation);
Expand All @@ -154,11 +154,6 @@ public Window InitializeMainWindow() {
return mainWindow;
}

static bool IsAdministrator() {
using (var id = WindowsIdentity.GetCurrent())
return new WindowsPrincipal(id).IsInRole(WindowsBuiltInRole.Administrator);
}

void IDsLoaderContentProvider.SetLoadingContent(object content) {
Debug.Assert(stackedContent.Count == 0);
stackedContent.AddChild(StackedContentChildImpl.GetOrCreate(content, content));
Expand Down
9 changes: 9 additions & 0 deletions dnSpy/dnSpy/MainApp/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ You should have received a copy of the GNU General Public License
along with dnSpy. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Security.Principal;

namespace dnSpy.MainApp {
static class Constants {
public const string DnSpy = "dnSpy";
// Used in filenames so must only have valid filename chars
public const string DnSpyFile = DnSpy;

public static bool IsRunningAsAdministrator { get; }

static Constants() {
using var id = WindowsIdentity.GetCurrent();
IsRunningAsAdministrator = new WindowsPrincipal(id).IsInRole(WindowsBuiltInRole.Administrator);
}
}
}
61 changes: 61 additions & 0 deletions dnSpy/dnSpy/MainApp/RestartAsAdministratorCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright (C) 2024 ElektroKill
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see <http://www.gnu.org/licenses/>.
*/

using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Input;
using dnSpy.Contracts.App;
using dnSpy.Contracts.Menus;

namespace dnSpy.MainApp {
[ExportMenuItem(OwnerGuid = MenuConstants.APP_MENU_FILE_GUID, Header = "res:RestartAsAdministratorCommand", Group = MenuConstants.GROUP_APP_MENU_FILE_EXIT, Order = 900000)]
sealed class RestartAsAdministratorCommand : MenuItemBase {
readonly IAppWindow appWindow;

[ImportingConstructor]
public RestartAsAdministratorCommand(IAppWindow appWindow) => this.appWindow = appWindow;

public override bool IsVisible(IMenuItemContext context) => !Constants.IsRunningAsAdministrator;

public override void Execute(IMenuItemContext context) {
appWindow.MainWindowClosing += OnMainWindowClosing;
((ICommand)ApplicationCommands.Close).Execute(context);
}

void OnMainWindowClosing(object? sender, CancelEventArgs args) {
appWindow.MainWindowClosing -= OnMainWindowClosing;
// If a different handler canceled the close operation, don't restart.
if (args.Cancel)
return;
var location = Assembly.GetEntryAssembly()?.Location;
if (location is null) {
args.Cancel = true;
return;
}
try {
Process.Start(new ProcessStartInfo(location) { UseShellExecute = true, Verb = "runas" });
}
catch {
args.Cancel = true;
}
}
}
}
9 changes: 9 additions & 0 deletions dnSpy/dnSpy/Properties/dnSpy.Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dnSpy/dnSpy/Properties/dnSpy.Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@
<data name="ResourcesFolder" xml:space="preserve">
<value>Resources</value>
</data>
<data name="RestartAsAdministratorCommand" xml:space="preserve">
<value>Restart as Administrator</value>
</data>
<data name="SaveKey" xml:space="preserve">
<value>Ctrl+S</value>
</data>
Expand Down

0 comments on commit 006e76c

Please sign in to comment.