using System;
using System.ComponentModel;
using System.Diagnostics;
.....
private void Button_Click(object sender, EventArgs e)
{
// process
Process myProcess = new Process();
try
{
// pathName
string pathName = @"C:\WINDOWS\notepad.exe"; 
// myProcess.StartInfo
myProcess.StartInfo.FileName = pathName;
myProcess.Start();
clearButton.Enabled = false; 
// waitForExit();
myProcess.WaitForExit();
msgLabel.Text = "Process End";
clearButton.Enabled = true;
myProcess.Close();
}
catch(Win32Exception exception)
{
msgLabel.Text += "Err";
if (exception.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
msgLabel.Text += "(ERROR_FILE_NOT_FOUND)";
} else if (exception.NativeErrorCode == ERROR_ACCESS_DENIED) {
msgLabel.Text += "(ERROR_ACCESS_DENIED)";
}
}
}转载于:https://blog.51cto.com/jammeswang/244970
本文展示了一个使用C#启动外部应用程序(记事本)的例子,并处理了可能遇到的错误情况,如文件未找到或权限不足等。
583

被折叠的 条评论
为什么被折叠?



