如何查找正在运行Diagnostic.Process的凭据

时间:2014-09-03 21:32:31

标签: c# visual-studio-2012

如何在c#visual studio 2012 web应用程序中找到System.Diagnostic.Process以编程方式运行的凭据/身份?

1 个答案:

答案 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"]);
        }
    }
 }