1.process类的使用
Start 启动进程资源将其与process类关联
Kill立即关闭进程
waitforExit 在等待关联进程的退出
Close 释放与此关联的所有进程
11 | using System.Collections.Generic; |
13 | using System.Windows.Forms; |
15 | using System.Diagnostics; |
20 | /// Description of MainForm. |
22 | public partial class MainForm |
25 | public static void Main( string [] args) |
27 | Application.EnableVisualStyles(); |
28 | Application.SetCompatibleTextRenderingDefault( false ); |
29 | Application.Run( new MainForm()); |
37 | InitializeComponent(); |
44 | void Button1Click( object sender, System.EventArgs e) |
49 | void Button2Click( object sender, System.EventArgs e) |
51 | Process.Start( "explorer.exe" ); |
54 | void Button3Click( object sender, System.EventArgs e) |
56 | Process.Start( "EXCEL.exe" ); |
59 | void Button4Click( object sender, System.EventArgs e) |
61 | Process.Start( "dvdplay.exe" ); |
源码下载:http://download.youkuaiyun.com/source/195507
http://download1.youkuaiyun.com/down3/20070617/17164940911.rar 用C#来实现相同的效果,发现C#本身方便的进程线程机制使工作变得简单至极,随手记录一下。
2.首先,我们可以通过设置Process类,获取输出接口,代码如下:
1 | Process proc = new Process(); |
2 | proc .StartInfo.FileName = strScript; |
3 | proc .StartInfo.WorkingDirectory = strDirectory; |
4 | proc .StartInfo.CreateNoWindow = true ; |
5 | proc .StartInfo.UseShellExecute = false ; |
6 | proc .StartInfo.RedirectStandardOutput = true ; |
然后设置线程连续读取输出的字符串:
1 | eventOutput = new AutoResetEvent( false ); |
2 | AutoResetEvent[] events = new AutoResetEvent[1]; |
3 | events[0] = m_eventOutput; |
5 | m_threadOutput = new Thread( new ThreadStart( DisplayOutput ) ); |
6 | m_threadOutput.Start(); |
7 | WaitHandle.WaitAll( events ); |
线程函数如下:
01 | private void DisplayOutput() |
03 | while ( m_procScript != null && !m_procScript.HasExited ) |
05 | string strLine = null ; |
06 | while ( ( strLine = m_procScript.StandardOutput.ReadLine() ) != null ) |
08 | m_txtOutput.AppendText( strLine + "\r\n" ); |
09 | m_txtOutput.SelectionStart = m_txtOutput.Text.Length; |
10 | m_txtOutput.ScrollToCaret(); |
这里要注意的是,使用以下语句使TextBox显示的总是最新添加的,而AppendText而不使用+=,是因为+=会造成整个TextBox的回显使得整个显示区域闪烁
1 | m_txtOutput.AppendText( strLine + "\r\n" ); |
2 | m_txtOutput.SelectionStart = m_txtOutput.Text.Length; |
3 | m_txtOutput.ScrollToCaret(); |
为了不阻塞主线程,可以将整个过程放到一个另一个线程里就可以了
3.bat文件控制参数的方法:
将你的net use \\172.16.17.1 /user:username password写到bat文件中,然后运行下面代码就可以了。
1 | System.Diagnostics.Process process = new System.Diagnostics.Process(); |
2 | process.StartInfo.CreateNoWindow = false ; |
3 | process.StartInfo.FileName = "d:\\netuse.bat" ; |
程序控制参数方法:
1 | System.Diagnostics.ProcessStartInfo psi = |
2 | new System.Diagnostics.ProcessStartInfo(); |
4 | psi.FileName = @"C:\WINDOWS\system32\cmd.exe" ; |
5 | psi.Arguments = @"net use \\172.16.17.1 /user:username password" ; |
6 | psi.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden; |
7 | System.Diagnostics.Process.Start(psi); |
就是用进程启动cmd.exe
使用Process类运行ShellExecute的一个问题(点击查看引用)
只有在STA线程上ShellExecute 才能确保工作无误。在Process的实现中,并没有考虑到这个问题,所以使用Process类运行ShellExecute可能会出错。如果你不能保证调用Process.Start的线程的ApartmentState,可以使用如下的代码来避免这个问题:
02 | using System.Threading; |
04 | public static void OpenUrl() { |
07 | public static void Main() { |
08 | ThreadStart openUrlDelegate = new ThreadStart(Foo.OpenUrl); |
09 | Thread myThread = new Thread(openUrlDelegate); |
10 | myThread.SetApartmentState(ApartmentState.STA); |