C# cmd输出进度(%)一行

发布于 2025-01-09 03:11:55 字数 2042 浏览 1 评论 0原文

我正在将 cmd 输出到 RichTextBox。 有没有办法将所有进度 (%) 合并/连接到 RichTextBox 的一行中?而不是为每个 % 创建一行。我希望它像 cmd (除了像现在一样删除空白行)。

private async void btnStart_Click(object sender, EventArgs e){
await Task.Factory.StartNew(() =>
{
    Execute1("Prtest.exe", " x mode2 C:\\input.iso C:\\output.iso");
});
}

private void Execute1(string filename, string cmdLine){
var fileName = filename;
var arguments = cmdLine;

var info = new ProcessStartInfo();
info.FileName = fileName;
info.Arguments = arguments;

info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.CreateNoWindow = true;

using (var p = new Process())
{
    p.StartInfo = info;
    p.EnableRaisingEvents = true;

    p.OutputDataReceived += (s, o) =>
    {
        tConsoleOutput(o.Data);
    };
    p.ErrorDataReceived += (s, o) =>
    {
        tConsoleOutput(o.Data);
    };

    p.Start();
    p.BeginOutputReadLine();
    p.BeginErrorReadLine();
    p.WaitForExit();
}
}

public void tConsoleOutput(string text){
BeginInvoke(new Action(delegate ()
{
    rtConsole.AppendText(text + Environment.NewLine);
    rtConsole.ScrollToCaret();
    //remove empty lines
    rtConsole.Text = Regex.Replace(rtConsole.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
}));
}

真实cmd.exe输出:

处理:100%

扇区:43360

已完成。

C#RichTextBox code> (rtConsole) 输出:

处理:2%
处理:4%
处理:7%
处理:9%
处理:11%
处理:14%
处理:16%
处理:39%
处理:100%
扇区:43360
已完成。

更新:已解决

在此处输入图像描述

非常感谢@Jackdaw

I'm working on a cmd output to RichTextBox.
Is there any way to merge/join all the progress (%) into a single line of the RichTextBox? Instead of creating a line for each %. I would like it to be like cmd (except removing the blank lines as it is now).

private async void btnStart_Click(object sender, EventArgs e){
await Task.Factory.StartNew(() =>
{
    Execute1("Prtest.exe", " x mode2 C:\\input.iso C:\\output.iso");
});
}

private void Execute1(string filename, string cmdLine){
var fileName = filename;
var arguments = cmdLine;

var info = new ProcessStartInfo();
info.FileName = fileName;
info.Arguments = arguments;

info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.CreateNoWindow = true;

using (var p = new Process())
{
    p.StartInfo = info;
    p.EnableRaisingEvents = true;

    p.OutputDataReceived += (s, o) =>
    {
        tConsoleOutput(o.Data);
    };
    p.ErrorDataReceived += (s, o) =>
    {
        tConsoleOutput(o.Data);
    };

    p.Start();
    p.BeginOutputReadLine();
    p.BeginErrorReadLine();
    p.WaitForExit();
}
}

public void tConsoleOutput(string text){
BeginInvoke(new Action(delegate ()
{
    rtConsole.AppendText(text + Environment.NewLine);
    rtConsole.ScrollToCaret();
    //remove empty lines
    rtConsole.Text = Regex.Replace(rtConsole.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
}));
}

Real cmd.exe output:

Processing: 100%

Sectors: 43360

Completed.

C# RichTextBox (rtConsole) output:

Processing: 2%
Processing: 4%
Processing: 7%
Processing: 9%
Processing: 11%
Processing: 14%
Processing: 16%
Processing: 39%
Processing: 100%
Sectors: 43360
Completed.

UPDATE: Solved

enter image description here

Big Thanks @Jackdaw

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乱了心跳 2025-01-16 03:11:55

尝试下面的方法:

static public void tConsoleOutput(RichTextBox rtb, string line)
{
    var pattern = @"^Processing: \d{1,3}%.*$";   
    if (!line.EndsWith(Environment.NewLine))
        line += Environment.NewLine;

    var isProcessing = Regex.Match(line, pattern).Success;

    rtb.Invoke((MethodInvoker)delegate
        {
            var linesCount = rtb.Lines.Length;
            if (linesCount > 1 && isProcessing)
            {
                var last = rtb.Lines[linesCount - 2];
                if (Regex.Match(last, pattern).Success)
                {
                    var nlSize = Environment.NewLine.Length;
                    // Update latest line
                    var sIndex = rtb.GetFirstCharIndexFromLine(linesCount - nlSize);
                    var eIndex = sIndex + last.Length + nlSize;
                    rtb.Select(sIndex, eIndex - sIndex);
                    rtb.SelectedText = line;    
                    return;                                
                }                
            }
            rtb.AppendText(line);
        });
}

看起来像这样:

在此处输入图像描述

Try the method below:

static public void tConsoleOutput(RichTextBox rtb, string line)
{
    var pattern = @"^Processing: \d{1,3}%.*
quot;;   
    if (!line.EndsWith(Environment.NewLine))
        line += Environment.NewLine;

    var isProcessing = Regex.Match(line, pattern).Success;

    rtb.Invoke((MethodInvoker)delegate
        {
            var linesCount = rtb.Lines.Length;
            if (linesCount > 1 && isProcessing)
            {
                var last = rtb.Lines[linesCount - 2];
                if (Regex.Match(last, pattern).Success)
                {
                    var nlSize = Environment.NewLine.Length;
                    // Update latest line
                    var sIndex = rtb.GetFirstCharIndexFromLine(linesCount - nlSize);
                    var eIndex = sIndex + last.Length + nlSize;
                    rtb.Select(sIndex, eIndex - sIndex);
                    rtb.SelectedText = line;    
                    return;                                
                }                
            }
            rtb.AppendText(line);
        });
}

And seems like that:

enter image description here

我们的影子 2025-01-16 03:11:55

我不知道您编写的代码(或者,如果它不是您的,则它来自哪里),但我可以告诉您,执行此操作的最简单方法是 \r 字符,这会将插入符号重置为行首。

这意味着您必须确保不使用 Console.WriteLine 而是使用 Console.Write

一个例子:

Console.Write("00.00% Done");
Thread.Sleep(1500);
Console.Write("\r100.00% Done");

I don't know about the code that you have written (or, if it's not yours, where it comes from) but I can tell you that the easiest way of doing this is the \r character, which resets your caret to the beginning of the line.

This means that you must make sure not to use Console.WriteLine but Console.Write instead.

An example:

Console.Write("00.00% Done");
Thread.Sleep(1500);
Console.Write("\r100.00% Done");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文