C#Windowsデスクトップアプリケーションは1つのインスタンスしか実行しません
1)「プログラムセットの署名」に従って実行されたインスタンスを取得する
2)「プロセス名」に従って実行したインスタンスを取得する
3)「プロセス名とパス」に従って実行したインスタンスを取得する
4)「Mutex」により同一のインスタンスが実行されているか否かを判断する
/// <summary>
/// “ ”
/// .EXE ,
/// </summary>
/// <param name="runningProcess"> Process</param>
/// <returns> ture/false</returns>
/// <remarks> 。 : 。</remarks>
private static bool GetRunningProcessByAssemblyName(out Process runningProcess)
{
bool returnValue = false;
runningProcess = null;
AssemblyName currentAssemblyName =
AssemblyName.GetAssemblyName(Assembly.GetExecutingAssembly().Location);
AssemblyName processAssemblyName = new AssemblyName();
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
// , 。
if (process.Id != current.Id &&
process.ProcessName != "System" &&
process.ProcessName != "csrss" &&
process.ProcessName != "svchost" &&
process.ProcessName != "services" &&
process.ProcessName != "smss" &&
process.ProcessName != "winlogon" &&
process.ProcessName != "explorer" &&
process.ProcessName != "pds" &&
process.ProcessName != "alg" &&
process.ProcessName != "msdtc" &&
process.ProcessName != "spoolsv" &&
process.ProcessName != "lsass" &&
process.ProcessName != "Idle" &&
process.ProcessName != "iexplore" &&
process.ProcessName != "sqlserver" &&
process.ProcessName != "notepad" &&
process.ProcessName != "360tray" &&
process.ProcessName != "XDict"
)
{
try
{
//
processAssemblyName = AssemblyName.GetAssemblyName(process.MainModule.FileName);
}
catch (Exception)
{
processAssemblyName = null;
}
// GetPublicKey() ; , GetPublicKey() Null。
if (processAssemblyName != null &&
CompareBytes(currentAssemblyName.GetPublicKey(),
processAssemblyName.GetPublicKey()))
{
runningProcess = process;
returnValue = true;
break;
}
}
}
return returnValue;
}
2)「プロセス名」に従って実行したインスタンスを取得する
/// <summary>
/// “ ”
/// </summary>
/// <param name="runningProcess"> Process</param>
/// <returns> ture/false</returns>
/// <remarks> :1) ; 2) , 。</remarks>
private static bool GetRunningProcessByProcessName(out Process runningProcess)
{
bool returnValue = false;
runningProcess = null;
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (process.ProcessName == current.ProcessName)
{
runningProcess = process;
returnValue = true;
break;
}
}
}
return returnValue;
}
3)「プロセス名とパス」に従って実行したインスタンスを取得する
/// <summary>
/// “ ”
/// </summary>
/// <param name="runningProcess"> Process</param>
/// <returns> ture/false</returns>
/// <remarks> : , 。</remarks>
private static bool GetRunningProcessByProcessFullName(out Process runningProcess)
{
bool returnValue = false;
runningProcess = null;
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (process.MainModule.FileName ==
Assembly.GetExecutingAssembly().Location)
{
runningProcess = process;
returnValue = true;
break;
}
}
}
return returnValue;
}
4)「Mutex」により同一のインスタンスが実行されているか否かを判断する
/// <summary>
/// “Mutex”
/// </summary>
/// <param name="runningProcess"> null</param>
/// <returns> ture/false</returns>
/// <remarks> : Process</remarks>
private static bool GetRunningProcessByMutex(out Process runningProcess)
{
bool returnValue = false;
runningProcess = null;
bool isCreated;
Mutex m = new Mutex(false, "OneInstance", out isCreated);
if (!(isCreated))
{
MessageBox.Show(" 。", " ",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
returnValue = !isCreated;
return returnValue;
}
#region
/// <summary>
///
/// </summary>
private static void SetForegroundProcess(Process process)
{
bool isIcon = IsIconic(process.MainWindowHandle);
//
if (isIcon)
{
//
ShowWindowAsync(process.MainWindowHandle, SW_RESTORE);
}
else
{
//
SetForegroundWindow(process.MainWindowHandle);
}
}
/// <summary>
///
/// </summary>
private static bool CompareBytes(byte[] bytes1, byte[] bytes2)
{
if (bytes1 == null || bytes2 == null)
return false;
if (bytes1.Length != bytes2.Length)
return false;
for (int i = 0; i < bytes1.Length; i++)
{
if (bytes1[i] != bytes2[i])
return false;
}
return true;
}
#endregion
#region Windows API
/// <summary>
/// ,
/// </summary>
/// <param name="hWnd"> </param>
/// <returns> , </returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool OpenIcon(IntPtr hWnd);
/// <summary>
///
/// </summary>
/// <param name="hWnd"> </param>
/// <returns> , </returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
/// <summary>
///
/// </summary>
/// <param name="hWnd"> </param>
/// <returns> , </returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// ShowWindow , ShowWindow , 。
/// , 。
/// ,
/// </summary>
/// <param name="hWnd"> </param>
/// <param name="cmdShow"> </param>
/// <returns> , TRUE( ), FALSE( )</returns>
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
private const int SW_HIDE = 0; // ,
private const int SW_SHOWNORMAL = 1; // ,
private const int SW_SHOWMINIMIZED = 2; // ,
private const int SW_SHOWMAXIMIZED = 3; // ,
private const int SW_SHOWNOACTIVATE = 4; // ,
private const int SW_RESTORE = 9; // ,
private const int SW_SHOWDEFAULT = 10; //
#endregion