Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all. Ok look at my code:

C#
//get Windows OS Key
            byte[] id = null;
            var regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
            if (regKey != null)
            {
                id = regKey.GetValue("DigitalProductId") as byte[];
                inv_oslicenseTB.Text = id.ToString();


I keep getting null value for id. It's ruining everything. Either I'm looking for the Windows OS License Key in the wrong place (although when i checked in the regedit console, the "DigitalProductID" had a full value in it) OR can't access because of permissions.

I added an app.manifest in there and configured these lines:
XML
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<!-- If your application is designed to work with Windows 7, uncomment the following supportedOS node-->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>

      <!-- If your application is designed to work with Windows 8, uncomment the following supportedOS node-->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"></supportedOS>

      <!-- If your application is designed to work with Windows 8.1, uncomment the following supportedOS node-->
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>


still get null value. What am I doing wrong, or is there a different way to get the OS License key?
Posted

Check you have permissions to read the registry at that location.
 
Share this answer
 
Did you check the byte array value in debug mode? id.ToString() won't return the correct value as id is byte[].

Also check which version of OS (32 bit or 64 bit) you are using and what's the target build version of your application. If it's 64bit OS and your target build is 32-bit you have to look inside SOFTWARE\Wow6432Node Node in the registry.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900