개발노트/C#
[C#] cmd 명령어 실행결과 추출
by lovvepearl
2024. 3. 25.
//using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Diagnostics;
//using System.Drawing;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Windows.Forms;
ProcessStartInfo cmd = new ProcessStartInfo();
Process process = new Process();
cmd.FileName = @"cmd";
cmd.WindowStyle = ProcessWindowStyle.Hidden; // cmd창이 숨겨지도록 하기
cmd.CreateNoWindow = true; // cmd창을 띄우지 않도록 하기
cmd.UseShellExecute = false;
cmd.RedirectStandardOutput = true; // cmd창에서 데이터를 가져오기
cmd.RedirectStandardInput = true; // cmd창으로 데이터 보내기
cmd.RedirectStandardError = true; // cmd창에서 오류 내용 가져오기
process.EnableRaisingEvents = false;
process.StartInfo = cmd;
process.Start();
process.StandardInput.Write("{명령어}" + Environment.NewLine); // 명령어 입력 후 NewLine처리
process.StandardInput.Close();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
return result; //결과 출력