如何在运行批处理文件时隐藏cmd窗口?如何在运行批处理文件时隐藏cmd窗口?
我用下面的代码来运行批处理文件
process = new Process();
process.StartInfo.FileName = batchFilePath;
process.Start();
如何在运行批处理文件时隐藏cmd窗口?如何在运行批处理文件时隐藏cmd窗口?
我用下面的代码来运行批处理文件
process = new Process();
process.StartInfo.FileName = batchFilePath;
process.Start();
如果proc.StartInfo.UseShellExecute是假,那么你正在启动过程中,可以使用:
proc.StartInfo.CreateNoWindow = true;
如果proc.StartInfo.UseShellExecute是真,那么OS是启动该过程,并且必须通过以下方式向过程提供“提示”:
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
但是, ed应用程序可能会忽略这后一个请求
如果使用UseShellExecute = 假,你可能要考虑重定向标准输出/错误,捕获产生的任何记录:
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;
}
的功能有一个良好的页面覆盖CreateNoWindow
这在an MSDN blog。
在Windows中还有一个错误,如果您传递用户名/密码,可能会抛出一个对话框并击败CreateNoWindow
。有关详细信息
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476 http://support.microsoft.com/?kbid=818858
用途: process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
有时候工作,其他人没有按“T。这是否取决于bat文件命令? – Ahmed 2009-07-08 08:18:56
按照Process properties,你有一个:
物业:
CreateNoWindow
注意:您可以静默运行一个命令行程序。 它不会闪烁控制台窗口。
和:
物业:
WindowStyle
注:使用此为隐藏设置窗口。 作者经常使用ProcessWindowStyle.Hidden
。
举一个例子!
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.
}
}
有时作品,其他作品不。这是否取决于bat文件命令? – Ahmed 2009-07-08 08:19:31
这是对我工作, 当您重定向所有的输入和输出,并设置隐藏窗口它应该工作
Process p = new Process();
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
有时作品,其他作品不。这是否取决于bat文件命令? – Ahmed 2009-07-08 08:17:48