当前位置导航:炫浪网>>网络学院>>编程开发>>Visual C#教程

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(); 
 
程序控制参数方法: 
 
lovevsnet(编程一把手)提到: 
 
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(); 
    } 

相关内容
赞助商链接