Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding from left in C# console

Currently I'm trying to print some information in the console, but I'm trying to give all results the same padding.

Here an example, you can see CurrentBuildNumber result has one /t to much. /t is only to align everything out, it could be anything else. Example screenshot

So, i need a "fixed" padding for the : [result]. How can i do this correctly in my code?

Thanks!

Code:

        RegistryKey registryKeys = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");

        foreach (string registryKey in registryKeys.GetValueNames())
        {
            Console.WriteLine(registryKey + "\t\t: " + registryKeys.GetValue(registryKey));
        }
like image 369
Wouter0100 Avatar asked Nov 30 '22 02:11

Wouter0100


1 Answers

Use string.Format() or just Console.Write() as it has support for formats

const string format = "{0,-32} :{1}";

Console.WriteLine(format, "Key", "Value")

the format value -32 means that key should take 32 positions and aligned to the left.

like image 100
evhen14 Avatar answered Dec 12 '22 03:12

evhen14