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();
//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();
}
}
博客介绍了C#中控制bat文件和程序参数的方法,前者可将net use命令写入bat文件并通过Process类运行,后者用进程启动cmd.exe。还指出使用Process类运行ShellExecute在非STA线程可能出错,给出了确保在STA线程运行的代码示例。
714

被折叠的 条评论
为什么被折叠?



