假设我已经有了窗口的句柄,我可以使用GetWindowThreadProcessId
. 有没有一种方法可以让我获得进程名称而不必获取所有进程并尝试匹配我的 PID?
问问题
23162 次
3 回答
20
您可以Process.GetProcessById
使用Process
. Process
有很多关于正在运行的程序的信息。 Process.ProcessName
给你名字,Process.MainModule.FileName
给你可执行文件的名字。
于 2011-01-27T17:39:05.187 回答
14
string name;
using (var p = Process.GetProcessById(id)) { name = p.ProcessName; }
于 2011-01-27T17:40:22.997 回答
0
// 这是一个返回任务管理器内存的简洁小方法。如果进程id不存在,会抛出异常,内存返回0
/// <summary>
/// Gets the process memory.
/// </summary>
/// <param name="processId">The process identifier.</param>
/// <returns></returns>
/// <para> </para>
/// <para> </para>
/// <exception cref="ArgumentException"> </exception>
/// <exception cref="ArgumentNullException"> </exception>
/// <exception cref="ComponentModel.Win32Exception"> </exception>
/// <exception cref="InvalidOperationException"> </exception>
/// <exception cref="PlatformNotSupportedException"> </exception>
/// <exception cref="UnauthorizedAccessException"> </exception>
public static long GetProcessMemory(int processId)
{
try
{
var instanceName = Process.GetProcessById(processId).ProcessName;
using (var performanceCounter = new PerformanceCounter("Process", "Working Set - Private", instanceName))
{
return performanceCounter.RawValue / Convert.ToInt64(1024);
}
}
catch (Exception)
{
return 0;
}
}
于 2016-05-13T12:31:54.770 回答