1、启动应用程序
System.Diagnostics.Process pro = new System.Diagnostics.Process();
pro.StartInfo.FileName = "C:\\Program Files\\Winsoft\\WinMain.exe";
pro.Start();//关键
2、启动网页
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="iexplore";
proc.StartInfo.Arguments="http://www.****.net";
proc.Start();
proc.WaitForExit();
MessageBox.Show("You have just visited www.microsoft.com");
3、C#中关于System.Diagnostics.Process的应用
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();
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;\
p lic class Foo{
p lic static void OpenUrl() {
System.Diagnostics.Process.Start(@"http://www.google.com");
}
p lic static void Main() {
ThreadStart openUrlDelegate = new ThreadStart(Foo.OpenUrl);
Thread myThread = new Thread(openUrlDelegate);
myThread.SetApartmentState(ApartmentState.STA);
myThread.Start();
myThread.Join();
}
}