2010-09-18 32 views
9

Đối với một dự án, tôi đang xây dựng giao diện người dùng mới cho Hệ thống tập lệnh Batch cũ. Tôi phải sử dụng Windows XP và C# với .Net. Tôi không muốn chạm vào hệ thống Backend cũ này vì nó được tạo ra trong thập kỷ vừa qua. Ý tưởng của tôi là bắt đầu chương trình cmd.exe và thực thi kịch bản Bash trong đó. Đối với điều này tôi sẽ sử dụng chức năng "hệ thống" trong .Net.Đọc đầu ra tập lệnh Batch shell vào C# .Net Program

Nhưng tôi cũng cần đọc "Dòng lệnh kịch bản lệnh Batch" trở lại Chương trình C# của tôi. Tôi có thể chuyển hướng nó vào một tập tin. Nhưng phải có cách để có được Đầu ra Chuẩn từ CMD.exe vào Chương trình C# của tôi.

Cảm ơn bạn rất nhiều!

+2

bởi 'Bash', ý của bạn là' Lô'? –

+0

@W_P: bash là một vỏ unix. –

+0

@Brian: Tôi nghĩ W_P biết điều đó. –

Trả lời

3

Cách của bạn tốt. Nhưng bạn chỉ nhận được toàn bộ Đầu ra ở cuối. Tôi muốn đầu ra khi kịch bản đang chạy. Vì vậy, ở đây nó là, đầu tiên khá nhiều giống nhau, nhưng hơn tôi twised đầu ra. Nếu bạn có vấn đề xem xét: 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; 
} 

Something tắt, nhưng bất cứ điều gì bạn có được những ý tưởng ...

Các DoSomething là chức năng này:

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

Hope this helps

+0

'BeginOutputReadLine()' cũng là cần thiết. –

8

Cho câu hỏi được cập nhật. Đây là cách bạn có thể khởi chạy cmd.exe để chạy một tệp lô và nắm bắt đầu ra của tập lệnh trong ứng dụng 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

Bạn không thể chụp đầu ra với chức năng system, bạn phải đi sâu hơn một chút vào API phụ.

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*/ } 

Bạn dường như bị nhầm lẫn về việc bạn đang cố chạy tập lệnh bash hay tệp bó. Đây không phải là điều tương tự. Bash là một vỏ unix, có một số cổng Windows tồn tại. “Tệp hàng loạt” là tên thường được cung cấp cho tập lệnh cmd. Đoạn mã trên giả định rằng bạn muốn chạy một tập lệnh bash. Nếu bạn muốn chạy tập lệnh cmd, hãy thay đổi bash thành cmd. Nếu bạn muốn chạy tập lệnh bash và bash.exe không có trên số PATH của bạn, hãy thay đổi bash thành đường dẫn đầy đủ đến bash.exe.

+0

xin lỗi, tôi đã từng nói bash nhưng tôi muốn Windows cmd Batch skript – Thomas

+0

giúp bạn, mã của bạn hoạt động tốt. Hai bit: Tại dòng 7 có một khung đến nhiều. Thứ hai bit: sau khi path_to_script bạn chỉ cần để lại một tấm chăn và bạn có thể thêm đối số của bạn vào kịch bản. Cảm ơn tất cả các bạn rất nhiều, ông chủ của tôi rất hạnh phúc: D – Thomas

Các vấn đề liên quan