gpt4 book ai didi

c# - 格式化文件中的文本表,将每行两行合并为一行

转载 作者:行者123 更新时间:2023-11-30 14:12:23 25 4
gpt4 key购买 nike

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

代码:

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 兼容主机 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

最佳答案

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

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

虽然这不会填充空格,因此您需要在 composite format string 中包含对齐方面.你想要左填充,所以你需要负对齐,看起来你想要空格前的 12 个字符,所以:

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

关于c# - 格式化文件中的文本表,将每行两行合并为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17821616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com