From b12cbff0206a5156c5fcfb8c4f9845048e08a4ca Mon Sep 17 00:00:00 2001 From: ElektroKill Date: Tue, 13 Feb 2024 20:47:29 +0100 Subject: [PATCH] Follow up to 006e76c938b73878a1ca0dcfb75396a69a83820c Fixed functionality for .NET 8 builds --- dnSpy/dnSpy/MainApp/Constants.cs | 15 +++++++++++++++ .../MainApp/RestartAsAdministratorCommand.cs | 7 +------ .../Settings/WindowsExplorerIntegration.cs | 11 +---------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/dnSpy/dnSpy/MainApp/Constants.cs b/dnSpy/dnSpy/MainApp/Constants.cs index 42a99c3e60..b5b474a5b6 100644 --- a/dnSpy/dnSpy/MainApp/Constants.cs +++ b/dnSpy/dnSpy/MainApp/Constants.cs @@ -17,6 +17,8 @@ You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ +using System.IO; +using System.Reflection; using System.Security.Principal; namespace dnSpy.MainApp { @@ -27,9 +29,22 @@ static class Constants { public static bool IsRunningAsAdministrator { get; } + public static string ExecutablePath { get; } + static Constants() { using var id = WindowsIdentity.GetCurrent(); IsRunningAsAdministrator = new WindowsPrincipal(id).IsInRole(WindowsBuiltInRole.Administrator); + + ExecutablePath = Assembly.GetEntryAssembly()!.Location; +#if NET + // Use the native exe and not the managed file + ExecutablePath = Path.ChangeExtension(ExecutablePath, "exe"); + if (!File.Exists(ExecutablePath)) { + // All .NET files could be in a bin sub dir + if (Path.GetDirectoryName(Path.GetDirectoryName(ExecutablePath)) is string baseDir) + ExecutablePath = Path.Combine(baseDir, Path.GetFileName(ExecutablePath)); + } +#endif } } } diff --git a/dnSpy/dnSpy/MainApp/RestartAsAdministratorCommand.cs b/dnSpy/dnSpy/MainApp/RestartAsAdministratorCommand.cs index 913f8dbf4c..61b33b3464 100644 --- a/dnSpy/dnSpy/MainApp/RestartAsAdministratorCommand.cs +++ b/dnSpy/dnSpy/MainApp/RestartAsAdministratorCommand.cs @@ -45,13 +45,8 @@ void OnMainWindowClosing(object? sender, CancelEventArgs args) { // 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" }); + Process.Start(new ProcessStartInfo(Constants.ExecutablePath) { UseShellExecute = true, Verb = "runas" }); } catch { args.Cancel = true; diff --git a/dnSpy/dnSpy/MainApp/Settings/WindowsExplorerIntegration.cs b/dnSpy/dnSpy/MainApp/Settings/WindowsExplorerIntegration.cs index fa49ab5bb9..4b37c071a3 100644 --- a/dnSpy/dnSpy/MainApp/Settings/WindowsExplorerIntegration.cs +++ b/dnSpy/dnSpy/MainApp/Settings/WindowsExplorerIntegration.cs @@ -74,16 +74,7 @@ public bool? WindowsExplorerIntegration { return; bool enabled = value.Value; - var path = Assembly.GetEntryAssembly()!.Location; -#if NET - // Use the native exe and not the managed file - path = Path.ChangeExtension(path, "exe"); - if (!File.Exists(path)) { - // All .NET files could be in a bin sub dir - if (Path.GetDirectoryName(Path.GetDirectoryName(path)) is string baseDir) - path = Path.Combine(baseDir, Path.GetFileName(path)); - } -#endif + var path = Constants.ExecutablePath; if (!File.Exists(path)) { messageBoxService.Show("Cannot locate dnSpy!"); return;