colins5286
Member
- Joined
- Nov 8, 2018
- Messages
- 12
- 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.
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.
I tried writing each line of information to a variable but because there are multiple disks it just doesn't work.
Thanks
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.
C#:
//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