ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C# :: (펌)외부 응용프로그램 실행하기 (Process.Start 메서드) - CMD Application
    컴퓨터&모바일/Windows 2017. 5. 16. 16:00

    윈도우를 종료
    System.Diagnostics.Process.Start("cmd.exe","ShutDown.exe -s -f -t 00");


    윈도우를 재부팅

    System.Diagnostics.Process.Start("cmd.exe","ShutDown.exe -r -f -t 00");

     

    특정 폴더 열기
    System.Diagnostics.Process.Start("explorer.exe", "C:\Temp");

    특정 사이트 열기
    System.Diagnostics.Process.Start("explorer.exe", "http://www.naver.com");

     

    도스명령어 실행

    System.Diagnostics.Process.Start("cmd.exe","/c dir");

    // cmd 옵션에 대해 더 알고싶으면.. c:>help cmd

     

    Process.Start 메서드 사용형식
     

    using System.Diagnostics;

    //System.Diagnostics 네임스페이스는 시스템 프로세스, 이벤트 로그 및 성능 카운터와 상호 작용할 수 있는 클래스를 제공합니다.

     

    public bool Start();
    //이 Process 구성 요소의 StartInfo 속성으로 지정된 프로세스 리소스를 시작하거나 다시 사용하여 구성 요소에 연결합니다.

     

    Process myProcess = new Process();
    string myDocumentsPath =   Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    myProcess.StartInfo.FileName = myDocumentsPath + "
    \MyFile.doc";
    myProcess.StartInfo.Verb = "Print";
    myProcess.StartInfo.CreateNoWindow = true;
    myProcess.Start();

     

    public static Process Start( ProcessStartInfo startInfo);
    // ProcessStartInfo : 파일 이름 및 모든 명령줄 인수를 포함하여 프로세스를 시작하는 데 사용되는 정보
    // 시작할 프로세스의 파일 이름 같은 프로세스 시작 정보가 포함된 매개 변수에 의해 지정된
    // 프로세스 리소스를 시작하고 해당 리소스를 새 Process 구성 요소에 연결합니다

     

    ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
    startInfo.WindowStyle = ProcessWindowStyle.Minimized;
    startInfo.Arguments = "
    www.naver.com";
    Process.Start(startInfo);

     

    public static Process Start(string fileName);
    // fileName : 프로세스에서 실행될 응용 프로그램 파일 이름입니다.

    //문서 또는 응용 프로그램 파일 이름을 지정하여 프로세스 리소스를 시작하고 해당 리소스를 새 Process 구성 요소에 연결합니다

     

    Process.Start("IExplore.exe");

     

    public static Process Start(string fileName, string arguments);
    // arguments : 프로세스를 시작할 때 전달할 명령줄 인수입니다

    //응용 프로그램 이름 및 명령줄 인수 집합을 지정하여 프로세스 리소스를 시작하고 해당 리소스를 새 Process 구성 요소에 연결합니다.

     

    Process.Start("IExplore.exe", "C:\myPath\myFile.htm");
    Process.Start("IExplore.exe", "C:\myPath\myFile.asp");

     

     

    Process 클래스

    Process 구성 요소는 컴퓨터에서 실행 중인 프로세스에 대한 액세스를 제공합니다. 간단히 말해 프로세스란 실행 중인 응용 프로그램을 말합니다.

     

    Process 구성 요소는 응용 프로그램의 시작, 중지, 제어 및 모니터링을 위한 유용한 도구입니다.
    Process 구성 요소를 사용하면 실행 중인 프로세스의 목록을 얻거나 새로운 프로세스를 시작할 수 있습니다. 또한 Process 구성 요소를 사용하여 시스템 프로세스에도 액세스할 수 있습니다.
    Process 구성 요소를 초기화한 후에는 해당 구성 요소를 사용하여 실행 중인 프로세스에 대한 정보를 얻을 수 있으며 그러한 정보에는 스레드 집합, 로드된 모듈(.dll 및 .exe 파일), 프로세스가 사용하고 있는 메모리 양과 같은 성능 정보 등이 포함됩니다.

     

    프로세스 구성 요소는 속성 그룹에 대한 정보를 한 번에 가져옵니다. Process 구성 요소가 특정 그룹의 한 멤버에 대한 정보를 가져올 때 해당 그룹의 나머지 속성 값이 캐싱되므로 Refresh 메서드를 호출하지 않는 한 그룹의 다른 멤버에 대한 새로운 정보를 가져오지 않습니다. 따라서 속성 값이 Refresh 메서드를 마지막으로 호출하여 얻은 속성 값과 같을 수 있습니다. 이러한 그룹 명세는 운영 체제에 따라 다릅니다.

     

    더 자세한 사항은 Microsoft Visual Studio .NET 2003 도움말에서 Process 클래스를 참고하세요.

     

     

    도스명령수행 프로그램 


     

    [전체소스]


    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

    using System.Diagnostics;

    using System.IO;

    using System.Threading;


    namespace mouseEvents

    {


        public class Form1 : System.Windows.Forms.Form

        {

            private System.Windows.Forms.TextBox textBox1;

            private System.Windows.Forms.Label label2;

            private System.ComponentModel.IContainer components;

            private System.Windows.Forms.TextBox tbComm;

            private System.Windows.Forms.Button btnExec;



            public Form1()

            {

                InitializeComponent();

            }


            protected override void Dispose( bool disposing )

            {

                if( disposing )

                {

                    if (components != null)

                    {

                        components.Dispose();

                    }

                }

                base.Dispose( disposing );

            }


            #region Windows Form Designer generated code

            /// <summary>

            /// Required method for Designer support - do not modify

            /// the contents of this method with the code editor.

            /// </summary>

            private void InitializeComponent()

            {

                this.label2 = new System.Windows.Forms.Label();

                this.tbComm = new System.Windows.Forms.TextBox();

                this.btnExec = new System.Windows.Forms.Button();

                this.textBox1 = new System.Windows.Forms.TextBox();

                this.SuspendLayout();

                //

                // label2

                //

                this.label2.Location = new System.Drawing.Point(8, 8);

                this.label2.Name = "label2";

                this.label2.Size = new System.Drawing.Size(72, 17);

                this.label2.TabIndex = 5;

                this.label2.Text = "도스명령어:";

                //

                // tbComm

                //

                this.tbComm.Location = new System.Drawing.Point(80, 8);

                this.tbComm.Name = "tbComm";

                this.tbComm.Size = new System.Drawing.Size(355, 21);

                this.tbComm.TabIndex = 6;

                this.tbComm.Text = "";

                //

                // btnExec

                //

                this.btnExec.Location = new System.Drawing.Point(440, 8);

                this.btnExec.Name = "btnExec";

                this.btnExec.Size = new System.Drawing.Size(104, 25);

                this.btnExec.TabIndex = 1;

                this.btnExec.Text = "실행";

                this.btnExec.Click += new System.EventHandler(this.button2_Click);

                //

                // textBox1

                //

                this.textBox1.AcceptsReturn = true;

                this.textBox1.AcceptsTab = true;

                this.textBox1.Location = new System.Drawing.Point(8, 32);

                this.textBox1.Multiline = true;

                this.textBox1.Name = "textBox1";

                this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;

                this.textBox1.Size = new System.Drawing.Size(528, 312);

                this.textBox1.TabIndex = 2;

                this.textBox1.Text = "";

                //

                // Form1

                //

                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

                this.ClientSize = new System.Drawing.Size(547, 350);

                this.Controls.Add(this.tbComm);

                this.Controls.Add(this.label2);

                this.Controls.Add(this.textBox1);

                this.Controls.Add(this.btnExec);

                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

                this.MaximizeBox = false;

                this.Name = "Form1";

                this.Text = "Process Class";

                this.ResumeLayout(false);


            }

            #endregion


            [STAThread]

            static void Main()

            {

                Application.Run(new Form1());

            }


            private void button2_Click(object sender, System.EventArgs e)

            {

                this.start();

            }


            private void start()

            {


                //Network 변수

                StreamWriter DosWriter;

                StreamReader DosRedaer;

                StreamReader ErrorReader;

               

                //프로세스 생성및 초기화

                Process DosPr = new Process();

               

                ProcessStartInfo psI = new ProcessStartInfo("cmd");

                psI.UseShellExecute = false;

                psI.RedirectStandardInput = true;

                psI.RedirectStandardOutput = true;

                psI.RedirectStandardError = true;

                psI.CreateNoWindow = true;



                //명령 실행

                DosPr.StartInfo = psI;

                DosPr.Start();

                DosWriter = DosPr.StandardInput;

                DosRedaer = DosPr.StandardOutput;

                ErrorReader = DosPr.StandardError;


                DosWriter.AutoFlush = true;


                DosWriter.WriteLine(tbComm.Text);

               

                DosWriter.Close();


                //출력

                textBox1.Text = DosRedaer.ReadToEnd();

                textBox1.Text += ErrorReader.ReadToEnd();


            }

        }

    }



      작성자 : HOONS(박경훈)
      이메일 : tajopkh@hanmail.net

    출처 : http://www.cyworld.com/nowy/192742

    댓글

Designed by Tistory.