Formatting text table in file, merging two lines per row into one

.net, c#, winforms

Solution

You've called `WriteLine` twice in your loop - so it's adding two lines for each item. You only want to add one line, e.g.

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

That won't pad the spaces though, so you need to include an alignment aspect in the composite format string. You want left padding, so you need a negative alignment, and it looks like you want 12 characters before the space, so:

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

Problem

I'm trying to write some information to a text file but the second row making spaces between the lines The code : ``` 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(); } ``` Once i added the line : ``` w.WriteLine(" " + ManageObject["DisplayName"].ToString()); ``` The result in the text file is like this : ``` 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 ``` Before i added the line the text file was like this : ``` Module Name Display Name Driver Type Link Date ============ ====================== ============= ====================== 1394ohci 3ware ACPI acpiex acpipagr AcpiPmi ``` I just wanted to add the Display Name row now so i added spaces so the row will start under Display Name but then its adding spaces in between the lines. How can i solve it ? ** In the end the text file should look like this : ``` 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 ``` And in the display name row and maybe also in the moudle name row to make sure the names will be in one line full names. For example in Display Name : 1394 OHCI Compliant Ho Should be : 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 ```

Original source