C# 在C ManagementObjectSeacher中,我无法使用Query.QueryString和Get()
这是我的代码,即使在添加System.Management命名空间之后,我仍然对mos.Query和GetC# 在C ManagementObjectSeacher中,我无法使用Query.QueryString和Get(),c#,wpf,windows,process,C#,Wpf,Windows,Process,这是我的代码,即使在添加System.Management命名空间之后,我仍然对mos.Query和Get string processName = Process.GetCurrentProcess().MainModule.ModuleName; ManagementObjectSearcher mos = new ManagementObjectSearcher(); mos.Query.QueryString = @"SELECT * FROM Win32_Process WHERE
string processName = Process.GetCurrentProcess().MainModule.ModuleName;
ManagementObjectSearcher mos = new ManagementObjectSearcher();
mos.Query.QueryString = @"SELECT * FROM Win32_Process WHERE Name = '" + processName + @"'";
if (mos.Get().Count > 1)
{
return true;
}
else
{
return false;
}
当我重新构造代码时,我只收到一个结果,因此您的代码将始终返回false,除非您运行具有相同图像名称的同一应用程序的多个实例
var processName = Process.GetCurrentProcess().MainModule.ModuleName;
var mos = new ManagementObjectSearcher();
mos.Query.QueryString = @"SELECT * FROM Win32_Process WHERE Name = '" + processName + @"'";
var collection = mos.Get();
var count = collection.Count;
// If count == 1, the process is already running. If it's 0, it's not running. If it's > 1, then multiple copies are running.
return count == 1;
您可能还需要阅读互斥和单实例应用程序。这里和其他地方都有很多关于这个主题的好教程。大家好,欢迎来到SO。对您的问题进行更详细的描述会有所帮助。同时,我还建议您阅读。实际上,我正在尝试检查我的应用程序是否已在运行。?然后您可能要检查.Count属性是否>=1,而不是>1。或者,如果您试图创建单个实例,请选中.Count==1。