C# 如何在运行批处理文件时隐藏 cmd 窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1096591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide cmd window while running a batch file?
提问by Ahmed Atia
How to hide cmd window while running a batch file?
如何在运行批处理文件时隐藏 cmd 窗口?
I use the following code to run batch file
我使用以下代码运行批处理文件
process = new Process();
process.StartInfo.FileName = batchFilePath;
process.Start();
采纳答案by Joel Goodwin
If proc.StartInfo.UseShellExecute is false, then you are launching the process and can use:
如果 proc.StartInfo.UseShellExecute 为false,则您正在启动该进程并可以使用:
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.UseShellExecute 为true,则操作系统正在启动该进程,您必须通过以下方式向该进程提供“提示”:
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:
如果使用 UseShellExecute = false,您可能需要考虑重定向标准输出/错误,以捕获生成的任何日志记录:
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.
MSDN 博客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
Windows 中还有一个错误,CreateNoWindow
如果您传递用户名/密码,它可能会抛出一个对话框并失败。详情
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476http://support.microsoft.com/?kbid=818858
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476 http://support.microsoft.com/?kbid=818858
回答by kaizen
Use: process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
使用:process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
回答by VonC
According to the Process properties, you do have a:
根据Process 属性,您确实有:
Property:
CreateNoWindow
Notes: Allows you to run a command line program silently. It does not flash a console window.
属性:
CreateNoWindow
注: 允许您静默运行命令行程序。它不会闪烁控制台窗口。
and:
和:
Property:
WindowStyle
Notes: Use this to set windows as hidden. The author has usedProcessWindowStyle.Hidden
often.
属性:
WindowStyle
注释: 使用它可以将窗口设置为隐藏。作者ProcessWindowStyle.Hidden
经常使用。
As an example!
举个例子!
static void LaunchCommandLineApp()
{
// For the example
const string ex1 = "C:\";
const string ex2 = "C:\Dir";
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
回答by Mnyikka
This is what worked for me, When you redirect all of the input and output, and set the window hidden it should work
这对我有用,当您重定向所有输入和输出,并将窗口设置为隐藏时,它应该可以工作
Process p = new Process();
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;