从进程中隐藏控制台窗口。开始c#

时间:2022-08-29 14:45:26

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,

我正在尝试在一个使用System.Diagnostics的远程机器上创建进程。流程类。我能创造一个过程。但问题是,创建服务需要花费很长时间,并且显示了控制台窗口。另一个恼人的事情是控制台窗口显示在我的windows窗体上,我不能做任何其他的操作。我设置了所有的属性,比如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.

这里是我用来执行sc命令的代码的一部分。

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();

3 个解决方案

#1


86  

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

这里有一页详细说明为什么UseShellExecute属性必须设置为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.

如果UseShellExecute属性为真,或用户名和密码属性不为空,则CreateNoWindow属性值被忽略,并创建一个新窗口。

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;
}

#2


38  

I've had bad luck with this answer, with the process (Wix light.exe) essentially going out to lunch and not coming home in time for dinner. However, the following worked well for me:

我在这个问题上遇到了坏运气,这个过程(Wix light.exe)基本上是出去吃午饭,而不是按时回家吃晚饭。然而,下面的工作对我来说很有效:

Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// etc, then start process

#3


10  

This should work, try;

这应该工作,尝试;

Add a System Reference.

添加一个系统参考。

using System.Diagnostics;

Then use this code to run your command in a hiden CMD Window.

然后使用此代码在hiden CMD窗口中运行您的命令。

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.Arguments = "Enter your command here";
cmd.Start();

#1


86  

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

这里有一页详细说明为什么UseShellExecute属性必须设置为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.

如果UseShellExecute属性为真,或用户名和密码属性不为空,则CreateNoWindow属性值被忽略,并创建一个新窗口。

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;
}

#2


38  

I've had bad luck with this answer, with the process (Wix light.exe) essentially going out to lunch and not coming home in time for dinner. However, the following worked well for me:

我在这个问题上遇到了坏运气,这个过程(Wix light.exe)基本上是出去吃午饭,而不是按时回家吃晚饭。然而,下面的工作对我来说很有效:

Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// etc, then start process

#3


10  

This should work, try;

这应该工作,尝试;

Add a System Reference.

添加一个系统参考。

using System.Diagnostics;

Then use this code to run your command in a hiden CMD Window.

然后使用此代码在hiden CMD窗口中运行您的命令。

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.Arguments = "Enter your command here";
cmd.Start();