2013-11-04 104 views
0

在我正在处理的应用程序中,一切都很好。我的问题是,有没有办法在执行psexec时压缩命令窗口?我希望它能够静静地运行。下面是我使用的代码..我在网上阅读了很多例子,但似乎没有任何工作。思考?谢谢。禁止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(); 
       } 
      } 
+0

正在运行什么进程?如果这个过程有一个可以被抑制的主窗口,它通常是......但是如果它产生了随机窗口,那么你可以做的事情就不多了。 – Vlad

+0

我正在做几件事情。网络使用,出口regKeys等。每次,它开启一个新窗口。 – Sparhawk

回答

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); 
... 
+0

好的交易,我会做出改变并对其进行测试。谢谢。 – Sparhawk

+0

工作就像一个魅力。谢谢! – Sparhawk