BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #24461同步于 2008/3/17
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

[求助]C#问题 关于Process.Start的问题

lihui6626
2008/3/17镜像同步3 回复
现在是这样 我已经连接好了一个字符串para 诸如这样:“perl -w c:\inetpub\wwwroot\wjc\easymsg.pl -A -a -i c:\inetpub\wwwroot\wjc\upload\message07.txt -o c:\inetpub\wwwroot\wjc\web\message07.html” 这一段放在cmd.exe中是可以运行的 或者开始->运行 中输入这一段 现在我想这个过程在服务器端运行 4 Process p = new Process(); 8 p.StartInfo.FileName = "perl"; //设定程序名 9 p.StartInfo.Arguments = para; //设定程式执行参数 10 p.StartInfo.UseShellExecute = false; //关闭Shell的使用 11 p.StartInfo.RedirectStandardInput = true; //重定向标准输入 12 p.StartInfo.RedirectStandardOutput = true; //重定向标准输出 13 p.StartInfo.RedirectStandardError = true; //重定向错误输出 14 p.StartInfo.CreateNoWindow = true; //设置不显示窗口 15 16 p.Start(); //启动 烦请大家告诉我process.start的调用方法? 谢谢各位
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
Quaker机器人#1 · 2008/3/17
给你个写的函数,应该能帮上忙吧! private string[] RunCmd(string[] command) { Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; // Run this program process.StartInfo.UseShellExecute = false; // Must be false process.StartInfo.RedirectStandardInput = true; // Redirect input stream process.StartInfo.RedirectStandardOutput = true; // Redirect output stream process.StartInfo.RedirectStandardError = true; // Redirect error stream process.StartInfo.CreateNoWindow = true; // All commands run in one window process.Start(); StreamWriter sw = process.StandardInput; sw.AutoFlush = true; StreamReader srout = process.StandardOutput; StreamReader srerr = process.StandardError; for (int i = 0; i < command.Length; i++) { sw.WriteLine(command[i]); } sw.Close(); string[] output = new string[2]; output[0] = srout.ReadToEnd(); output[1] = srerr.ReadToEnd(); srerr.Close(); srout.Close(); process.WaitForExit(); process.Close(); return output; }
lihui6626机器人#2 · 2008/3/17
最后我是这么写的 public void runCommand(string command) { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "cmd.exe"; psi.Arguments = "/c "+command; psi.RedirectStandardOutput = true; psi.UseShellExecute = false; Process p = Process.Start(psi); } 在主函数中调用:runCommand(para) para = “perl -w c:\inetpub\wwwroot\wjc\easymsg.pl -A -a -i c:\inetpub\wwwroot\wjc\upload\message07.txt -o c:\inetpub\wwwroot\wjc\web\message07.html” 就可以执行了 举个例子 如果你将psi.Arguments = "/c "+command; 改为 ”/c dir d:“ 那么你就会得到dir c:的结果了 ---------------------------------------------------------------------------------------------------- 另外 我输出得到结果页面 就是那个message07.html 之后 直接用Response.Redirect转向的话会有问题(而事实上你去目录下打开是不会有问题的) 我就用了一个Thread.Sleep(1000); 停顿一秒就行了。可能是处理不过来! 谢谢各位的帮助
Lonhero机器人#3 · 2008/3/17
恩,就是这样,自己动手,丰衣足食