"Query User" 通过 CMD.exe 调用结果 0 输出

"Query User" called via CMD.exe results 0 Output

我正在尝试调用并收集 CMD 命令返回的数据 query user

从 Windows-startbar 通过 cmd 调用它给我一个正常的结果。

通过此 c# 函数调用此函数给出 0 输出。

     public void callQueryUser()
        {
            ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
            Process p = Process.Start(psi);

            string cmd = string.Format(@"/c query user");
             
            psi.Arguments = cmd;
                                
            psi.RedirectStandardOutput = true;
            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;
            psi.WaitForExit();
           
            string result = p.StandardOutput.ReadToEnd();
            MessageBox.Show(result);
        }

我检查了 Window 说找不到命令...我还检查了它们是否相同 cmd.exe 也是如此。似乎通过 C# 调用 cmd.exe 会有所不同。 有人知道我接下来要检查什么吗?

无需使用 cmd 即可使用 Process 检索您想要的信息。但是,如果您的 OS 是 64 位的,而您的程序是 运行 作为 32 位的,并且您正在尝试访问 %windir%\System32\query.exe,则需要改用 %windir%\Sysnative\query.exe .

尝试以下操作:

选项 1:

public void callQueryUser()
{
    string queryPath = string.Empty;

    //use 'Sysnative' to access 64-bit files (in System32) if program is running as 32-bit process
    //use 'SysWow64' to access 32-bit files on 64-bit OS
    if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
        queryPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "Sysnative", "query.exe");
    else
        queryPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "System32", "query.exe");

    Debug.WriteLine("queryPath: " + queryPath);

    // create new instance
    ProcessStartInfo startInfo = new ProcessStartInfo(queryPath);
    startInfo.Arguments = "user"; //arguments
    startInfo.CreateNoWindow = true; //don't create a window
    startInfo.RedirectStandardError = true; //redirect standard error
    startInfo.RedirectStandardOutput = true; //redirect standard output
    startInfo.UseShellExecute = false; //if true, uses 'ShellExecute'; if false, uses 'CreateProcess'
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;

    //create new instance
    using (Process p = new Process { StartInfo = startInfo, EnableRaisingEvents = true })
    {
        //subscribe to event and add event handler code
        p.ErrorDataReceived += (sender, e) =>
        {
            if (!String.IsNullOrEmpty(e.Data))
            {
                //ToDo: add desired code 
                Debug.WriteLine("Error: " + e.Data);
            }
        };

        //subscribe to event and add event handler code
        p.OutputDataReceived += (sender, e) =>
        {
            if (!String.IsNullOrEmpty(e.Data))
            {
                //ToDo: add desired code
                Debug.WriteLine("Output: " + e.Data);

                string result = e.Data;
                MessageBox.Show(result);
            }
        };

        p.Start(); //start

        p.BeginErrorReadLine(); //begin async reading for standard error
        p.BeginOutputReadLine(); //begin async reading for standard output

        //waits until the process is finished before continuing
        p.WaitForExit();
    } 
}

选项 2:

public void callQueryUser()
{
    string queryPath = string.Empty;

    //environment variable windir has the same value as SystemRoot
    //use 'Sysnative' to access 64-bit files (in System32) if program is running as 32-bit process
    //use 'SysWow64' to access 32-bit files on 64-bit OS
    if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
        queryPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "Sysnative", "query.exe");
    else
        queryPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "System32", "query.exe");

    Debug.WriteLine("queryPath: " + queryPath);

    // create new instance
    ProcessStartInfo startInfo = new ProcessStartInfo(queryPath);
    startInfo.Arguments = "user"; //arguments
    startInfo.CreateNoWindow = true; //don't create a window
    startInfo.RedirectStandardError = true; //redirect standard error
    startInfo.RedirectStandardOutput = true; //redirect standard output
    startInfo.UseShellExecute = false; //if true, uses 'ShellExecute'; if false, uses 'CreateProcess'
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;

    //create new instance
    using (Process p = new Process { StartInfo = startInfo, EnableRaisingEvents = true })
    {
        p.Start(); //start

        //waits until the process is finished before continuing
        p.WaitForExit();

        string result = p.StandardOutput.ReadToEnd();
        MessageBox.Show(result);
    }
}

资源: