【问题标题】:Preserve CMD output format when redirecting it重定向时保留 CMD 输出格式
【发布时间】:2015-10-08 17:50:15
【问题描述】:

现在我有一个应用程序,用户可以在其中将一些参数传递给进程并在文本框和/或文本文件中获取输出。但是,输出的格式与通过命令提示符运行这些进程时的格式相同。它们只是逐行阅读并在没有适当段落和间距的情况下放入。有没有办法保留原始输出?以下两个相关函数:

    //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();
        }
     }

【问题讨论】:

  • processOutput 控件和processOutput.Text += (line + Environment.NewLine); 使用固定大小的字体。
  • 是的,修复了它!谢谢。
  • @Igor,考虑发布您的命令作为答案,因此问题不会保持开放(假设 OP 接受它)...

标签: c# cmd textbox output text-files


【解决方案1】:

在@aschipfl 的建议下:

processOutput控件使用固定大小的字体

processOutput.Text += (line + Environment.NewLine);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2013-09-05
    相关资源
    最近更新 更多