■ Process 클래스 : BeginOutputReadLine 메소드를 사용해 도스 명령 실행 결과 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Diagnostics;

using System.Text;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 결과 문자열 빌더

        /// </summary>

        private StringBuilder resultStringBuilder = new StringBuilder();

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.runButton.Click += runButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 프로세스 출력 데이터 수신시 처리하기 - process_OutputDataReceived(sender, e)

 

        /// <summary>

        /// 프로세스 출력 데이터 수신시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)

        {

            this.resultStringBuilder.AppendLine(e.Data);

        }

 

        #endregion

        #region 실행 버튼 클릭시 처리하기 - runButton_Click(sender, e)

 

        /// <summary>

        /// 실행 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void runButton_Click(object sender, EventArgs e)

        {

            this.resultStringBuilder.Clear();

 

            string dosCommand = this.dosCommandTextBox.Text.Trim();

 

            Process process = GetProcess(dosCommand);

 

            process.OutputDataReceived += process_OutputDataReceived;

 

            process.Start();

 

            process.BeginOutputReadLine();

 

            process.WaitForExit();

 

            this.resultTextBox.Text = this.resultStringBuilder.ToString();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 프로세스 구하기 - GetProcess(dosCommand)

 

        /// <summary>

        /// 프로세스 구하기

        /// </summary>

        /// <param name="dosCommand">DOS 명령</param>

        /// <returns>프로세스</returns>

        private Process GetProcess(string dosCommand)

        {

            Process process = new Process();

 

            process.EnableRaisingEvents              = true;

            process.StartInfo.FileName               = "cmd.exe";

            process.StartInfo.Arguments              = string.Format("/C {0}", dosCommand);

            process.StartInfo.WindowStyle            = ProcessWindowStyle.Minimized;

            process.StartInfo.CreateNoWindow         = true;

            process.StartInfo.UseShellExecute        = false;

            process.StartInfo.RedirectStandardOutput = true;

 

            return process;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요