string path = "E:\\study\\noisemap\\Noisemap\\NMap\\zhou_test.bat";
int poi = path.LastIndexOf('\\');
string path_pre = path.Substring(0, poi + 1);
if (File.Exists(path_pre + "Sample.opx"))
{
Process P = new Process();
P.StartInfo.FileName = "cmd.exe";
P.StartInfo.UseShellExecute = false; //是否使用操作系统外壳启动该程序
P.StartInfo.RedirectStandardInput = true; //标准输入
P.StartInfo.RedirectStandardOutput = true; //标准输出
P.StartInfo.RedirectStandardError = true; //输出错误
P.StartInfo.CreateNoWindow = false; //不再新窗口显示该值
P.Start(); P.StandardInput.WriteLine("cd " + path_pre);
string panfu = path.Substring(0, 2);
P.StandardInput.WriteLine(panfu);
//读取10命令
// FileStream fs10 = new FileStream(bat, FileMode.Open);
FileStream fsrun = new FileStream(path_pre + "Run Sample NMap Case.bat", FileMode.Open);
StreamReader sr10 = new StreamReader(fsrun);
string temp10;
while ((temp10 = sr10.ReadLine()) != null)
{
P.StandardInput.WriteLine(temp10);
}
fsrun.Close(); P.Close();
Response.Write("<script>alert('.GRD生成成功!');</script>");
}
这个代码我先前用的一个批处理可以,这次这个的批处理有点复杂,需要在dos的环境下运行一段时间,不是以前那次瞬间就处理完成的,这个批处理是用一个.exe执行程序用他的输入文件产生一个.GRD文件。
这个.bat文件我直接双击批处理还能成功,但是我用这个代码就运行不了,不知问什么,还有其他的C#处理批处理的方法吗?
int poi = path.LastIndexOf('\\');
string path_pre = path.Substring(0, poi + 1);
if (File.Exists(path_pre + "Sample.opx"))
{
Process P = new Process();
P.StartInfo.FileName = "cmd.exe";
P.StartInfo.UseShellExecute = false; //是否使用操作系统外壳启动该程序
P.StartInfo.RedirectStandardInput = true; //标准输入
P.StartInfo.RedirectStandardOutput = true; //标准输出
P.StartInfo.RedirectStandardError = true; //输出错误
P.StartInfo.CreateNoWindow = false; //不再新窗口显示该值
P.Start(); P.StandardInput.WriteLine("cd " + path_pre);
string panfu = path.Substring(0, 2);
P.StandardInput.WriteLine(panfu);
//读取10命令
// FileStream fs10 = new FileStream(bat, FileMode.Open);
FileStream fsrun = new FileStream(path_pre + "Run Sample NMap Case.bat", FileMode.Open);
StreamReader sr10 = new StreamReader(fsrun);
string temp10;
while ((temp10 = sr10.ReadLine()) != null)
{
P.StandardInput.WriteLine(temp10);
}
fsrun.Close(); P.Close();
Response.Write("<script>alert('.GRD生成成功!');</script>");
}
这个代码我先前用的一个批处理可以,这次这个的批处理有点复杂,需要在dos的环境下运行一段时间,不是以前那次瞬间就处理完成的,这个批处理是用一个.exe执行程序用他的输入文件产生一个.GRD文件。
这个.bat文件我直接双击批处理还能成功,但是我用这个代码就运行不了,不知问什么,还有其他的C#处理批处理的方法吗?
但是在执行到p.WaitForInputIdle()的时候,就是在等待外壳应用程序结束的时候在执行后面的代码,提示的错误是“WaitForInputIdle 失败。这可能是因为该进程没有图形界面”我的外壳应用程序是cmd.exe进程下执行的,这是为什么,怎么修改呢?
回复3楼:string path = "E:\\study\\noisemap\\Noisemap\\NMap\\Run Sample NMap Case.bat";
int poi = path.LastIndexOf('\\');
string path_pre = path.Substring(0, poi + 1);
if (File.Exists(path_pre + "Sample.opx"))
{
//string sysFolder =Environment.GetFolderPath(Environment.SpecialFolder.System);
////Create a new process info structure.
//ProcessStartInfo pInfo = new ProcessStartInfo();
////Set the file name member of the process info structure.
//pInfo.FileName = "E:\\study\\noisemap\\Noisemap\\NMap\\Run Sample NMap Case.bat";
////Start the process.
//Process p = Process.Start(pInfo);
////Wait for the window to finish loading.
//p.WaitForInputIdle(50000);
////Wait for the process to end.
//p.WaitForExit();
//Response.Write("<script>alert('.GRD生成成功!');</script>");
Process P = new Process();
P.StartInfo.FileName = "cmd.exe";
P.StartInfo.UseShellExecute = false; //是否使用操作系统外壳启动该程序
P.StartInfo.RedirectStandardInput = true; //标准输入
//P.StartInfo.RedirectStandardOutput = true; //标准输出
P.StartInfo.RedirectStandardError = true; //输出错误
P.StartInfo.CreateNoWindow = false; //不再新窗口显示该值
P.Start(); //P.StandardInput.WriteLine("cd " + path_pre);
string panfu = path.Substring(0, 2);
P.StandardInput.WriteLine(panfu);
P.StandardInput.WriteLine("cd " + path_pre);
FileStream fsrun = new FileStream(path_pre + "Run Sample NMap Case.bat", FileMode.Open);
StreamReader sr10 = new StreamReader(fsrun);
string temp10;
while ((temp10 = sr10.ReadLine()) != null)
{
P.StandardInput.WriteLine(temp10);
}
//P.WaitForInputIdle();
fsrun.Close();
P.WaitForExit();
P.Close();
Response.Write("<script>alert('.GRD生成成功!');</script>");
用这种方法为什么结果是只出现cmd.exe的一个dos窗口,命令行没有写进去?
public static extern uint WinExec(string lpCmdLine, uint uCmdShow); [DllImport("shell32.dll")]
public extern static int ShellExecute(IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd
);