网上看到的代码,稍微做了下修改,将要操作的文件夹权限设置为Everybody-》完全控制
private void RunCmd()
{
StreamReader sOut = null;
StreamWriter sIn = null;
try
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = mobilePath;
Process proc = Process.Start(psi);
sOut = proc.StandardOutput;
sIn = proc.StandardInput;
string builderXml = string.Format(@"mkdir c:/test22");
sIn.WriteLine(builderXml);
sIn.WriteLine("EXIT");
proc.Close();
}
catch (Exception ex)
{
return;
}
finally
{
sIn.Close();
sOut.Close();
}
}
网上相关资料
http://codebetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx
Run a .BAT file from ASP.NET
Okay, running .BAT files from ASP.NET using the System.Diagnostics.Process object and static methods this should be easy, right? Well, this might work for you, but it certainly won't work on my machines. And after doing lots of reasearh on the issue, it seems that other people are also having problems with this too.
I wrestled with permissions and all sorts of other stuff, trying to get a simple batch file to run, with no luck. I tried lauching the bat file directly, launching cmd.exe and calling the bat file using stin. No dice. It seems that something on my machine was keeping an unattended process from running bat files. This makes sense, but I was never able to pinpoint what was preventing this, so I came up with a workaround.
I realized that since I could sucessfully run cmd.exe, and send commands to it via stin, I could just open the batch file, and send each line to cmd.exe, which is essentially the same as running a batch file itself. This technique works great, and I thought I'd pass along the code here.
// Get the full file path
string strFilePath = “c://temp//test.bat”;
// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c://temp//“;
// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;
// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;
// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
sIn.WriteLine(strm.ReadLine());
}
strm.Close();
// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";
sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");
// Close the process
proc.Close();
// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();
// Close the io Streams;
sIn.Close();
sOut.Close();
// Write out the results.
string fmtStdOut = "{0}";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "
")));
经过测试,以上读BAT的写法在WINDOWS 2003是正常的,但放在2000下,老是不能执行这个BAT,最后只好直接在代码里输入命令,其中原因还未找到,估计是权限的问题,呵呵。