Interaction.Shell
関連付けられたドキュメントでアプリケーションを開くことができないようです。(a) 関連するMSDN ページにはそのように記載されていません (ただし、PathName
パラメーターの例は誤解を招くようです)。(b)D:\abc.txt
存在する場合でも失敗します。
System.Diagnostics.Process
または、次のクラスを使用できます。
using (Process process = Process.Start(@"D:\abc.txt"))
{
int pid = process.Id;
// Whether you want for it to exit, depends on your needs. Your
// Interaction.Shell() call above suggests you don't. But then
// you need to be aware that "pid" might not be valid when you
// you look at it, because the process may already be gone.
// A problem that would also arise with Interaction.Shell.
// process.WaitForExit();
}
D:\abc.txt
が存在する必要があることに注意してくださいFileNotFoundException
。
更新本当に使用する必要がある場合Interaction.Shell
は、次を使用できます
int pid = Interaction.Shell(@"notepad.exe D:\abc.txt", false, -1);
個人Process
的には、一般に起動されたプロセスのより堅牢な処理を提供するクラスを使用します。この場合、どのプログラムがファイルに関連付けられているかを「知る」ことからも解放され.txt
ます (常に を使用したい場合を除くnotepad.exe
)。