Results 1 to 3 of 3

Thread: Looped Output Results

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2018
    Posts
    2
    Rep Power
    0

    Looped Output Results

    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

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,979
    Rep Power
    154
    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 by jmcilhinney; 11-08-2018 at 10:07 AM. Reason: Added mising parenthesis.

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Nov 2018
    Posts
    2
    Rep Power
    0

    Solved

    Quote Originally Posted by jmcilhinney View Post
    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.

Similar Threads

  1. these 2 codes are smiliar but the results are different.
    By lovejoy226 in forum Graphics / GDI+
    Replies: 7
    Last Post: 04-04-2016, 2:43 PM
  2. output All Options Of 2D Array
    By a906290 in forum Console Application
    Replies: 4
    Last Post: 08-19-2014, 7:58 AM
  3. Question code and the output isnt matching
    By edub91 in forum C# General Discussion
    Replies: 1
    Last Post: 04-07-2014, 7:25 PM
  4. tabcontrol for each output?
    By isriam in forum Windows Forms
    Replies: 1
    Last Post: 01-12-2013, 1:06 PM
  5. Replies: 3
    Last Post: 07-25-2012, 2:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •