• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Looped Output Results

colins5286

New member
Joined
Nov 8, 2018
Messages
2
Programming Experience
Beginner
Hi All,

I am relatively new to C# and am struggling with outputting some results.

I have a piece of code that uses the inbuilt WMI commands to retrieve some system information. It currently writes the output to the console and works fine (both shown below). What I would like to do is output the results to a variable so I can write them into a text file. I have no idea how to do this.

Code:
                //GET PHYSICAL DISK INFORMATION USING WMIC
                string PDisk = "";
                try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_DiskDrive");


                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
                        Console.WriteLine("Model: {0}", queryObj["Model"]);
                        Console.WriteLine("Name: {0}", queryObj["Name"]);
                        Console.WriteLine("SerialNumber: {0}", queryObj["SerialNumber"]);
                        Console.WriteLine("Signature: {0}", queryObj["Signature"]);
                        Console.WriteLine("-----------------------------------");
                        //PDisk = ("-----------------------------------") + Environment.NewLine;
                        //PDisk = PDisk + string.Format("Manufacturer: {0}", queryObj["Manufacturer"]) + Environment.NewLine;
                        //PDisk = PDisk + string.Format("Model: {0}", queryObj["Model"]) + Environment.NewLine;
                        //PDisk = PDisk + string.Format("Name: {0}", queryObj["Name"]) + Environment.NewLine;
                        //PDisk = PDisk + string.Format("SerialNumber: {0}", queryObj["SerialNumber"]) + Environment.NewLine;
                        //PDisk = PDisk + string.Format("Signature: {0}", queryObj["Signature"]) + Environment.NewLine;
                        //PDisk = PDisk = ("-----------------------------------");
                    }
                }
                catch (ManagementException ze)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + ze.Message);
                }
This is the output in the console. I would love to get this into a variable so I can then use the information later in the code.

-----------------------------------
Manufacturer: (Standard disk drives)
Model: Intel Raid 0 Volume
Name: \\.\PHYSICALDRIVE1
SerialNumber: Volume1
Signature:
-----------------------------------
-----------------------------------
Manufacturer: (Standard disk drives)
Model: TOSHIBA MQ01ABD100
Name: \\.\PHYSICALDRIVE0
SerialNumber: Y38CCU4WT
Signature: 95910472
-----------------------------------
-----------------------------------
Manufacturer: (Standard disk drives)
Model: USB2.0
Name: \\.\PHYSICALDRIVE2
SerialNumber: 92070034F0274626239
Signature: 1465625
-----------------------------------
I tried writing each line of information to a variable but because there are multiple disks it just doesn't work.

Thanks
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,172
Location
Sydney, Australia
Programming Experience
10+
You don't need a variable. Just create a StreamWriter and then call its WriteLine method instead of Console.WriteLine:
                using (var writer = new StreamWriter(filePath))
                {
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        writer.WriteLine("-----------------------------------");
                        writer.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
                        writer.WriteLine("Model: {0}", queryObj["Model"]);
                        writer.WriteLine("Name: {0}", queryObj["Name"]);
                        writer.WriteLine("SerialNumber: {0}", queryObj["SerialNumber"]);
                        writer.WriteLine("Signature: {0}", queryObj["Signature"]);
                        writer.WriteLine("-----------------------------------");
                    }
                }
 
Last edited:

colins5286

New member
Joined
Nov 8, 2018
Messages
2
Programming Experience
Beginner
Solved

You don't need a variable. Just create a StreamWriter and then call its WriteLine method instead of Console.WriteLine:
                using (var writer = new StreamWriter(filePath)
                {
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        writer.WriteLine("-----------------------------------");
                        writer.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
                        writer.WriteLine("Model: {0}", queryObj["Model"]);
                        writer.WriteLine("Name: {0}", queryObj["Name"]);
                        writer.WriteLine("SerialNumber: {0}", queryObj["SerialNumber"]);
                        writer.WriteLine("Signature: {0}", queryObj["Signature"]);
                        writer.WriteLine("-----------------------------------");
                    }
                }

Thanks so much for that - worked a treat.
 
Top Bottom