4

I will check my Windows os name (Windows 8 Pro) in c# but it gives an error, what's wrong?

    RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion", true);
    string currentKey;
    currentKey = reg.GetValue("ProductName", true).ToString();

    textBox1.Text = currentKey;
Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63

5 Answers5

5

You can use Environment.OSVersion for this.

Edit: Getting the OS name is answered here: Stackoverflow OS Friendly name

Community
  • 1
  • 1
Measurity
  • 1,316
  • 1
  • 12
  • 24
3

I'll just quote MSDN:

Registry.GetValue()-Method

Retrieves the value associated with the specified name, in the specified registry key. If the name is not found in the specified key, returns a default value that you provide, or a null reference (Nothing in Visual Basic) if the specified key does not exist.

This means that the value you are trying to get is not available.

Edit possible Solution:

Source: How to get the “friendly” OS Version Name?

private string GetOSName()
{
    var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
                select x.GetPropertyValue("Caption")).First();
    return name != null ? name.ToString() : "Unknown";
}

And to check whether OS is 32 or 64bit use following code:

private string GetOSBitness()
{
    if (Environment.Is64BitOperatingSystem == true)
        return " x64";
    else
        return " x86";
}

Above code will return (at least on my system):

Microsoft Windows 7 Professional x64

Community
  • 1
  • 1
Daniel Abou Chleih
  • 2,440
  • 2
  • 19
  • 31
  • Thanks. That's explaining the error (which was the question), although he surely wants to know how to get the OS Name (which gets solved by other answers) – Daniel Abou Chleih Oct 23 '13 at 20:20
2

Hacking at the registry is probably the wrong solution.

But why is it failing? Since you use Registry.LocalMachine, the HKLM is wrong. Remove the HKLM. That's a clear error.

On top of that watch out for registry redirection. Perhaps your process is 32 bit but the value you are looking for is in the 64 bit view of the registry. Use the RegistryView enumeration to gain access to the 64 bit view.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

You can get the commercial name of your operating system, including service pack information by querying the Windows Management Instrumentation interface:

    public static string GetOSNameAndVersion()
    {
        string str = Environment.OSVersion.ToString();
        try
        {
            var obj2 = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
                        .Get()
                        .Cast<System.Management.ManagementObject>()
                        .First<System.Management.ManagementObject>();
            str = ((string)obj2["Caption"]).Trim();
            string spMaj = obj2["ServicePackMajorVersion"].ToString();
            string spMin = obj2["ServicePackMinorVersion"].ToString();
            string osVer = obj2["Version"].ToString();
            if (((spMaj != "") && (spMaj != "0")) || ((spMin != "") && (spMin != "0")))
            {
                str = str + " SP " + spMaj;
                if ((spMin != "") && (spMin != "0"))
                {
                    str = str + "." + spMin;
                }
            }
            if (Environment.Is64BitOperatingSystem)
            {
                str = str + " x64";
            }
            else
            {
                str = str + " x86";
            }
            str = str + " (" + osVer + ")";
        }
        catch
        {
            // TODO: Implement your own exception handling here

            // the way it is, the method will fall back on to the Environment.OSVersion
            //   if the query fails
        }
        if (str.StartsWith("Microsoft"))
        {
            str = str.Substring("Microsoft".Length + 1);
        }
        return str;
    }
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
1

Your program is subject to what I presume to be a NullReferenceException because the program cannot find the registry sub-key because the path that you have supplied is incorrect.

You do not need to specify the hive in the hive path because your relative path is already the local hive. Exclude the hive from the path like like this:

Registry.LocalMachine.OpenSubKey(
    @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", true);

Depending on your programs' access privileges and operating system configuration your program may still throw an exception due to your program having insufficient access permissions.

User 12345678
  • 7,714
  • 2
  • 28
  • 46