# 여러가지 프로그램을 한개 창에서 선택하여 실행시키는 프로그램 + (CMD 실행 기능)

2021. 1. 25. 08:24개발/C#

 

 

using System;

using System.Windows.Forms;

using System.Diagnostics;

namespace MagicBox

{

  public partial class Form1 : Form

  {

    public Form1()

    {

      InitializeComponent();

      listBox1.Items.Add("인터넷");

      listBox1.Items.Add("계산기");

      listBox1.Items.Add("CMD");

      listBox1.Items.Add("Microsoft Excel");

    }

    private void button1_Click(object sender, EventArgs e)

    {

      try

      {

        string a = listBox1.SelectedItem.ToString();

        if (a == null)

        {

          MessageBox.Show("아무 것도 선택되지 않았습니다!");

        }

        if (a == "계산기")

        {

          Process.Start("calc.exe");

        }

        if (a == "CMD")

        {

          Process.Start("cmd.exe");

        }

        if (a == "Microsoft Excel")

        {

          Process.Start("Excel.exe");

        }

        if (a == "인터넷")

        {

          Process.Start("explorer.exe", "http://www.naver.com");

        }

      }

      catch

      {

        MessageBox.Show("아무 것도 선택되지 않았습니다!");

      }

    }

    private void button2_Click(object sender, EventArgs e)

    {

      //Process.Start("cmd.exe", "/C" + " " + textBox2.Text);

     

      Process cmd = new Process();

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

      cmd.StartInfo.Arguments = "/C " + textBox1.Text;

      cmd.StartInfo.RedirectStandardOutput = true;

      cmd.StartInfo.UseShellExecute = false;

      cmd.Start();

      string txt = cmd.StandardOutput.ReadToEnd();

      textBox2.Text = txt;

    }

  }

}