3

我正在尝试将一些信息写入文本文件,但第二行在行之间留有空格

编码 :

private void CreateDriversList()
{
    StreamWriter w = new StreamWriter(contentDirectory + "\\" + "Drivers.txt");
    w.WriteLine("Module Name  Display Name           Driver Type   Link Date");
    w.WriteLine("============ ====================== ============= ======================");
    //Declare, Search, and Get the Properties in Win32_SystemDriver
    System.Management.SelectQuery query = new System.Management.SelectQuery("Win32_SystemDriver");
    System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);
    foreach (System.Management.ManagementObject ManageObject in searcher.Get())
    {
        w.WriteLine(ManageObject["Name"].ToString());
        w.WriteLine("             " + ManageObject["DisplayName"].ToString());
    }
    w.Close();
}

一旦我添加了这一行:

w.WriteLine("             " + ManageObject["DisplayName"].ToString());

文本文件中的结果是这样的:

Module Name  Display Name           Driver Type   Link Date
============ ====================== ============= ======================
1394ohci
             1394 OHCI Compliant Host Controller
3ware
             3ware
ACPI
             Microsoft ACPI Driver
acpiex
             Microsoft ACPIEx Driver
acpipagr

在我添加该行之前,文本文件是这样的:

Module Name  Display Name           Driver Type   Link Date
============ ====================== ============= ======================
1394ohci
3ware
ACPI
acpiex
acpipagr
AcpiPmi

我现在只想添加显示名称行,所以我添加了空格,因此该行将在显示名称下开始,然后在行之间添加空格。

我该如何解决?

**

最后,文本文件应如下所示:

Module Name  Display Name           Driver Type   Link Date             
============ ====================== ============= ======================
1394ohci     1394 OHCI Compliant Ho Kernel        7/26/2012 5:26:46 AM  
3ware        3ware                  Kernel        3/8/2012 10:33:45 PM  
ACPI         Microsoft ACPI Driver  Kernel        9/20/2012 9:09:16 AM  
acpiex       Microsoft ACPIEx Drive Kernel        7/26/2012 5:25:57 AM  
acpipagr     ACPI Processor Aggrega Kernel        7/26/2012 5:27:16 AM  
AcpiPmi      ACPI Power Meter Drive Kernel        7/26/2012 5:27:33 AM  
acpitime     ACPI Wake Alarm Driver Kernel        7/26/2012 5:27:37 AM  

并且在显示名称行中,也可能在模块名称行中,以确保名称将在一行全名中。

例如在显示名称中:1394 OHCI Compliant Ho 应该是:1394 OHCI Compliant Host Controller

**

Module Name  Display Name           Driver Type   Link Date
============ ====================== ============= ======================
1394ohci     1394 OHCI Compliant Host Controller
3ware        3ware
ACPI         Microsoft ACPI Driver
acpiex       Microsoft ACPIEx Driver
acpipagr     ACPI Processor Aggregator Driver
AcpiPmi      ACPI Power Meter Driver
4

3 回答 3

5

WriteLine在循环中调用了两次 - 所以它为每个项目添加了两行。您只想添加一行,例如

w.WriteLine("{0} {1}", ManageObject["Name"], ManageObject["DisplayName"]);

但这不会填充空格,因此您需要在复合格式字符串中包含对齐方面。你想要左填充,所以你需要一个负对齐,看起来你需要在空格前 12 个字符,所以:

w.WriteLine("{0,-12} {1}", ManageObject["Name"], ManageObject["DisplayName"]);
于 2013-07-23T21:48:09.187 回答
4

usingWriteLine将始终附加一个换行符,因此一个简单的解决方案是改为使用Write。您可以使用PadRight固定宽度第一个字段,直到它是正确的大小:

w.Write(ManageObject["Name"].ToString().PadRight(13, ' '));
w.WriteLine(ManageObject["DisplayName"]);

但为了更好地解决每个字段的宽度问题,我建议改用格式化字符串:

w.WriteLine("{0,-12} {1}", ManageObject["Name"], ManageObject["DisplayName"]);

{0,x}语法只会将字符填充到输入参数的末尾。如果它们太长,它不会截断它们。DisplayName如果比为列分配的空间长,这可能会导致一些问题。您可以使用String.Remove将字符串截断为适当的长度。最后,您的完整代码将如下所示:

w.WriteLine("{0,-12} {1,-22} {2,13}, {3,22:M/d/yyyy h:mm:ss tt}",
    ManageObject["Name"].ToString().Remove(12),        // Truncate to 12 chars
    ManageObject["DisplayName"].ToString().Remove(22), // Truncate to 22 chars
    ManageObject["DriverType"].ToString().Remove(13),  // Truncate to 13 chars
    ManageObject["LinkDate"]                       // No need to truncate here
);
于 2013-07-23T21:50:40.687 回答
0

我为这些类型的问题找到了一个很好的解决方案 C# Function to create formatted table in Text File

于 2014-03-19T05:10:50.210 回答