C# cmd输出进度(%)一行
我正在将 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
Big Thanks @Jackdaw
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试下面的方法:
看起来像这样:
Try the method below:
And seems like that:
我不知道您编写的代码(或者,如果它不是您的,则它来自哪里),但我可以告诉您,执行此操作的最简单方法是
\r
字符,这会将插入符号重置为行首。这意味着您必须确保不使用
Console.WriteLine
而是使用Console.Write
。一个例子:
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
butConsole.Write
instead.An example: