如何在運行批處理文件時隱藏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