我正在尝试将一些信息写入文本文件,但第二行在行之间创建空格
代码:
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
在显示名称行中,也可能在moudle名称行中,以确保名称将以一行全名显示。
例如,在显示名称中:1394 OHCI Compliant Ho 应该是:1394 OHCI兼容主机控制器
**
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
答案 0 :(得分:4)
你已经在循环中调用了WriteLine
两次 - 所以它为每个项目添加了两行。您只想添加一个行,例如
w.WriteLine("{0} {1}", ManageObject["Name"], ManageObject["DisplayName"]);
这不会填充空格,因此您需要在composite format string中包含对齐方面。你想要左边填充,所以你需要一个负对齐,看起来你想在空格前有12个字符,所以:
w.WriteLine("{0,-12} {1}", ManageObject["Name"], ManageObject["DisplayName"]);
答案 1 :(得分:3)
使用WriteLine
将始终附加换行符号,因此一个简单的解决方案是使用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
);
答案 2 :(得分:0)
我为这类问题找到了一个很好的解决方案 C# Function to create formatted table in Text File