C# 如何获取正在运行的进程的guid
我已使用中的代码查看我的应用程序是否已在运行。如果它正在运行,我希望能够得到正在运行的应用程序,并把它带到前面,以便用户可以看到它 要做到这一点,我可以使用C# 如何获取正在运行的进程的guid,c#,C#,我已使用中的代码查看我的应用程序是否已在运行。如果它正在运行,我希望能够得到正在运行的应用程序,并把它带到前面,以便用户可以看到它 要做到这一点,我可以使用Process.Name属性并检查该属性,但如果另一个进程的名称与我的应用程序的名称相同,或者名称在不同版本之间发生更改,则可能无法工作。为了解决这个问题,我认为可以在应用程序guid上进行比较。我可以通过执行以下操作获取我的应用程序guid: string appGuid = ((GuidAttribute)Assembly.GetExec
Process.Name
属性并检查该属性,但如果另一个进程的名称与我的应用程序的名称相同,或者名称在不同版本之间发生更改,则可能无法工作。为了解决这个问题,我认为可以在应用程序guid上进行比较。我可以通过执行以下操作获取我的应用程序guid:
string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(GuidAttribute), false)
.GetValue(0)).Value.ToString();
有没有一种方法可以获取正在运行的进程的应用程序guid或正在执行的程序集guid,以便对其进行比较?结果表明,只需在进程上使用
assembly.LoadFrom
,就相当简单了。这可能会引发一些与访问有关的错误,因此要小心这些错误。我创建了以下类来检查一个应用程序是否正在运行,以及它是否正在运行,从而将进程带到前端
请注意,下面的代码只是在加载程序集时包含异常,而这不是您想要的。目前情况就是这样,而我找到了合适的例外情况
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;
namespace Helpers
{
public static class SingleInstance
{
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);
private const int SW_RESTORE = 9;
private static string _appGuid;
private static Mutex _mutex;
static SingleInstance()
{
_appGuid = GetAssemblyGuid(Assembly.GetExecutingAssembly());
}
public static bool IsAlreadyRunning(bool useGlobal)
{
//This code was taken from http://stackoverflow.com/a/229567/4631427
string mutexId;
if (useGlobal)
{
mutexId = String.Format("Global\\{{{0}}}", _appGuid);
}
else
{
mutexId = String.Format("{{{0}}}", _appGuid);
}
MutexAccessRule allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
MutexSecurity securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
bool createdNew;
_mutex = new Mutex(false, mutexId, out createdNew, securitySettings);
bool hasHandle = false;
try
{
hasHandle = _mutex.WaitOne(0, false);
if (!hasHandle)
{
return true;
}
}
catch (AbandonedMutexException)
{
hasHandle = true;
}
return false;
}
public static void ShowRunningApp()
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcesses())
{
if (process.Id == current.Id)
{
continue;
}
try
{
Assembly assembly = Assembly.LoadFrom(process.MainModule.FileName);
string processGuid = GetAssemblyGuid(assembly);
if (_appGuid.Equals(processGuid))
{
BringProcessToFront(process);
return;
}
} catch { }
}
}
private static string GetAssemblyGuid(Assembly assembly)
{
object[] customAttribs = assembly.GetCustomAttributes(typeof(GuidAttribute), false);
if (customAttribs.Length < 1)
{
return null;
}
return ((GuidAttribute)(customAttribs.GetValue(0))).Value.ToString();
}
private static void BringProcessToFront(Process process)
{
IntPtr handle = process.MainWindowHandle;
if (IsIconic(handle))
{
ShowWindow(handle, SW_RESTORE);
}
SetForegroundWindow(handle);
}
}
}
使用系统;
使用系统诊断;
运用系统反思;
使用System.Runtime.InteropServices;
使用System.Security.AccessControl;
使用System.Security.Principal;
使用系统线程;
命名空间帮助程序
{
公共静态类单实例
{
[DllImport(“User32.dll”)]
私有静态外部布尔SetForegroundWindow(IntPtr句柄);
[DllImport(“User32.dll”)]
私有静态外部bool ShowWindow(IntPtr句柄,intncmdshow);
[DllImport(“User32.dll”)]
私有静态外部布尔函数(IntPtr句柄);
私有常量int SW_RESTORE=9;
私有静态字符串_appGuid;
专用静态互斥锁(Mutex);;
静态单实例()
{
_appGuid=GetAssemblyGuid(Assembly.GetExecutionGassembly());
}
公共静态bool IsAlreadyRunning(bool useGlobal)
{
//此代码取自http://stackoverflow.com/a/229567/4631427
字符串mutexId;
如果(使用全局)
{
mutexId=String.Format(“全局\\{{{0}}}”,_appGuid);
}
其他的
{
mutexId=String.Format(“{{{0}}}”,_-appGuid);
}
MutexAccessRule allowEveryoneRule=新的MutexAccessRule(新的安全标识符(WellKnownSidType.WorldSid,null)、MutexRights.FullControl、AccessControlType.Allow);
MutexSecurity securitySettings=新的MutexSecurity();
securitySettings.AddAccessRule(AllowEveryoneUrle);
布尔创造了新的;
_mutex=新的mutex(false、mutexId、out createdNew、securitySettings);
bool-hasHandle=false;
尝试
{
hasHandle=_mutex.WaitOne(0,false);
if(!hasHandle)
{
返回true;
}
}
捕获(放弃mutexception)
{
hasHandle=true;
}
返回false;
}
公共静态无效ShowRunningApp()
{
Process current=Process.GetCurrentProcess();
foreach(Process.getprocesss()中的进程)
{
if(process.Id==current.Id)
{
继续;
}
尝试
{
Assembly=Assembly.LoadFrom(process.MainModule.FileName);
字符串processGuid=GetAssemblyGuid(程序集);
if(_appGuid.Equals(processGuid))
{
将流程带到前端(流程);
返回;
}
}捕获{}
}
}
私有静态字符串GetAssemblyGuid(程序集)
{
object[]customAttribs=assembly.GetCustomAttributes(typeof(GuidAttribute),false);
如果(customAttribs.Length<1)
{
返回null;
}
return((GuidAttribute)(customAttribs.GetValue(0)).Value.ToString();
}
私有静态void BringProcessToFront(进程进程)
{
IntPtr handle=process.MainWindowHandle;
if(二次曲线(手柄))
{
显示窗口(手柄、开关恢复);
}
SetForegroundWindow(手柄);
}
}
}
事实证明,只需在流程中使用Assembly.LoadFrom
就可以了。这可能会引发一些与访问有关的错误,因此要小心这些错误。我创建了以下类来检查一个应用程序是否正在运行,以及它是否正在运行,从而将进程带到前端
请注意,下面的代码只是在加载程序集时包含异常,而这不是您想要的。目前情况就是这样,而我找到了合适的例外情况
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;
namespace Helpers
{
public static class SingleInstance
{
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);
private const int SW_RESTORE = 9;
private static string _appGuid;
private static Mutex _mutex;
static SingleInstance()
{
_appGuid = GetAssemblyGuid(Assembly.GetExecutingAssembly());
}
public static bool IsAlreadyRunning(bool useGlobal)
{
//This code was taken from http://stackoverflow.com/a/229567/4631427
string mutexId;
if (useGlobal)
{
mutexId = String.Format("Global\\{{{0}}}", _appGuid);
}
else
{
mutexId = String.Format("{{{0}}}", _appGuid);
}
MutexAccessRule allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
MutexSecurity securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
bool createdNew;
_mutex = new Mutex(false, mutexId, out createdNew, securitySettings);
bool hasHandle = false;
try
{
hasHandle = _mutex.WaitOne(0, false);
if (!hasHandle)
{
return true;
}
}
catch (AbandonedMutexException)
{
hasHandle = true;
}
return false;
}
public static void ShowRunningApp()
{
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcesses())
{
if (process.Id == current.Id)
{
continue;
}
try
{
Assembly assembly = Assembly.LoadFrom(process.MainModule.FileName);
string processGuid = GetAssemblyGuid(assembly);
if (_appGuid.Equals(processGuid))
{
BringProcessToFront(process);
return;
}
} catch { }
}
}
private static string GetAssemblyGuid(Assembly assembly)
{
object[] customAttribs = assembly.GetCustomAttributes(typeof(GuidAttribute), false);
if (customAttribs.Length < 1)
{
return null;
}
return ((GuidAttribute)(customAttribs.GetValue(0))).Value.ToString();
}
private static void BringProcessToFront(Process process)
{
IntPtr handle = process.MainWindowHandle;
if (IsIconic(handle))
{
ShowWindow(handle, SW_RESTORE);
}
SetForegroundWindow(handle);
}
}
}
使用系统;
使用系统诊断;
运用系统反思;
使用System.Runtime.InteropServices;
使用System.Security.AccessControl;
使用System.Security.Principal;
使用系统线程;
命名空间帮助程序
{
公共静态类单实例
{
[DllImport(“User32.dll”)]
私有静态外部布尔SetForegroundWindow(IntPtr句柄);
[DllImport(“User32.dll”)]
私有静态外部bool ShowWindow(IntPtr句柄,intncmdshow);
[DllImport(“User32.dll”)]
私有静态外部布尔函数(IntPtr句柄);
私有常量int SW_RESTORE=9;
私有静态字符串_appGuid;
专用静态互斥锁(Mutex);;
静态单实例()
{
_appGuid=GetAssemblyGuid(Assembly.GetExecutionGassembly());
}
公共静态bool IsAlreadyRunning(bool useGlobal)
{
//此代码取自http://stackoverflow.com/a/229