我有一個批處理文件,基本上從站點下載文件或返回錯誤消息說文件不可用。我想在C#中執行這個.bat文件(SSIS腳本組件),並將文件名保存到變量中(如果文件已下載)或將錯誤消息分配給變量。在C#中執行批處理文件#
預先感謝您
我有一個批處理文件,基本上從站點下載文件或返回錯誤消息說文件不可用。我想在C#中執行這個.bat文件(SSIS腳本組件),並將文件名保存到變量中(如果文件已下載)或將錯誤消息分配給變量。在C#中執行批處理文件#
預先感謝您
可以使用的System.Diagnostics.Process與/ C選項運行的Cmd.exe。提供批處理文件的名稱和基本在那裏。爲了簡化包裝過程的創建和捕獲標準輸入和輸出,我建議您查看前段時間我合併的ProcessRunner類。如果不是至少讀過How to use System.Diagnostics.Process correctly這篇文章。
此外,您可以在同一個庫中查看ScriptRunner類,該庫創建一個運行幾種不同類型Windows OS腳本文件的包裝器。
執行使用Process.Start批處理文件。您可以通過讀取返回的Process對象上的StandardError和StandardOutput流來獲取文本的輸出。您需要將RedirectStandardOutput
屬性設置爲true。
下面是從MSDN的例子:
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "yourbatchfile.bat";
p.Start();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
的ProcessStartInfo的StartInfo; 過程batchExecute; 的StartInfo =新的ProcessStartInfo(@ 「C:\測試\ test.bat的」); startInfo.WorkingDirectory = @ 「C:\測試」 ; startInfo.CreateNoWindow = TRUE; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.UseShellExecute = TRUE; batchExecute =新工藝(); batchExecute.StartInfo = StartInfo的; batchExecute.Start(); batchExecute .WaitForExit();我正在使用休閒代碼,但如何將輸出存儲到變量? – 2011-01-10 17:09:28
爲什麼不直接在SSIS中下載? – 2011-01-10 17:06:07
即要求... :( – 2011-01-10 17:07:44