Process.Start로 실행한 콘솔 프로그램의 출력 결과를 얻는 방법
이미 아래의 글에서 쓴 적이 있지만,
C# - 배치 파일 실행하고 출력 결과를 얻는 방법
; https://www.sysnet.pe.kr/2/0/1810
배치 파일이 아닌, 콘솔 프로그램이라면 "cmd.exe"를 경유하지 않고 곧바로 exe를 FileName에 적용하면 됩니다.
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetOutput("tasklist.exe", ""));
// int pid = Process.GetCurrentProcess().Id;
// Console.WriteLine(GetOutput("tasklist.exe", $"/FI \"PID eq {pid}\""));
}
private static string GetOutput(string executable, string arguments)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = executable;
psi.Arguments = arguments;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process proc = Process.Start(psi);
string txt = proc.StandardOutput.ReadToEnd();
return txt;
}
}
}
(첨부 파일은 이 글의 예제 프로젝트를 포함합니다.)
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]