mirror of
https://github.com/ckaczor/Common.git
synced 2026-01-27 09:35:40 -05:00
Initial commit
This commit is contained in:
40
Helpers/ApplicationIsolation.cs
Normal file
40
Helpers/ApplicationIsolation.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Security.AccessControl;
|
||||
using System.Security.Principal;
|
||||
using System.Threading;
|
||||
|
||||
namespace Common.Helpers
|
||||
{
|
||||
public static class ApplicationIsolation
|
||||
{
|
||||
public static IDisposable GetIsolationHandle()
|
||||
{
|
||||
string applicationName = Assembly.GetEntryAssembly().GetName().Name;
|
||||
|
||||
return GetIsolationHandle(applicationName);
|
||||
}
|
||||
|
||||
public static IDisposable GetIsolationHandle(string applicationName)
|
||||
{
|
||||
// Create mutex security settings
|
||||
MutexSecurity mutexSecurity = new MutexSecurity();
|
||||
|
||||
// Create a world security identifier
|
||||
SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
|
||||
|
||||
// Create an access rule for the mutex
|
||||
MutexAccessRule mutexAccessRule = new MutexAccessRule(securityIdentifier, MutexRights.FullControl, AccessControlType.Allow);
|
||||
|
||||
// Add the access rule to the mutex security settings
|
||||
mutexSecurity.AddAccessRule(mutexAccessRule);
|
||||
|
||||
// Create the mutex and see if it was newly created
|
||||
bool createdNew;
|
||||
Mutex mutex = new Mutex(false, applicationName, out createdNew, mutexSecurity);
|
||||
|
||||
// Return the mutex if it is new
|
||||
return createdNew ? mutex : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user