如何将我的程序的控制台输出重定向到.Net上的文件中
本文关键字:Net 文件 重定向 控制台 我的 程序 输出 | 更新日期: 2023-09-27 17:57:27
您好!
我在程序中运行了一些*.exe文件。此exe文件控制台应用程序。所以,我这样开始:
proc = new Process();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.RedirectStandardInput = false;
proc.StartInfo.FileName = procpath;
proc.StartInfo.Arguments = String.Format("/someParam1:true , _NamePipe1);
proc.Start();
这个程序启动,然后在操作过程中进行控制台输出。那么,如何将这个输出写入文件呢?我不能修改外国应用程序。
谢谢!
编辑:目标应用程序有KeyPress功能(等等),当我用这个目标应用程序运行进程时(UseShellExecute==false)-KeyPress认为按下了一些键,目标应用程序停止了(当你输入一些键时-目标应用程序将停止。)
那么,我能做什么呢?
检查此代码:
Process proc = new Process();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.RedirectStandardInput = false;
proc.StartInfo.FileName = "procpath";
proc.StartInfo.Arguments = String.Format("/someParam1:true , _NamePipe1");
proc.Start();
StreamWriter writer = File.CreateText("newfile.txt");
writer.WriteLine(proc.StandardOutput.ReadToEnd());
proc.WaitForExit();
writer.Flush();
writer.Close();
从这个链接得到
从.NET应用程序(C#)捕获控制台输出
我希望这对你有用
private void reg(string host)
{
string build = "QUERY ''''" + host + "''HKEY_USERS";
string parms = @build;
string output = "";
string error = string.Empty;
ProcessStartInfo psi = new ProcessStartInfo("reg.exe", parms);
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
psi.UseShellExecute = false;
System.Diagnostics.Process reg;
reg = System.Diagnostics.Process.Start(psi);
using (System.IO.StreamReader myOutput = reg.StandardOutput)
{
output = myOutput.ReadToEnd();
}
using (System.IO.StreamReader myError = reg.StandardError)
{
error = myError.ReadToEnd();
}
Output.AppendText(output + "'n");
} ``