|
如果你要運行一個命令行程序,或者打開一個windows應用程序,或者打開默認的web瀏覽器或email客戶端,你應該如何在你的C#代碼中實現(xiàn)這個功能呢?
以下這些例子完成相同的任務,你可以使用System.Diagnostics.Process中的類和方法完成這些任務,甚至作的更多。 例1:不管輸出結果,僅僅是運行一個命令行程序:
private void simpleRun_Click(object sender, System.EventArgs e){ System.Diagnostics.Process.Start(@"C:\listfiles.bat"); }
例2. 得到程序運行結果等待直到程序中止(同步運行程序)
private void runSyncAndGetResults_Click(object sender, System.EventArgs e){
System.Diagnostics.ProcessStartInfo psi =new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
this.processResults.Text = output;
}
}
例3. 使用用戶機器里的默認瀏覽器顯示URL
private void launchURL_Click(object sender, System.EventArgs e){
string targetURL = @http://www.;
System.Diagnostics.Process.Start(targetURL);
}
|