C# 重定向时保留CMD输出格式
现在我有一个应用程序,用户可以将一些参数传递到进程中,并在文本框和/或文本文件中获取输出。但是,输出的格式与通过命令提示符运行这些进程时的格式相同。它们只是逐行阅读,没有适当的段落和间距。有没有办法保留原始输出?以下两个相关功能:C# 重定向时保留CMD输出格式,c#,cmd,textbox,output,text-files,C#,Cmd,Textbox,Output,Text Files,现在我有一个应用程序,用户可以将一些参数传递到进程中,并在文本框和/或文本文件中获取输出。但是,输出的格式与通过命令提示符运行这些进程时的格式相同。它们只是逐行阅读,没有适当的段落和间距。有没有办法保留原始输出?以下两个相关功能: //function called via a button to write to textbox // userInput is the textbox's name private void call_Process(object sen
//function called via a button to write to textbox
// userInput is the textbox's name
private void call_Process(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = selectedFilePath;
process.StartInfo.Arguments = string.Format("{0}", userInput.Text);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
try
{
process.Start();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
processOutput.Text += line;
}
}
//function called via button to write to textfile
private void write_To_File(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.InitialDirectory = "c:\\";
openfile.Filter = "All Files (*.*)|*.*";
openfile.FilterIndex = 1;
if (openfile.ShowDialog() == true)
{
TextWriter tw = new StreamWriter(openfile.FileName);
tw.WriteLine(processOutput.Text);
tw.Close();
}
}
根据@aschipfl的建议:
对
processOutput
控件和
processOutput.Text+=(line+Environment.NewLine)
对processOutput
控件和:processOutput.Text+=(line+Environment.NewLine)使用固定大小的字体代码>。是的,已经解决了!谢谢。Igor,考虑把你的命令作为回答,所以问题不会继续开放(假设OP接受它)…