在C#控制台中从左侧填充

Wou*_*100 3 c# padding console-application

目前我正在尝试在控制台中打印一些信息,但我试图给所有结果提供相同的填充.

这里有一个例子,你可以看到CurrentBuildNumber结果有一个/ t到多./ t只是将所有内容对齐,它可能是其他任何内容. 示例截图

所以,我需要一个"固定"填充:[结果].我怎样才能在我的代码中正确执行此操作?

谢谢!

码:

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

        foreach (string registryKey in registryKeys.GetValueNames())
        {
            Console.WriteLine(registryKey + "\t\t: " + registryKeys.GetValue(registryKey));
        }
Run Code Online (Sandbox Code Playgroud)

evh*_*n14 10

使用string.Format()或只是Console.Write(),因为它支持格式

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

Console.WriteLine(format, "Key", "Value")
Run Code Online (Sandbox Code Playgroud)

格式值-32表示该键应占用32个位置并与左侧对齐.


Mat*_*und 6

例如,在标签上使用.PadRight(30).