簡體   English   中英

如何從C#獲取許可證密鑰

[英]how to get license key from c#

我正在嘗試為我的應用程序創建自己的許可證系統。 因此,我試圖讀取注冊表項,但無法成功完成。

我已經手動創建了注冊表項,並且下面的代碼沒有讀取它。

這是regedit屏幕

在此處輸入圖片說明

這是我的下面的代碼

// 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

我究竟做錯了什么?

您需要先打開test密鑰,然后再讀取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);
        } 
    } 
} 

您試圖做的是在HostKeys Key下讀取一個稱為test的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM