글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)
(시리즈 글이 6개 있습니다.)
.NET Framework: 480. C# - 배치 파일 실행하고 출력 결과를 얻는 방법
; https://www.sysnet.pe.kr/2/0/1810
.NET Framework: 602. Process.Start의 cmd.exe에서 stdin만 redirect 하는 방법
; https://www.sysnet.pe.kr/2/0/11029
.NET Framework: 817. Process.Start로 실행한 콘솔 프로그램의 출력 결과를 얻는 방법
; https://www.sysnet.pe.kr/2/0/11870
Linux: 24. Linux/C# - 실행 파일이 아닌 스크립트 형식의 명령어를 Process.Start로 실행하는 방법
; https://www.sysnet.pe.kr/2/0/12073
닷넷: 2292. C# - 자식 프로세스의 출력이 4,096보다 많은 경우 Process.WaitForExit 호출 시 hang 현상
; https://www.sysnet.pe.kr/2/0/13707
닷넷: 2314. C# - ProcessStartInfo 타입의 Arguments와 ArgumentList
; https://www.sysnet.pe.kr/2/0/13876
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;
}
}
}
(첨부 파일은 이 글의 예제 프로젝트를 포함합니다.)
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]
[연관 글]
... 196 197 198 [199]
... 196 197 198 [199]