Trying to extract the "Motherboard Name" as a String variable value

57 views Asked by At

I'm new here, but I used the forum so many times just to save my mind from heating-up. Today I'm trying to code a software/script to get the motherboard name, using it as a string value and converting the string to a HEX code. But, let's focus on the "Using it as a string value".

I used a code just for some tests to get the Mobo name. It acts pretty good, but now I'm stuck in the part to convert it as a string value. Can you guys give me some help?

Here is the code that I'm using, ignore the last field, that's just me testing the HEX converter.

using System;
using System.Management;
using System.Text;

    public class Programa
    {
        public static void Main()
        {

        //MOBO NAME AND SERIAL GRABBER (REMEMBER TO REMOVE THE SERIAL)

        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Product, SerialNumber FROM Win32_BaseBoard"))
            {
            ManagementObjectCollection information = searcher.Get();
            foreach (ManagementObject obj in information)
                {
                    foreach (PropertyData data in obj.Properties)
                    {
                        Console.WriteLine("{0} = {1}", data.Name, data.Value);
                    }
                    Console.WriteLine();
                }

            //HEX CONVERTER

            string value = "AiPapi";

            byte[] bytes = Encoding.UTF8.GetBytes(value);

            string hexString = Convert.ToHexString(bytes);

            Console.WriteLine($"String value: \"{value}\"");
            Console.WriteLine($"   Hex value: \"{hexString}\"");
            }
        }
    }
1

There are 1 answers

6
AKX On

Sounds like you could get away here with just

private string FindFirstMoboName()
{
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Product FROM Win32_BaseBoard"))
    {
        foreach (ManagementObject obj in searcher.Get())
        {
            var prod = obj["Product"];
            if(prod != null) return prod.ToString();
        }
    }
    return "<no motherboard>";  // would be better to throw
}