Reputation:
How could I allow a single instance application started from the same directory but several instances from different directories of the same executable ?
More clearly,
I want MyProgram.Exe<instance 1>
to be run mono instance from "C:\Directory1"
I want MyProgram.Exe<instance 2>
to be run mono instance from "C:\Directory2"
And MyProgram.Exe<instance 1>
and MyProgram.Exe<instance 2>
can be run at the same time.
What I'm doing at this time is using Mutex, but I don't know how to achieve this :
public partial class App : Application
{
private static Mutex _mutex = null;
// Application GUID
private string _applicationGUID = "4hfd130b-1eh6-4979-bbqc-08g16478c36f";
public App()
{
bool isNewInstance;
_mutex = new Mutex(true, _applicationGUID, out isNewInstance);
if (!isNewInstance)
{
MessageBox.Show("An application is already running. Closing...", "MyProgram", MessageBoxButton.OK, MessageBoxImage.Exclamation);
Current.Shutdown();
}
else
{
// Other stuff ...
}
}
}
Your help will be appreciated.
Upvotes: 2
Views: 366
Reputation: 81563
You might be able to use Assembly.Location
(or the exe path in general) as the named mutex, with or without your Id.
Gets the full path or UNC location of the loaded file that contains the manifest.
_mutex = new Mutex(true, directory, out isNewInstance);
or completely overkill
var location = Assembly.GetEntryAssembly().Location;
var hs = System.Security.Cryptography.MD5.Create();
var bytes = hs.ComputeHash(System.Text.Encoding.UTF8.GetBytes(location));
_mutex = new Mutex(true, Convert.ToBase64String(bytes), out isNewInstance);
Upvotes: 1