2010-09-18 57 views
9

對於一個項目,我正在構建一箇舊的批處理腳本系統的新前端。我必須使用Windows XP和C#與.Net。我不想觸摸這個過去十年製作的舊式後端系統。所以我的想法是啓動cmd.exe程序並在那裏執行Bash腳本。爲此,我將使用.Net中的「系統」功能。閱讀批處理腳本輸出到C#.Net程序

但我還需要將「批處理腳本命令行輸出」讀回到我的C#程序中。我可以將它重定向到一個文件中。但是必須有一種方法可以將標準輸出從CMD.exe轉換爲我的C#程序。

非常感謝!

+2

by'Bash',你的意思是'批'嗎? – 2010-09-18 14:49:56

+0

@W_P:bash是一個unix shell。 – 2010-09-18 14:51:19

+0

@Brian:我想W_P知道。 – 2010-09-18 15:32:04

回答

3

你的方式很好。但是最終你只能得到整個輸出。腳本運行時我想要輸出。所以在這裏,首先幾乎是一樣的,但是我扭曲了輸出。看看 如果有問題: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

public void execute(string workingDirectory, string command) 
{ 

    // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters. 
    // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit. 
    System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(workingDirectory + "\\" + "my.bat ", command); 

    procStartInfo.WorkingDirectory = workingDirectory; 

    //This means that it will be redirected to the Process.StandardOutput StreamReader. 
    procStartInfo.RedirectStandardOutput = true; 
    //This means that it will be redirected to the Process.StandardError StreamReader. (same as StdOutput) 
    procStartInfo.RedirectStandardError = true; 

    procStartInfo.UseShellExecute = false; 
    // Do not create the black window. 
    procStartInfo.CreateNoWindow = true; 
    // Now we create a process, assign its ProcessStartInfo and start it 
    System.Diagnostics.Process proc = new System.Diagnostics.Process(); 

    //This is importend, else some Events will not fire! 
    proc.EnableRaisingEvents = true; 

    // passing the Startinfo to the process 
    proc.StartInfo = procStartInfo; 

    // The given Funktion will be raised if the Process wants to print an output to consol      
    proc.OutputDataReceived += DoSomething; 
    // Std Error 
    proc.ErrorDataReceived += DoSomethingHorrible; 
    // If Batch File is finished this Event will be raised 
    proc.Exited += Exited; 
} 

東西是關閉的,但無論你的想法...

的DoSomething的是這樣的功能:

void DoSomething(object sendingProcess, DataReceivedEventArgs outLine); 
{ 
    string current = outLine.Data; 
} 

希望這有助於

+0

'BeginOutputReadLine()'也是需要的。 – 2017-04-25 15:14:27

8

鑑於更新的問題。以下是如何啓動cmd.exe來運行批處理文件並在C#應用程序中捕獲腳本的輸出。

var process = new Process(); 
var startinfo = new ProcessStartInfo("cmd.exe", @"/C c:\tools\hello.bat"); 
startinfo.RedirectStandardOutput = true; 
startinfo.UseShellExecute = false; 
process.StartInfo = startinfo; 
process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data); // do whatever processing you need to do in this handler 
process.Start(); 
process.BeginOutputReadLine(); 
process.WaitForExit(); 
2

您不能捕獲與system函數的輸出,你必須去深一點到子API。

using System; 
using System.Diagnostics; 
Process process = Process.Start(new ProcessStartInfo("bash", path_to_script) { 
            UseShellExecute = false, 
            RedirectStandardOutput = true 
           }); 
string output = process.StandardOutput.ReadToEnd(); 
process.WaitForExit(); 
if (process.ExitCode != 0) { … /*the process exited with an error code*/ } 

您似乎對是否嘗試運行bash腳本或批處理文件感到困惑。這些不是一回事。 Bash是一個unix shell,爲其存在多個Windows端口。 「批處理文件」是通常給cmd腳本的名稱。上面的代碼假定你想運行一個bash腳本。如果要運行cmd腳本,請將bash更改爲cmd。如果您想運行bash腳本並且bash.exe不在您的PATH上,請將bash更改爲bash.exe的完整路徑。

+0

對不起,我用來說bash,但我想Windows cmd批量skirpt – Thomas 2010-09-18 17:02:21

+0

坦克你,你的代碼工作很好。兩位:在第7行有一個支架。第二點:在path_to_script之後,您只需留下一條毯子,然後您可以將您的參數添加到腳本中。謝謝大家,我的老闆非常高興:D – Thomas 2010-09-20 10:08:29