C#メモ 外部モジュールがコンソールに出力する文字列を取得してみる

外部プログラムとして呼び出す実行モジュールの文字列を調べたくなったので。

ポイントとしてはこんな感じ

  • ProcessStartInfoクラスでプロセス実行時の設定をする
  • プロセス実行時の設定は次のように設定する
    • UseShellExecuteプロパティをfalse
    • RedirectStandardOutputプロパティをtrue
  • Process.StandardOutput.ReadToEnd()でコンソールに出力した文字列を取得する

サンプルとして作ったプロジェクトはこんな感じ。
Outsideってプロジェクトは外部モジュールとして呼び出される方でProtoはOutsideのモジュールを呼び出すようにする。

とりあえず、実行モジュールとしてはこんな文字列を出力するものを準備した。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
static void Main(string[] arguments)
{
Console.WriteLine("Hello!!");
}
static void Main(string[] arguments) { Console.WriteLine("Hello!!"); }
static void Main(string[] arguments)
{
	Console.WriteLine("Hello!!");
}

んで、コードとしてはこんな感じ。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
static void Main(string[] arguments)
{
try
{
// 今回はコード直書きでOutsideプロジェクトでビルドして作った実行モジュールへの
// 相対パスをとおすことにする(とりあえずはDebugビルドが前提ってことで)
string filepath = @"..\..\..\Outside\bin\Debug\Outside.exe";
// ProcessStartInfoクラスで外部モジュール実行時の設定をする
ProcessStartInfo informations = new ProcessStartInfo();
informations.FileName = filepath;
informations.UseShellExecute = false;
informations.RedirectStandardOutput = true;
// 外部モジュールを実行する
Process process = Process.Start(informations);
// 外部モジュールがコンソールに出力した文字列を取得する
string buffer = process.StandardOutput.ReadToEnd();
// 取得できたかどうかをこの実行モジュールでコンソールに出力する
Console.WriteLine(buffer);
}
catch(Exception exception)
{
Console.WriteLine(exception.Message);
}
finally
{
// 何もしないとすぐにプロセスが終了するのでダミーのキー入力を入れて一時停止するようにしとく
Console.ReadKey();
}
}
static void Main(string[] arguments) { try { // 今回はコード直書きでOutsideプロジェクトでビルドして作った実行モジュールへの // 相対パスをとおすことにする(とりあえずはDebugビルドが前提ってことで) string filepath = @"..\..\..\Outside\bin\Debug\Outside.exe"; // ProcessStartInfoクラスで外部モジュール実行時の設定をする ProcessStartInfo informations = new ProcessStartInfo(); informations.FileName = filepath; informations.UseShellExecute = false; informations.RedirectStandardOutput = true; // 外部モジュールを実行する Process process = Process.Start(informations); // 外部モジュールがコンソールに出力した文字列を取得する string buffer = process.StandardOutput.ReadToEnd(); // 取得できたかどうかをこの実行モジュールでコンソールに出力する Console.WriteLine(buffer); } catch(Exception exception) { Console.WriteLine(exception.Message); } finally { // 何もしないとすぐにプロセスが終了するのでダミーのキー入力を入れて一時停止するようにしとく Console.ReadKey(); } }
static void Main(string[] arguments)
{
	try
	{
		// 今回はコード直書きでOutsideプロジェクトでビルドして作った実行モジュールへの
		// 相対パスをとおすことにする(とりあえずはDebugビルドが前提ってことで)
		string filepath = @"..\..\..\Outside\bin\Debug\Outside.exe";
		// ProcessStartInfoクラスで外部モジュール実行時の設定をする
		ProcessStartInfo informations = new ProcessStartInfo();
		informations.FileName = filepath;
		informations.UseShellExecute = false;
		informations.RedirectStandardOutput = true;
		// 外部モジュールを実行する
		Process process = Process.Start(informations);
		// 外部モジュールがコンソールに出力した文字列を取得する
		string buffer = process.StandardOutput.ReadToEnd();
		// 取得できたかどうかをこの実行モジュールでコンソールに出力する
		Console.WriteLine(buffer);
	}
	catch(Exception exception)
	{
		Console.WriteLine(exception.Message);
	}
	finally
	{
		// 何もしないとすぐにプロセスが終了するのでダミーのキー入力を入れて一時停止するようにしとく
		Console.ReadKey();
	}
}

OutsideプロジェクトをDビルドしてからProtoプロジェクトを実行してみるとこんな感じ。
一応、文字列を読み取ってくれとる。

んまま、自己満足ってことで。