Arif YILMAZ
Arif YILMAZ

Reputation: 5866

how to get license key from c#

I am trying to create my own license system for my applications. Therefore, I am trying to read registry key but I couldnt do it succesfully.

I have created a registry key manually and the code below doesnt read it.

here is the regedit screen

enter image description here

here is my code below

// The Registry key we'll check

// *********** it returns null**********************
RegistryKey licenseKey = Registry.CurrentUser.OpenSubKey("Software\\Acme\\HostKeys");

if ( licenseKey != null )  
{
    // Passed the first test, which is the existence of the
    // Registry key itself. Now obtain the stored value
    // associated with our app's GUID.
    string strLic = (string)licenseKey.GetValue("test"); // reflected!
    if ( strLic != null ) 
    {
        // Passed the second test, which is some value
        // exists that's associated with our app's GUID.
        if ( String.Compare("Installed",strLic,false) == 0 )
        {
            // Got it...valid license...
            return new RuntimeRegistryLicense(type);
        } // if
    } // if
} // if

what am I doing wrong?

Upvotes: 0

Views: 984

Answers (1)

CathalMF
CathalMF

Reputation: 10055

You need to open the test key first and then read null.

RegistryKey licenseKey = Registry.CurrentUser.OpenSubKey("Software\\Acme\\HostKeys\\test");

if ( licenseKey != null )  
{
    string strLic = (string)licenseKey.GetValue(null); // GetValue(null) will return the default of the key.
    if ( strLic != null ) 
    {
        if ( String.Compare("Installed",strLic,false) == 0 )
        {
            return new RuntimeRegistryLicense(type);
        } 
    } 
} 

What you were trying to do was read a value called test under the HostKeys Key.

Upvotes: 1

Related Questions