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

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

问题描述

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

我用下面的code运行批处理文件

  =进程新的Process();
process.StartInfo.FileName = batchFilePath;
的Process.Start();


解决方案

如果proc.StartInfo.UseShellExecute为,然后你正在启动的过程中,可以使用:

  proc.StartInfo.CreateNoWindow = TRUE;

如果proc.StartInfo.UseShellExecute为真正,然后在操作系统启动过程中,你必须通过提供一个暗示的过程:

  proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

然而,所谓的应用程序可能忽略后者的要求。

如果使用UseShellExecute = ,你可能要考虑重定向标准输出/错误,来捕获产生的任何记录:

  proc.StartInfo.UseShellExecute = FALSE;
proc.StartInfo.RedirectStandardOutput =真;
proc.OutputDataReceived + =新DataReceivedEventHandler(ProcessOutputHandler);
proc.StartInfo.RedirectStandardError = TRUE;
proc.ErrorDataReceived + =新DataReceivedEventHandler(ProcessOutputHandler);

和有像

函数

 私人无效ProcessOutputHandler(对象sendingProcess,DataReceivedEventArgs轮廓)
{
   如果//使用输出outLine.Data某种方式(String.IsNullOrEmpty(outLine.Data)!);
}

有覆盖良好的网页 CreateNoWindow 本上的在MSDN博客

有也是在Windows可能抛出一个对话和失败中的错误 CreateNoWindow 如果你传递一个用户名/密码。有关详情

<一个href=\"http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476\">http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476
http://support.microsoft.com/?kbid=818858

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

解决方案

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

proc.StartInfo.CreateNoWindow = 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.

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

And have a function like

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

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

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=98476 http://support.microsoft.com/?kbid=818858

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

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