使用 C# .net 检测进程已在 Windows 中运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/187915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting a Process is already running in windows using C# .net
提问by jinsungy
How do I detect if a process is already running under the Windows Task Manager? I'd like to get the memory and cpu usage as well.
如何检测进程是否已在 Windows 任务管理器下运行?我也想获得内存和 CPU 使用率。
采纳答案by Ian Jacobs
Have you looked into the System.Diagnostics.ProcessClass.
您是否查看过System.Diagnostics.Process类。
回答by Tomalak
You could use WMI to query something along the lines of
您可以使用 WMI 来查询以下内容
"SELECT * FROM Win32_Process WHERE Name = '<your process name here>'"
Especially processor usage is a bit tricky with WMI, though. You are probably better off with System.Diagnostics.Process, as Ian Jacobssuggested.
不过,尤其是处理器的使用对于 WMI 来说有点棘手。正如Ian Jacobs建议的那样,使用 System.Diagnostics.Process 可能会更好。
回答by ZombieSheep
Simple example...
简单的例子...
bool processIsRunning(string process)
{
return (System.Diagnostics.Process.GetProcessesByName(process).Length != 0);
}
Oops... forgot the mem usage, etc...
哎呀...忘记了内存使用等...
bool processIsRunning(string process)
{
System.Diagnostics.Process[] processes =
System.Diagnostics.Process.GetProcessesByName(process);
foreach (System.Diagnostics.Process proc in processes)
{
Console.WriteLine("Current physical memory : " + proc.WorkingSet64.ToString());
Console.WriteLine("Total processor time : " + proc.TotalProcessorTime.ToString());
Console.WriteLine("Virtual memory size : " + proc.VirtualMemorySize64.ToString());
}
return (processes.Length != 0);
}
(I'll leave the mechanics of getting the data out of the method to you - it's 17:15 here, and I'm ready to go home. :)
(我将把从方法中获取数据的机制留给你——现在是 17:15,我准备回家了。:)
回答by StubbornMule
You can use System.Diagnostics.ProcessClass.
There is a GetProcesses()and a GetProcessesByName()method that will get a list of all the existing processes in an array.
您可以使用System.Diagnostics.Process类。
有一个GetProcesses()和一个GetProcessesByName()方法可以获取数组中所有现有进程的列表。
The Process
object has all the information you need to detect if a process is running.
该Process
对象具有检测进程是否正在运行所需的所有信息。
回答by Millhouse
If you wanted to find out about the IE Processes that are running:
如果您想了解正在运行的 IE 进程:
System.Diagnostics.Process[] ieProcs = Process.GetProcessesByName("IEXPLORE");
if (ieProcs.Length > 0)
{
foreach (System.Diagnostics.Process p in ieProcs)
{
String virtualMem = p.VirtualMemorySize64.ToString();
String physicalMem = p.WorkingSet64.ToString();
String cpu = p.TotalProcessorTime.ToString();
}
}
回答by Mike L
Something like this:
像这样的东西:
foreach ( WindowsProcess in Process.GetProcesses)
{
if (WindowsProcess.ProcessName == nameOfProcess) {
Console.WriteLine(WindowsProcess.WorkingSet64.ToString);
Console.WriteLine(WindowsProcess.UserProcessorTime.ToString);
Console.WriteLine(WindowsProcess.TotalProcessorTime.ToString);
}
}