从的Process.Start C#隐藏控制台窗口 [英] Hide console window from Process.Start C#

查看:1134
本文介绍了从的Process.Start C#隐藏控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用使用的System.Diagnostics.Process类的远程计算机上创建过程。
我能够创建一个进程。但问题是,建立一个服务是需要很长的时间,并显示控制台窗口。
另一个讨厌的就是显示在我的Windows窗体的顶部窗控制台,我不能做到这一点窗体上的任何其他操作。
我设置了所有的属性,如 CreateNoWindow = TRUE

  proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

但仍显示控制台窗口。连我都重定向输出和错误单独流,但没有运气。

有没有其他的方法来隐藏控制台窗口?请帮助我。

下面是我用于执行SC命令我的code的一部分。

 过程PROC =新工艺();
proc.StartInfo.UseShellExecute = FALSE;
proc.StartInfo.CreateNoWindow = TRUE;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.FileName =SC;
proc.StartInfo.Arguments =的String.Format(@\\\\ SYS25创建MySvc binPath = C:\\ mysvc.exe);
proc.StartInfo.RedirectStandardError = FALSE;
proc.StartInfo.RedirectStandardOutput = FALSE;
proc.StartInfo.UseShellExecute = FALSE;
proc.StartInfo.CreateNoWindow = TRUE;
proc.Start();
proc.WaitForExit();


解决方案

我试图启动一个进程,而不显示控制台窗口时,也有类似的问题。我的属性值的几种不同组合进行试验,直到我发现了一个展示我想要的行为。

下面是一个页面,详细说明了为什么 UseShellExecute 属性必须设置为false。结果
 <一href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx\">http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx

在页面上说明部分:


  

如果该UseShellExecute属性为true或用户名和
  Password属性不为空,则CreateNoWindow财产
  值被忽略,并创建一个新的窗口。


的ProcessStartInfo的StartInfo =新的ProcessStartInfo();
startInfo.FileName =完整路径;
startInfo.Arguments = ARGS;
startInfo.RedirectStandardOutput =真;
startInfo.RedirectStandardError = TRUE;
startInfo.UseShellExecute = FALSE;
startInfo.CreateNoWindow = TRUE;流程processTemp =新的Process();
processTemp.StartInfo = StartInfo的;
processTemp.EnableRaisingEvents = TRUE;
尝试
{
    processTemp.Start();
}
赶上(例外五)
{
    扔;
}

I am trying to create process on a remote machine using using System.Diagnostics.Process class. I am able to create a process. But the problem is, creating a service is take a long time and console window is displayed. Another annoying thing is the console window is displayed on top of my windows form and i cant do any other operations on that form. I have set all properties like CreateNoWindow = true,

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

but still it shows the console window. even i have redirected output and errors to seperate stream but no luck.

Is there any other way to hide the Console window? Please help me out .

Here is the part of my code i used to execute sc command.

Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.FileName = "sc";
proc.StartInfo.Arguments = string.Format(@"\\SYS25 create MySvc binPath= C:\mysvc.exe");
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();

解决方案

I had a similar issue when attempting to start a process without showing the console window. I tested with several different combinations of property values until I found one that exhibited the behavior I wanted.

Here is a page detailing why the UseShellExecute property must be set to false.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx

Under Remarks section on page:

If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created.

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fullPath;
startInfo.Arguments = args;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;

Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
try
{
    processTemp.Start();
}
catch (Exception e)
{
    throw;
}

这篇关于从的Process.Start C#隐藏控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆