From 923b00cfc8a50d376eb3acb3daf2842b4521e054 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:53:31 +0100 Subject: [PATCH] Use guid instead of app name to create the mutex --- ClipCMD/App.xaml.cs | 23 ++++++++++++++++------- ClipCMD/MainWindow.xaml.cs | 9 ++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ClipCMD/App.xaml.cs b/ClipCMD/App.xaml.cs index ee633c2..fb029cd 100644 --- a/ClipCMD/App.xaml.cs +++ b/ClipCMD/App.xaml.cs @@ -1,4 +1,5 @@ -using System.Threading; +using System; +using System.Threading; using System.Windows; namespace ClipCMD; @@ -8,20 +9,28 @@ namespace ClipCMD; /// public partial class App : Application { - Mutex mutex; + public const string AppName = "ClipCMD"; + + public const string AppGuid = "68d780d4-b946-4154-8815-e4632e5ec5d8"; + + private Mutex? mutex; + protected override void OnStartup(StartupEventArgs e) { - const string appName = "ClipCMD"; - - mutex = new Mutex(true, appName, out bool createdNew); + mutex = new Mutex(true, AppGuid, out bool createdNew); if (!createdNew) { //App is already running! Exiting the application - _ = MessageBox.Show($"Another instance of {appName} already running!", $"{appName} is already running!", MessageBoxButton.OK, MessageBoxImage.Exclamation); + _ = MessageBox.Show($"Another instance of {AppName} already running!", $"{AppName} is already running!", MessageBoxButton.OK, MessageBoxImage.Exclamation); Current.Shutdown(); } - base.OnStartup(e); + Exit += CloseHandler; + } + + protected void CloseHandler(object sender, EventArgs e) + { + mutex?.ReleaseMutex(); } } \ No newline at end of file diff --git a/ClipCMD/MainWindow.xaml.cs b/ClipCMD/MainWindow.xaml.cs index d5259b1..4c707fe 100644 --- a/ClipCMD/MainWindow.xaml.cs +++ b/ClipCMD/MainWindow.xaml.cs @@ -8,6 +8,7 @@ using System.IO; using System.Linq; using System.Management.Automation; using System.Management.Automation.Language; +using System.Reflection; using System.Text; using System.Text.Json; using System.Threading.Tasks; @@ -45,7 +46,7 @@ public partial class MainWindow : Window notifyIcon.Click += NotifyIcon_Click; - string applicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + string applicationDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "StoneRed"); string folderPath = Path.Combine(applicationDataPath, "ClipCMD"); if (!Directory.Exists(folderPath)) @@ -69,6 +70,12 @@ public partial class MainWindow : Window DataContext = this; InitializeComponent(); + +#if DEBUG + Title = $"{App.AppName} - Dev {Assembly.GetExecutingAssembly().GetName().Version}"; +#else + Title = $"{App.AppName} - {Assembly.GetExecutingAssembly().GetName().Version}"; +#endif } protected override void OnSourceInitialized(EventArgs e)