先引用命名空間:using System.Diagnostics;
小例:
//在現有視窗中開啟baidu
System.Diagnostics.Process.Start("http://www.baidu.com");
//在新視窗中開啟baidu
using System.Diagnostics;
Process ps=new Process();
string yourURL="http://www.baidu.com";
ps.StartInfo.FileName="iexplore.exe";
ps.StartInfo.Arguments=yourURL;
ps.Start();
1.process類的使用
Start 啟動進程資源將其與process類關聯
Kill立即關閉進程
waitforExit 在等待關聯進程的退出
Close 釋放與此關聯的所有進程
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2007-6-17
* Time: 16:20
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
//process類的名空間
using System.Diagnostics;
namespace process
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
//啟動IE首頁http://www.baidu.com/
void Button1Click(object sender, System.EventArgs e)
{
Process.Start("IExplore.exe","http://www.baidu.com/");
}
//啟動資源管理員
void Button2Click(object sender, System.EventArgs e)
{
Process.Start("explorer.exe");
}
//啟動office中的EXCEl
void Button3Click(object sender, System.EventArgs e)
{
Process.Start("EXCEL.exe");
}
//啟動WINDOWS播放器
void Button4Click(object sender, System.EventArgs e)
{
Process.Start("dvdplay.exe");
}
}
}
源碼下載:http://download.csdn.net/source/195507
http://download1.csdn.net/down3/20070617/17164940911.rar 用C#來實現相同的效果,發現C#本身方便的進程線程機制使工作變得簡單至極,隨手記錄一下。
2.首先,我們可以通過設定Process類,擷取輸出介面,代碼如下:
Process proc = new Process();
proc .StartInfo.FileName = strScript;
proc .StartInfo.WorkingDirectory = strDirectory;
proc .StartInfo.CreateNoWindow = true;
proc .StartInfo.UseShellExecute = false;
proc .StartInfo.RedirectStandardOutput = true;
proc .Start();
然後設定線程連續讀取輸出的字串:
eventOutput = new AutoResetEvent(false);
AutoResetEvent[] events = new AutoResetEvent[1];
events[0] = m_eventOutput;
m_threadOutput = new Thread( new ThreadStart( DisplayOutput ) );
m_threadOutput.Start();
WaitHandle.WaitAll( events );
線程函數如下:
private void DisplayOutput()
{
while ( m_procScript != null && !m_procScript.HasExited )
{
string strLine = null;
while ( ( strLine = m_procScript.StandardOutput.ReadLine() ) != null)
{
m_txtOutput.AppendText( strLine + "\r\n" );
m_txtOutput.SelectionStart = m_txtOutput.Text.Length;
m_txtOutput.ScrollToCaret();
}
Thread.Sleep( 100 );
}
m_eventOutput.Set();
}
這裡要注意的是,使用以下語句使TextBox顯示的總是最新添加的,而AppendText而不使用+=,是因為+=會造成整個TextBox的回顯使得整個顯示地區閃爍
m_txtOutput.AppendText( strLine + "\r\n" );
m_txtOutput.SelectionStart = m_txtOutput.Text.Length;
m_txtOutput.ScrollToCaret();
為了不阻塞主線程,可以將整個過程放到一個另一個線程裡就可以了
3.bat檔案控制參數的方法:
將你的net use \\172.16.17.1 /user:username password寫到bat檔案中,然後運行下面代碼就可以了。
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = "d:\\netuse.bat";
process.Start();
程式控制參數方法:
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo();
//prompt
psi.FileName = @"C:\WINDOWS\system32\cmd.exe"; // Path for the cmd prompt
psi.Arguments =@"net use \\172.16.17.1 /user:username password";
psi.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(psi);
就是用進程啟動cmd.exe
使用Process類運行ShellExecute的一個問題(點擊查看引用)
只有在STA線程上ShellExecute 才能確保工作無誤。在Process的實現中,並沒有考慮到這個問題,所以使用Process類運行ShellExecute可能會出錯。如果你不能保證 調用Process.Start的線程的ApartmentState,可以使用如下的代碼來避免這個問題:
using System;
using System.Threading;
public class Foo {
public static void OpenUrl() {
System.Diagnostics.Process.Start(@"http://www.google.com");
}
public static void Main() {
ThreadStart openUrlDelegate = new ThreadStart(Foo.OpenUrl);
Thread myThread = new Thread(openUrlDelegate);
myThread.SetApartmentState(ApartmentState.STA);
myThread.Start();
myThread.Join();
}
}