0

在我正在处理的应用程序中,一切正常。我的问题,有没有办法在执行 psexec 时抑制命令窗口?我希望它安静地运行。下面是我正在使用的代码。我在网上阅读了很多示例,但似乎没有任何效果。想法?谢谢。

            Process p = new Process();
            try
            {
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.RedirectStandardInput = true;                                      
                p = Process.Start(psExec, psArguments);
                if (p != null)
                {
                    string output = p.StandardOutput.ReadToEnd();
                    string error = p.StandardError.ReadToEnd();
                    p.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (p != null)
                {
                    p.Dispose();
                }
            }
4

1 回答 1

4

您在实际再次分配 p 变量之前设置 StartInfo ,您的代码必须如下所示:

...
ProcessStartInfo startinfo = new ProcessStartInfo(psExec, psArguments);
startinfo.UseShellExecute = false;
startinfo.CreateNoWindow = true;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.RedirectStandardOutput = true;
startinfo.RedirectStandardError = true;
startinfo.RedirectStandardInput = true;                                      
p = Process.Start(startinfo);
...
于 2013-11-04T16:52:27.987 回答