C# 格式化文件中的文本表,将每行两行合并为一行
我试图将一些信息写入文本文件,但第二行在行之间留有空格 守则:C# 格式化文件中的文本表,将每行两行合并为一行,c#,winforms,.net,C#,Winforms,.net,我试图将一些信息写入文本文件,但第二行在行之间留有空格 守则: private void CreateDriversList() { StreamWriter w = new StreamWriter(contentDirectory + "\\" + "Drivers.txt"); w.WriteLine("Module Name Display Name Driver Type Link Date"); w.WriteLine("======
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
在我添加行之前,文本文件如下所示:
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
我现在只想添加显示名称行,所以我添加了空格,所以该行将从显示名称下开始,然后在行之间添加空格
我怎样才能解决它
**
最后,文本文件应如下所示:
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兼容Ho
应为:1394 OHCI兼容主机控制器
**
您在循环中调用了两次
WriteLine
,因此它为每个项目添加了两行。您只想添加一行,例如
w.WriteLine("{0} {1}", ManageObject["Name"], ManageObject["DisplayName"]);
但这不会填充空间,因此需要在中包含对齐方面。您需要左填充,因此需要负对齐,并且看起来在空格前需要12个字符,因此:
w.WriteLine("{0,-12} {1}", ManageObject["Name"], ManageObject["DisplayName"]);
使用
WriteLine
将始终附加一个新行字符,因此一个简单的解决方案是使用Write
。您可以使用来固定宽度优先字段,直到其大小正确:
w.Write(ManageObject["Name"].ToString().PadRight(13, ' '));
w.WriteLine(ManageObject["DisplayName"]);
但是为了更好地固定每个字段的宽度,我建议使用格式化字符串:
w.WriteLine("{0,-12} {1}", ManageObject["Name"], ManageObject["DisplayName"]);
{0,x}
语法将只在输入参数的末尾填充字符。如果它们太长,它不会截断它们。如果DisplayName
大于为列分配的空间,则可能会导致一些问题。可以使用将字符串截断为适当的长度。最终,您的完整代码将如下所示:
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
);
我找到了解决这类问题的好办法
Jon Skeet您的解决方案正在运行,我必须将标题“驱动程序类型”和“链接日期”更向右移动,如何在驱动程序类型和链接日期的下一行添加更多字符?例如{1,-7}?我用文本文件现在的样子更新了我的问题。@HaimKashi:如果你需要匹配所有的值,你需要先计算出最大长度。如果你有一个最大长度的开始,这将使生活更容易。但是对于对齐的东西,我认为现在是一个好主意,让你自己做更多的研究,而不是我给你一勺解决方案。如果您按照有关复合格式字符串的MSDN文章的链接,您将在那里找到大量信息。