他のexeファイルを実行する - C#
ほかのexeファイルを実行する場合にはProcessクラスを用います。
ProcessクラスはSystem.Diagnostics名前空間にあります。
プログラム
UI
下図のUIを作成します。フォームにボタンを2つ配置します。
コード
下記のコードを記述します。
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;
using System.Diagnostics;
namespace ExecuteExeFile
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("notepad.exe");
}
private void button2_Click(object sender, EventArgs e)
{
Process.Start("notepad.exe", @"C:\Windows\system.ini");
}
}
}
解説
上記のコードでは、button1をクリックするとメモ帳(notepad.exe)を起動します。button2をクリックするとC:\Windows\system.iniを起動オプションに設定してメモ帳(notepad.exe)を起動します。
実行結果
プロジェクトを実行します。下図のウィンドウが表示されます。
[button1]をクリックします。メモ帳が起動します。
[button2]をクリックします。system.iniファイルが開かれた状態で、メモ帳が起動します。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用