public string RunCmd(string command, int milliseconds) { //实例一个Process类,启动一个独立进程 Process p = new Process(); string res = string.Empty; //Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,下面我们用到了他的几个属性: p.StartInfo.FileName = "cmd.exe"; //设定程序名 p.StartInfo.Arguments = "/c " + command; //设定程式执行参数 p.StartInfo.UseShellExecute = false; //关闭Shell的使用 p.StartInfo.RedirectStandardInput = true; //重定向标准输入 p.StartInfo.RedirectStandardOutput = true; //重定向标准输出 p.StartInfo.RedirectStandardError = true; //重定向错误输出 p.StartInfo.CreateNoWindow = true; //设置不显示窗口 p.Start(); //啟動 //return p.StandardOutput.ReadToEnd(); try { if (p.Start()) //开始进程 { if (milliseconds == 0) p.WaitForExit(); //这里无限等待进程结束 else p.WaitForExit(milliseconds); //这里等待进程结束,等待时间为指定的毫秒 res = p.StandardOutput.ReadToEnd(); } } catch { } finally { if (p != null) p.Close(); //string proc = (string)p; //KillProcess(p); } //p.StandardInput.WriteLine(command); //也可以用这种方式输入要执行的命令 //p.StandardInput.WriteLine("exit"); //不过要记得加上Exit要不然下一行程式执行的时候会当机 return res; //从输出流取得命令执行结果 } private void KillProcess(string processName) { System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(processName); foreach (System.Diagnostics.Process procCur in procs) { procCur.Kill(); procCur.Close(); } } 在vs中调试的话可能都能写入文件 但是在iis中就可能不行 所以在就考虑到时文件权限的问题 我把c盘的所有权限都给了 ASP.NET Machine Account了 所以 就可以写入了