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.csdn.net/source/195507
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 ;
|
然后设置线程连续读取输出的字符串:
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);
|
分享到:
相关推荐
双击可打开指定目录,如果经常用到哪个目录,可直接双击这个快捷方式
process类的使用
process类 任务管理器 完整c#代码源码 里面含有新建任务 结束任务 资源管理 进程加载 最小化 代码浏览等功能 c#process类编写出来
// Process p = new Process(); // p.StartInfo.FileName = "cmd.exe"; // p.StartInfo.UseShellExecute = false; // p.StartInfo.RedirectStandardInput = true; // p.StartInfo.RedirectStandardOutput = ...
c#利用进程(Process)调用系统应用程序源码,如调用计算机,画画,记事本....等等系统应用程序
2. C# Process:进程类 3. C# Thread:线程及与线程有关的类 4. C# ThreadStart:创建无参数无返回的线程 5. ParameterizedThreadStart:创建带参数线程 6. C# Priority:多线程优先级设置 7. C# lock:给线程...
一个简单的进程处理程序,用Process类查看当前全部进程的详细信息,可以自行添加一些进一步的进程处理。
最近做一个编程,用C#调用类似ssh,ftp,runas,adb shell之类有中间输入,中间输出的.网上说使用Process.StartInfo,Process.StandardInput,Process.StandOutput之类的,但是每次都在StandardOutput.Read/ReadToEnd卡死,...
C#调用process执行DOS命令,间接调用其他软件或进程
c#控制台下的汉字输出乱码,可以通过这段程序解决
c#防止程序假死 - - 博客频道
为了公司调试部门的安装需要,研究了一下C#调用adb各类指令和推送安装包到安卓设备。同时回顾了下多年不用的Winform,花了1天时间写了这个小程序,希望这个小软件能帮助身边和网络上需要的朋友
好看的progressbar 样式,弥补winform 默认样式的单调。
如果您使用Google,则可能会找到一些方法。
用户、进程监控用户
Process Monitor is an advanced monitoring tool for Windows that shows real-time file system, Registry and process/thread activity. It combines the features of two legacy Sysinternals utilities, ...
C#中获取程序当前路径的方法 //获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。 string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; ...
C#通用类库调用是个好东西,有文件,图片、视频、日历、网络、消息、条形码、Excel等的操作类
用C#做的一个进度条,挺简单的,仅供参考。