简体   繁体   中英

C# Align Text Right in Console

Is there a way for me to align text to the right side of my Console Application? I want to print a String with "[ok]" on the same line but on the right hand side. Like you see when booting a Linux Distro.

You could do something like this, if you're using Console.WriteLine...

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

Assuming your lines are 40 characters wide, the word "Finished" will be left-aligned in a 20 character field, and then the word "[ok]" will be right-aligned in another 20 character field. So you end up with something like

Finished!                            [ok]

I would suggest using curses as @Oded said.

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

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

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

使用curses库 - ncurses有一个c#绑定

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM