需要格式化输出C#

时间:2012-09-27 09:42:19

标签: c# command-line console

我需要创建一个控制台应用程序来打印一些帮助消息。我已经这样做但它没有在控制台中以默认的表格格式显示结果,如

c:\Users\>dir /?
Displays a list of files and subdirectories in a directory.

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
  [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

  [drive:][path][filename]
              Specifies drive, directory, and/or files to list.

  /A          Displays files with specified attributes.
  attributes   D  Directories                R  Read-only files
               H  Hidden files               A  Files ready for archiving
               S  System files               I  Not content indexed files
               L  Reparse Points             -  Prefix meaning not
  /B          Uses bare format (no heading information or summary).
  /C          Display the thousand separator in file sizes.  This is the
              default.  Use /-C to disable display of separator.
  /D          Same as wide but files are list sorted by column.
  /L          Uses lowercase.
  /N          New long list format where filenames are on the far right.
  /O          List by files in sorted order.
  sortorder    N  By name (alphabetic)       S  By size (smallest first)
               E  By extension (alphabetic)  D  By date/time (oldest first)
               G  Group directories first    -  Prefix to reverse order
  /P          Pauses after each screenful of information.

我是否需要使用转义序列或者是否有任何内置函数可以显示如下。我用谷歌搜索它。但无法找到解决方案可以帮助任何人:)?

4 个答案:

答案 0 :(得分:1)

您可能希望使用Composite Formatting

答案 1 :(得分:1)

在这种非常具体的情况下,只需将内容放在文本文件中,然后将控制流写入文本文件的内容。

我会将文本文件作为资源来简化部署。

答案 2 :(得分:1)

您必须使用@-quoted字符串文字。

string help = @"
Usage of @-quoted literal:
  1. Escape sequences are not processed
  2. To include double quotes then ""double it""
";
Console.WriteLine(help);

答案 3 :(得分:0)

只需使用空格格式化输出即可。

Console.WriteLine("DIR [drive:][path][filename] [/A[[:]attributes]]");
Console.WriteLine("  [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]]");