在 C# 应用程序中隐藏命令窗口
在你说这是一个重复的问题之前,请让我解释一下(因为我已经阅读了所有类似的线程)。
我的应用程序具有这两种设置:
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
并且还具有 WindowsApplication 作为输出类型。
当我调用命令行命令时,黑色窗口仍然出现。我还能做些什么来隐藏窗口吗?并非所有命令都会发生这种情况,XCOPY 是黑色窗口确实闪烁的情况。只有当我进行 XCOPY 的目标也已包含该文件并且它会提示我是否要替换它时,才会发生这种情况。即使我传入 /Y 它仍然会短暂闪烁。
如果有帮助的话,我愿意使用 vbscript,但是还有其他想法吗?
客户端将调用我的可执行文件,然后传入命令行命令,即:
C:\MyProgram.exe start XCOPY c:\Test.txt c:\ProgramFiles\
以下是应用程序的完整代码:
class Program
{
static void Main(string[] args)
{
string command = GetCommandLineArugments(args);
// /c tells cmd that we want it to execute the command that follows and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = procStartInfo;
process.Start();
}
private static string GetCommandLineArugments(string[] args)
{
string retVal = string.Empty;
foreach (string arg in args)
retVal += " " + arg;
return retVal;
}
}
Before you say its a duplicate question, please let me explain (as I've read all similar threads).
My application has both of these settings:
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
and is also has WindowsApplication as the output type.
The black window STILL comes up when I call a command line command. Is there anything else I can do to hide the window? It doesn't happen for all commands, XCOPY is a situation where it the black window does flash up. This only happens though when the destination I'm XCOPYing too already contains the file and it's prompting me if I want to replace it. Even if I pass in /Y it will still flash briefly.
I'm open to using vbscript if that will help, but any other ideas?
The client will call my executable and then pass in a command line command ie:
C:\MyProgram.exe start XCOPY c:\Test.txt c:\ProgramFiles\
Here's the full code of the application:
class Program
{
static void Main(string[] args)
{
string command = GetCommandLineArugments(args);
// /c tells cmd that we want it to execute the command that follows and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = procStartInfo;
process.Start();
}
private static string GetCommandLineArugments(string[] args)
{
string retVal = string.Empty;
foreach (string arg in args)
retVal += " " + arg;
return retVal;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我看到您正在调用 cmd ,然后将命令作为参数传递。而是直接调用命令,
例如
i see that you are calling cmd and then passing the command as parameters. Instead call the command directly
e.g.
您可以尝试添加
到您的流程中
You can try adding
to your process
如果您在 C# 代码中调用
cmd.exe
并通过标准input.WriteLine
将命令传递给它,并且您不希望每次都弹出 CMD 窗口当你运行你的代码时,你可以简单地编写这个命令:通过将 不创建窗口 设置为 false,我们将在后台运行发送到 CMD 的命令,并且不会向用户显示输出。通过将其设置为 false,会弹出 CMD 窗口。
If you are calling
cmd.exe
in your C# code and passing the commands to it via standardinput.WriteLine
and you don't want your CMD window to pop up every time you run your code, you can simply write this command:By setting create no window to false, we are running the command sent to the CMD in the background and the output is not being displayed to the user. By setting it to false, the CMD window pops up.
我有一个类似的任务 - 可以通过 API 调用创建后隐藏窗口。
(在您的情况下,您可能需要一个辅助线程。)
如果您知道新窗口的句柄,您可以调用
0 隐藏窗口,1 显示窗口
要获取窗口的句柄,请看一下:
pinvoke.net enumwindows(user32)
I had a similar task - It is possible to hide the window after creation via an API call.
(In your case you maybe need a helper thread.)
If you know the handle of the new Window you can call
0 hides the window, 1 shows the window
To get the handle of the Window take a look at:
pinvoke.net enumwindows(user32)
问题是您正在使用 cmd.exe。仅其控制台窗口将被隐藏,而不是您要求其启动的进程的控制台窗口。使用 cmd.exe 没有什么意义,除非您试图执行它本身实现的一些命令。就像复制一样。
如果您需要 cmd.exe,您仍然可以隐藏该窗口,您必须使用 /B 选项来启动。输入开始/?在命令提示符下查看选项。并不是说它有帮助,您不能使用 START COPY。
xcopy.exe 中有一个特定的怪癖,也可能会让您感到困惑。如果您不重定向输入,它不会执行。它只是无法在没有诊断的情况下运行。
The problem is that you're using cmd.exe. Only its console window will be hidden, not the console window for the process you ask it to start. There's little point in using cmd.exe, unless you are trying to execute some of the commands it implements itself. Like COPY.
You can still suppress the window if you need cmd.exe, you'll have to use the /B option for Start. Type start /? at the command prompt to see options. Not that it helps, you can't use START COPY.
There's a specific quirk in xcopy.exe that might throw you off as well. It does not execute if you don't also redirect the input. It just fails to run without diagnostic.