C # Align text right in console

Is there a way to align text on the right side of my console application? I want to print a line with "[ok]" on the same line, but on the right side. As you can see when booting Linux Distro.

+4
source share
3 answers

I would suggest using curses, as @Oded said.

If you really don't want to use third-party libraries, you can use Console.BufferWidth to get the size of the console, and then Console.Console.CursorLeft to set the column position.

 Console.CursorLeft = Console.BufferWidth - 4; Console.Write("[ok]"); 

The above print [ok] at the end of the current line, leaving the cursor in the first column, the next line

+5
source

You can do something similar if using Console.WriteLine ...

 Console.WriteLine("{0,-20} {1,20}", "Finished!", "[ok]"); 

Assuming your lines are 40 characters wide, the word Done will be left-aligned in a 20-character box, and then the word [ok] will be right-aligned in another 20-character box. So you get something like

 Finished! [ok] 
+10
source

Use the curses library - ncurses has C # binding .

+2
source

All Articles