使用命令提示符帮助.ReadToEnd()

时间:2010-09-25 00:32:23

标签: c# command-prompt

嘿,我正在尝试使用PowerISO的命令行程序piso。

我正在使用C#打开进程并输入命令来挂载cd / dvd。这工作正常,每次都会安装cd / dvd。

麻烦的是.ReadToEnd()不会停止阅读,程序会挂起。似乎命令提示符的响应应该是

PowerISO Version 4.5 Copyright(C) 2004-2009 PowerISO Computing, Inc Type piso -? for help

Mount successfully

但是我只能到达:

PowerISO Version 4.5 Copyright(C) 2004-2009 PowerISO Computing, Inc Type piso -? for help

并且程序将继续永远读取,永远不会让Mount成功输出。

这是我的C#代码:

String output = "";
System.Diagnostics.Process cmd = new System.Diagnostics.Process();
cmd.StartInfo.WorkingDirectory = @"C:\";               //@"

cmd.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardError = false;
cmd.Start();

cmd.StandardInput.WriteLine(MountPrgLoc + " " + 
                                                MountPrgCmd_Mount + " " +
                                                location + " " +
                                                drive);
StreamReader sr = cmd.StandardOutput;
output = sr.ReadToEnd() ;
MessageBox.Show(output);

提前感谢您的帮助 斯科特

------------------编辑----------------- 更多信息:

/* DVD Attributes */
String name = "My Movie Title";
String location = "\"" + @"C:\Users\Razor\Videos\iso\Movie.iso" + "\"";
String drive= "H:";
String format = ".iso";

/* Special Attributes */
String PlayPrg = "Windows Media Center";
String PlayPrgLoc = @"%windir%\ehome\";       //@"
String MountPrg = "PowerISO";
String MountPrgLoc = "\"" + @"C:\Program Files (x86)\PowerISO\piso.exe" + "\"";    //@"
String MountPrgCmd_Mount = "mount";
String MountPrgCmd_Unmount = "unmount";

1 个答案:

答案 0 :(得分:0)

为什么要使用cmd.exe?命令piso.exe将退出,但cmd.exe不会退出。 ReadToEnd()将继续等待程序cmd.exe退出&不会回来。更好的方法是直接调用piso.exe来进行挂载,在你的情况下将是:

String output = "";
System.Diagnostics.Process cmd = new System.Diagnostics.Process();
cmd.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\PowerISO";

cmd.StartInfo.FileName = "piso.exe";
cmd.StartInfo.Arguments = MountPrgCmd_Mount + " " + location + " " + drive;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.Start();

StreamReader sr = cmd.StandardOutput;
output = sr.ReadToEnd();
cmd.WaitForExit();
MessageBox.Show(output);