如何在c#visual studio 2012 web应用程序中找到System.Diagnostic.Process
以编程方式运行的凭据/身份?
答案 0 :(得分:1)
假设您已添加对System.Management的引用,并且您知道您感兴趣的进程ID,则可以使用以下代码:
public void GetProcessOwner(int processId)
{
string query = "SELECT * FROM Win32_Process WHERE ProcessId = " + processId;
using (var searcher = new ManagementObjectSearcher("root\\CIMV2", query))
{
foreach (var queryObj in searcher.Get().OfType<ManagementObject>())
{
ManagementBaseObject outParams = queryObj.InvokeMethod("GetOwner", null, null);
Console.WriteLine("{0} is owned by {1}\\{2}", queryObj["Name"], outParams["Domain"], outParams["User"]);
}
}
}