【发布时间】:2013-09-18 11:14:10
【问题描述】:
我有一个运行 phing 构建文件的一些任务的 c# Windows 窗体应用程序。 现在我有两种可能性:
单击 xml 任务的按钮会显示 cmd 窗口, 显示执行进度,然后关闭 cmd(使用 /k 我可以保持 cmd 窗口打开)。
单击按钮后,cmd 输出将保存在一个 .txt 文件中,并且 然后我可以读取文件并在一个文本框中显示内容。(这个 不显示 cmd 窗口)
我想要的是这两者的混合,因为我有一些向用户显示消息的任务,现在如果我使用第二个选项,它会打开一个空的 cmd 窗口,用户必须猜测问题是什么.
所以我真正想要的是一种在执行结束之前显示 cmd 窗口的方法(如第一个选项),但同时将 cmd 输出保存在一个文件中,这样我就可以将文件内容放在我的文本框中应用。
请原谅一些英文错误..
编辑 Dour High Arch:
textBox1.Clear();
var startInfo = new ProcessStartInfo("phing");
startInfo.WorkingDirectory = @"C:\wamp\bin\php\php5.4.3";
startInfo.Arguments = "commit > log.txt";
Process proc = Process.Start(startInfo);
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.CreateNoWindow =true ;
proc.WaitForExit();
using (StreamReader sr = new StreamReader(@"C:\wamp\bin\php\php5.4.3\log.txt"))
{
textBox1.Text = sr.ReadToEnd();
}
使用此代码,控制台的输出显示在文本框上,但是当我单击按钮时执行的提交任务向用户提出了一个问题,所以到那时,它会打开一个空的 cmd 窗口,预期用户提交消息但不显示问题,因此用户必须猜测预期的内容!
其他尝试:
string output = string.Empty;
string error = string.Empty;
string path = @"C:\wamp\bin\php\php5.4.3\";
string ffmpegPath = Path.Combine(path, "phing.bat");
string ffmpegParams = @"commit";
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Windows\System32\cmd.exe");
startInfo.WorkingDirectory = @"C:\wamp\bin\php\php5.4.3\";
startInfo.Arguments = "/k" + ffmpegPath + " " + ffmpegParams;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
Process ffmpeg = new Process();
ffmpeg.StartInfo = startInfo;
ffmpeg.Start();
StreamReader outputReader = ffmpeg.StandardOutput;
textBox1.Text = outputReader.ReadToEnd();
ffmpeg.WaitForExit();
ffmpeg.Close();
在这种情况下 cmd 没有打开,所以没有问题也没有答案!如果我设置startInfo.UseShellExecute = true;,那么我会遇到异常“进程对象必须将 UseShellExecute 属性设置为 false 才能重定向 IO 流”。
我尝试了更多的东西,但结果总是一样,或者我总是打开 cmd 窗口但代码没有传递到文本框(这是我现在正在使用的代码)
string path = @"C:\wamp\bin\php\php5.4.3\";
string ffmpegPath = Path.Combine(path, "phing.bat");
string ffmpegParams = @"update";
Process ffmpeg = new Process();
ffmpeg.StartInfo.WorkingDirectory = @"C:\wamp\bin\php\php5.4.3\";
ffmpeg.StartInfo.FileName = "cmd.exe";
ffmpeg.StartInfo.Arguments = "/k" + ffmpegPath + " " + ffmpegParams;
ffmpeg.Start();
或者我将输出保存在一个文件中或直接将其流式传输到文本框,但我没有看到问题。
那么有人知道怎么解决吗?
【问题讨论】:
-
this answer 有帮助吗?
-
我不能让它工作......就像我说的,我可以将输出放在文本框或 cmd 窗口中,但我不能让两者同时工作,我需要这个......我尝试过的所有时间我总是遇到一个例外
-
如果您遇到异常,您需要向我们展示异常以及引发异常的代码。
-
我编辑中的所有代码,抱歉后面的回答