What is the best way to display percentages in a .Net CLI application?

I am working on a CLI file uploading application and would like to have a combination of added and rewritten output. Ideally, it would look something like this:

c:\>upload file1.dat 100% file2.dat 100% file3.dat 59%, 36.4k/s 

I would like only the last line to periodically update the percentage and current speed. I know that I can use SetCursorPosition to write output to any part of the console, but it seems that GetCursorPosition does not exist, and the absolute position of the last print file will be different. I also wonder how all this will affect the redirected output, but the proper handling of this parameter is not critical for this application.

EDIT : It looks like Console.CursorLeft / Console.CursorTop etc. will give me the current cursor position. I also looked at them! Well. Free accepted answer if someone wants it.

+4
source share
4 answers

Do you consider using Console.CursorLeft / Console.CursorTop to get the current cursor position ;-)

+3
source

It is pretty simple. The \ r character moves the cursor to the beginning of the current line.

 for(int i = 0; i <= 100; i++) { printf("Progress: %02d \r", i); fflush(stdout); Sleep(200); } printf("\n"); 
+1
source

This is not a platform limitation; I know curses are a good tool for creating these kinds of things, and there is a version on Windows there. (Nethack is a great example of what you need, and the console version of Windows is pretty interesting as a bonus.) I don't know if there is anything for this level of control on .NET. Perhaps compiling in controlled curse code can do what you want?

0
source

The simplest solution that came from old DOS was to write enough backspaces (\ b) to the console to clear the last percentage output, and then print the new one.

0
source

All Articles