0

次のコードを使用して C# からバッチ ファイルを実行しようとしましたが、結果を WPF テキスト ボックスに表示したいと考えています。これを行う方法を教えてください。

using System;

namespace Learn
{
    class cmdShell
    {
        [STAThread]  // Lets main know that multiple threads are involved.
        static void Main(string[] args)
        {
            System.Diagnostics.Process proc; // Declare New Process
            proc = System.Diagnostics.Process.Start("C:\\listfiles.bat"); // run test.bat from command line.
            proc.WaitForExit(); // Waits for the process to end.
        }
    }
}

このバッチ ファイルは、フォルダーからファイルを一覧表示するためのものです。バッチが実行されると、結果がテキストボックスに表示されます。バッチ ファイルに複数のコマンドがある場合は、各コマンドの結果が textbox に表示されます。

4

2 に答える 2

2

標準出力ストリームをリダイレクトする必要があります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = "test.bat";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            string output = proc.StandardOutput.ReadToEnd();
            Console.WriteLine(output); // or do something else with the output
            proc.WaitForExit();
            Console.ReadKey();
        }
    }
}
于 2013-10-24T11:07:27.753 に答える