Jon Weinraub Jon Weinraub - 3 months ago 17
C# Question

Getting Registered Owner in C# returns unknown value

I am trying to get the values of RegisteredOwner and RegisteredOrganization in a C# WinForms application and it keeps returning the default value, unknown and I am not too sure why.

string Owner = "";
string Company = "";

OperatingSystem osInfo = System.Environment.OSVersion;
if ( osInfo.Platform == PlatformID.Win32Windows )
{
// Windows 98
Owner = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "RegisteredOwner", "Unknown");
Company = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "RegisteredOrganization", "Unknown");
}
else if (osInfo.Platform == PlatformID.Win32NT)
{
// for NT+
Owner = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOwner", "Unknown");
Company = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization", "Unknown");
// do you need admin to read? or just write? b/c you need the UAC elevation. so out it on the Save button if it is just save changes, or in manifest.
}

lblRegOwner.Text = Owner;
lblRegOrg.Text = Company;


I tried running the program as admin with same results, and using a standard command prompt I am able to get the value returned.

C:\Users\jweinraub>reg query "HKLM\Software\Microsoft\windows nt\currentversion" /v registeredowner


HKEY_LOCAL_MACHINE\Software\Microsoft\windows nt\currentversion
registeredowner REG_SZ Jonathan Weinraub

Answer

Try this:

string Owner = "";
string Company = "";

OperatingSystem osInfo = System.Environment.OSVersion;
if (osInfo.Platform == PlatformID.Win32Windows)
{
    // Windows 98                                                
    Owner = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "RegisteredOwner", "Unknown").ToString();
    Company = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "RegisteredOrganization", "Unknown").ToString();
}
else if (osInfo.Platform == PlatformID.Win32NT)
{
    // for NT+                
    RegistryKey localKey;
    if (Environment.Is64BitOperatingSystem)
        localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    else
        localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);

    Owner = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue("RegisteredOwner", "Unknown").ToString();
    Company = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue("RegisteredOrganization", "Unknown").ToString();
}

lblRegOwner.Text = Owner;
lblRegOrg.Text = Company;