Process.StartInfo.Username为空

Eph*_*dra 6 c# asp.net process

我开始一个过程

Process app = new Process();
app.StartInfo.UseShellExecute = false;
app.StartInfo.FileName = path;
app.StartInfo.Domain = "Domain";
app.StartInfo.UserName = "userName";

string password = "Password";
System.Security.SecureString ssPwd = new System.Security.SecureString();

for (int x = 0; x < password.Length; x++)
{
    ssPwd.AppendChar(password[x]);
}

password = "";

app.StartInfo.Password = ssPwd;
app.Start();
Run Code Online (Sandbox Code Playgroud)

然后我确认它正在运行:

private bool IsRunning(string name)
{
    Process[] processlist = Process.GetProcesses();

    if (Process.GetProcessesByName(name).Length > 0)
    {
        string user = Process.GetProcessesByName(name)[0].StartInfo.UserName; 
        log.Debug("Process " + name + " is running by : " + user);

        return true;
    }
    else
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

我回来true找到了这个过程但是UserName空的.这是为什么?

我还找到了一些代码来获取进程的所有者,但是当我使用它时,所有者也是空的.

public string GetProcessOwner(int processId)
{
    string query = "SELECT * FROM Win32_Process WHERE ProcessID = " + processId;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    ManagementObjectCollection processList = searcher.Get();

    foreach (ManagementObject obj in processList)
    {
        string[] argList = new string[] { string.Empty, string.Empty };
        int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));

        if (returnVal == 0)
        {
            // return DOMAIN\user
            return argList[1] + "\\" + argList[0];
        }
    }

    return "NO OWNER";
}
Run Code Online (Sandbox Code Playgroud)

请你解释一下为什么会这样?

pfx*_*pfx 10

这是设计的; 当请求关于Process通道的信息时,例如.GetProcessesByNameUserName没有得到检索/解决.

GetProcessByName通过下面的代码在内部检索其信息,Process从获得的ProcesInfo数据构造实例.

  public static Process[] GetProcesses(string machineName) 
  {
        bool isRemoteMachine = ProcessManager.IsRemoteMachine(machineName);
        ProcessInfo[] processInfos = ProcessManager.GetProcessInfos(machineName);
        Process[] processes = new Process[processInfos.Length];
        for (int i = 0; i < processInfos.Length; i++) {
            ProcessInfo processInfo = processInfos[i];

            processes[i] = new Process(machineName, isRemoteMachine, processInfo.processId, processInfo);
        }

        return processes;
}
Run Code Online (Sandbox Code Playgroud)


一个ProcessInfo实例不包含任何用户/所有者的信息.

internal class ProcessInfo 
{
    public ArrayList threadInfoList = new ArrayList();
    public int basePriority;
    public string processName;
    public int processId;
    public int handleCount;
    public long poolPagedBytes;
    public long poolNonpagedBytes;
    public long virtualBytes;
    public long virtualBytesPeak;
    public long workingSetPeak;
    public long workingSet;
    public long pageFileBytesPeak;
    public long pageFileBytes;
    public long privateBytes;
    public int mainModuleId;
    public int sessionId;
}
Run Code Online (Sandbox Code Playgroud)


私有构造中的Process类接受这个ProcessInfo.

因为没有ProcessStartInfo设置,所以它在检索时实例化一个,为UserName.

public string UserName {
    get { 
        if( userName == null) {
            return string.Empty;
        }
        else {        
            return userName;                     
        }
    } 
    set { userName = value; }
}
Run Code Online (Sandbox Code Playgroud)

关于GetProcessOwner代码; 这是对如何确定C#中进程所有者的答案的结果题.

这是另一个WMI相关的主题; 没有收到任何所有者信息可能与评论中建议的权限有关.
对我来说,"它可以在我的机器上运行"......
最好为这个开始一个单独的问题.