Search code examples
c#windows-forms-designerwmic

Windows Forms - How to return WMIC output into Textbox


I'm new to coding (3 days in and learning primarily from Google) it's going well enough so far but now I'm stuck :)

I want to run a WMIC command and return the second line of the output into a textbox. The WMIC command is:

'WMIC bios get serialnumber'

This returns:

SerialNumber 
 ABCD1234

How do I get just the serial number into a textbox please?

Thanks...


Solution

  • Add reference to System.Management and use the following in your form method.

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
            ManagementObject obj = searcher.Get().Cast<ManagementObject>().FirstOrDefault();          
    
                textBox.Text =  (string)obj["SerialNumber"];