Jelajahi Sumber

Use consistent exe path API

also works in .NET Core now https://stackoverflow.com/a/61182961

potentially fixes #23
Daniel Chalmers 2 tahun lalu
induk
melakukan
c9af70cd56

+ 5 - 3
DesktopClock/App.xaml.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Windows;
 using DesktopClock.Properties;
 using Microsoft.Win32;
@@ -11,6 +12,8 @@ namespace DesktopClock;
 /// </summary>
 public partial class App : Application
 {
+    public static string FilePath = Process.GetCurrentProcess().MainModule.FileName;
+
     // https://www.materialui.co/colors - A100, A700.
     public static IReadOnlyList<Theme> Themes { get; } = new[]
     {
@@ -43,12 +46,11 @@ public partial class App : Application
     /// <param name="runOnStartup"></param>
     public static void SetRunOnStartup(bool runOnStartup)
     {
-        var exePath = ResourceAssembly.Location;
-        var keyName = GetSha256Hash(exePath);
+        var keyName = GetSha256Hash(FilePath);
         using var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
 
         if (runOnStartup)
-            key?.SetValue(keyName, exePath); // Use the path as the name so we can handle multiple exes, but hash it or Windows won't like it.
+            key?.SetValue(keyName, FilePath); // Use the path as the name so we can handle multiple exes, but hash it or Windows won't like it.
         else
             key?.DeleteValue(keyName, false);
     }

+ 1 - 1
DesktopClock/MainWindow.xaml.cs

@@ -90,7 +90,7 @@ public partial class MainWindow : Window
         if (result != MessageBoxResult.OK)
             return;
 
-        var currentExe = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
+        var currentExe = new FileInfo(App.FilePath);
         var newExePath = Path.Combine(currentExe.DirectoryName, currentExe.GetFileAtNextIndex().Name);
 
         // Copy and start the new clock.

+ 1 - 1
DesktopClock/Properties/Settings.cs

@@ -26,7 +26,7 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
     private Settings()
     {
         // Settings file path from same directory as the executable.
-        var exeInfo = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
+        var exeInfo = new FileInfo(App.FilePath);
         var settingsFileName = Path.GetFileNameWithoutExtension(exeInfo.FullName) + ".settings";
         FilePath = Path.Combine(exeInfo.DirectoryName, settingsFileName);