I've a WPF Application that I can launch with command line in a PowerShell like this :
MyProgram.exe -arg1 -arg2 | Out-Default
I need to write in this PowerShell a progress bar that overwrite itself at each step. There is my code :
public void PerformNextState()
{
++mStep;
double nbRectangleToCompleteTheBar = 100.0;
string prefix = "\r0% [";
string suffix = "] 100%";
StringBuilder str = new StringBuilder();
str.Append(prefix);
int nbRectangles = (int)Math.Round(nbRectangleToCompleteTheBar * mStep / mMaxItems);
int i = 0;
for (; i < nbRectangles; ++i)
{
str.Append('█');
}
for (; i < nbRectangleToCompleteTheBar; ++i)
{
str.Append(' ');
}
str.Append(suffix);
Console.Write(str.ToString());
}
But the carriage return seems interpreted like a new line :
As you can see the character █ is also misinterpreted.
I also tried with this syntax \x000D
instead \r
with the same results.
Furthermore, I tried to do it with Console.SetCursorPosition()
, Console.CursorLeft
and Console.CursorTop
but I get IOException probably because it's a WPF Application and not Console Application.
I tried all these solutions in a Console Application and they all work. And I don't want to compile my WPF Application as Console Application because I think it's not a good practice and I will need to hide the console when launched with GUI.