如何在运行批处理文件时隐藏 cmd 窗口? [英] How to hide cmd window while running a batch file?

查看:45
本文介绍了如何在运行批处理文件时隐藏 cmd 窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行批处理文件时隐藏 cmd 窗口?

How to hide cmd window while running a batch file?

我使用以下代码运行批处理文件

I use the following code to run batch file

process = new Process();
process.StartInfo.FileName = batchFilePath;
process.Start();

推荐答案

如果 proc.StartInfo.UseShellExecute 是 false,那么您正在启动进程并且可以使用:

If proc.StartInfo.UseShellExecute is false, then you are launching the process and can use:

proc.StartInfo.CreateNoWindow = true;

如果 proc.StartInfo.UseShellExecute 为 true,则操作系统正在启动该进程,您必须通过以下方式向该进程提供提示":

If proc.StartInfo.UseShellExecute is true, then the OS is launching the process and you have to provide a "hint" to the process via:

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

然而,被调用的应用程序可能会忽略后一个请求.

However the called application may ignore this latter request.

如果使用 UseShellExecute = false,您可能需要考虑重定向标准输出/错误,以捕获生成的任何日志记录:

If using UseShellExecute = false, you might want to consider redirecting standard output/error, to capture any logging produced:

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
proc.StartInfo.RedirectStandardError = true;
proc.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);

并有一个类似的功能

private void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
   if (!String.IsNullOrEmpty(outLine.Data)) // use the output outLine.Data somehow;
}

MSDN 博客.

There's a good page covering CreateNoWindow this on an MSDN blog.

Windows 中还有一个错误,如果您传递用户名/密码,它可能会抛出一个对话框并使 CreateNoWindow 失效.详情

There is also a bug in Windows which may throw a dialog and defeat CreateNoWindow if you are passing a username/password. For details

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476http://support.microsoft.com/?kbid=818858

这篇关于如何在运行批处理文件时隐藏 cmd 窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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