目次

プロセスの作成と実行 - 外部アプリケーションの実行 - C#

C#でプロセスを作成し、開始するコードを紹介します。

UI

下図のUIを作成します。フォームにButtonを1つ配置します。

コード

下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProcessDemo
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      System.Diagnostics.Process proc = new System.Diagnostics.Process();
      proc.StartInfo.FileName = "notepad.exe";
      proc.StartInfo.Arguments = @"c:\windows\win.ini";
      proc.Start();
    }
  }
}

解説

System.Diagnostics.Process proc = new System.Diagnostics.Process();
にてプロセスクラスのインスタンスを作成します。

proc.StartInfo.FileName = "notepad.exe";
proc.StartInfo.Arguments = @"c:\windows\win.ini";
にて、プロセスで実行するプログラムのファイル名をStartInfo.FileNameに設定します。また、StartInfo.Argumentsには実行するプログラムの引数を設定します。

proc.Start();
プロセスを開始します。

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。


ボタンを押します。notepad.exeが実行されメモ帳のウィンドウが表示されます。また、引数に設定したファイル名が開かれファイルの内容がメモ帳に表示されます。


著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2012-08-21
iPentec all rights reserverd.