---
title: "C# Process"
author: "witsky"
date: "2014-07-02T11:41:48Z"
tags: []
categories: ["编程语言"]
slug: "c-process"
draft: false
lastmod: "2014-07-02T11:41:48Z"
id: 2087499
url: "https://witsky.iteye.com/blog/2087499"
---

1.process类的使用

Start 启动进程资源将其与process类关联

Kill立即关闭进程

waitforExit 在等待关联进程的退出

Close 释放与此关联的所有进程

`01` `/*`

`02` `* Created by SharpDevelop.`

`03` `* User: Administrator`

`04` `* Date: 2007-6-17`

`05` `* Time: 16:20`

`06` `*`

`07` `* To change this template use Tools | Options | Coding | Edit Standard Headers.`

`08` `*/`

`09`  

`10` `using` `System;`

`11` `using` `System.Collections.Generic;`

`12` `using` `System.Drawing;`

`13` `using` `System.Windows.Forms;`

`14` `//process类的名空间`

`15` `using` `System.Diagnostics;`

`16`  

`17` `namespace` `process`

`18` `{`

`19` `/// <summary>`

`20` `/// Description of MainForm.`

`21` `/// </summary>`

`22` `public` `partial` `class` `MainForm`

`23` `{`

`24` `[STAThread]`

`25` `public` `static` `void` `Main(string[] args)`

`26` `{`

`27` `Application.EnableVisualStyles();`

`28` `Application.SetCompatibleTextRenderingDefault(false);`

`29` `Application.Run(new` `MainForm());`

`30` `}`

`31` `  ` 

`32` `public` `MainForm()`

`33` `{`

`34` `//`

`35` `// The InitializeComponent() call is required for Windows Forms designer support.`

`36` `//`

`37` `InitializeComponent();`

`38` `   ` 

`39` `//`

`40` `// TODO: Add constructor code after the InitializeComponent() call.`

`41` `//`

`42` `}`

`43` `//启动IE主页http://www.baidu.com/`

`44` `void` `Button1Click(object` `sender, System.EventArgs e)`

`45` `{`

`46` `Process.Start("IExplore.exe","http://www.baidu.com/");`

`47` `}`

`48` `//启动资源管理器`

`49` `void` `Button2Click(object` `sender, System.EventArgs e)`

`50` `{`

`51` `Process.Start("explorer.exe");`

`52` `}`

`53` `//启动office中的EXCEl`

`54` `void` `Button3Click(object` `sender, System.EventArgs e)`

`55` `{`

`56` `Process.Start("EXCEL.exe");`

`57` `}`

`58` `//启动WINDOWS播放器`

`59` `void` `Button4Click(object` `sender, System.EventArgs e)`

`60` `{`

`61` `Process.Start("dvdplay.exe");`

`62` `}`

`63` `}`

`64` `}`

源码下载:http://download.csdn.net/source/195507

[http://download1.csdn.net/down3/20070617/17164940911.rar](http://download1.csdn.net/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;`

`7` `proc .Start();`

然后设置线程连续读取输出的字符串：

    

`1` `eventOutput = new` `AutoResetEvent(false);`

`2` `AutoResetEvent[] events = new` `AutoResetEvent[1];`

`3` `events[0] = m_eventOutput;`

`4`  

`5` `m_threadOutput = new` `Thread( new` `ThreadStart( DisplayOutput ) );`

`6` `m_threadOutput.Start();`

`7` `WaitHandle.WaitAll( events );`

线程函数如下：

  

`01` `private` `void` `DisplayOutput()`

`02` `{`

`03` `while` `( m_procScript != null` `&& !m_procScript.HasExited )`

`04` `{`

`05` `string` `strLine = null;`

`06` `while` `( ( strLine = m_procScript.StandardOutput.ReadLine() ) != null)`

`07` `{`

`08` `m_txtOutput.AppendText( strLine + "\r\n"` `);`

`09` `m_txtOutput.SelectionStart = m_txtOutput.Text.Length;`

`10` `m_txtOutput.ScrollToCaret();`

`11` `}`

`12` `Thread.Sleep( 100 );`

`13` `}`

`14` `m_eventOutput.Set();`

`15` `}`

这里要注意的是，使用以下语句使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";`

`4` `process.Start();`

程序控制参数方法:

`1` `System.Diagnostics.ProcessStartInfo psi =`

`2` `new` `System.Diagnostics.ProcessStartInfo();`

`3` `//prompt`

`4` `psi.FileName = @"C:\WINDOWS\system32\cmd.exe"; // Path for the cmd prompt`

`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，可以使用如下的代码来避免这个问题：

`01` `using` `System;`

`02` `using` `System.Threading;`

`03` `public` `class` `Foo {`

`04` `public` `static` `void` `OpenUrl()    {`

`05` `System.Diagnostics.Process.Start(@"http://www.google.com");`

`06` `}`

`07` `public` `static` `void` `Main() {`

`08` `ThreadStart openUrlDelegate = new` `ThreadStart(Foo.OpenUrl);`

`09` `Thread myThread = new` `Thread(openUrlDelegate);`

`10` `myThread.SetApartmentState(ApartmentState.STA);`

`11` `myThread.Start();`

`12` `myThread.Join();`

`13` `}`

`14` `}`