C#操作登錄檔簡單教程(附帶操作某資料庫客戶端登錄檔以實現重置試用期的效果)

WeskyNet發表於2023-02-20

 前言:

使用Windows系統,經常會遇到需要做登錄檔的操作。例如,一些軟體需要修改登錄檔、自己編寫的軟體需要新建登錄檔進行寫入註冊資訊等等。以下內容以某常見的資料庫操作客戶端為例,做一個登錄檔操作的學習。

 

1、例如,找一個已經登錄檔過期的軟體,記錄一下時間和當前過期的效果。 

 

 

2、參考C#操作登錄檔官方文件:

https://learn.microsoft.com/zh-cn/dotnet/api/microsoft.win32.registrykey?redirectedfrom=MSDN&view=net-6.0

 

3、官方示例程式碼:

using System;
using System.Security.Permissions;
using Microsoft.Win32;
​
class RegKey
{
    static void Main()
    {
        // Create a subkey named Test9999 under HKEY_CURRENT_USER.
        RegistryKey test9999 =
            Registry.CurrentUser.CreateSubKey("Test9999");
        // Create two subkeys under HKEY_CURRENT_USER\Test9999. The
        // keys are disposed when execution exits the using statement.
        using(RegistryKey
            testName = test9999.CreateSubKey("TestName"),
            testSettings = test9999.CreateSubKey("TestSettings"))
        {
            // Create data for the TestSettings subkey.
            testSettings.SetValue("Language", "French");
            testSettings.SetValue("Level", "Intermediate");
            testSettings.SetValue("ID", 123);
        }
​
        // Print the information from the Test9999 subkey.
        Console.WriteLine("There are {0} subkeys under {1}.",
            test9999.SubKeyCount.ToString(), test9999.Name);
        foreach(string subKeyName in test9999.GetSubKeyNames())
        {
            using(RegistryKey
                tempKey = test9999.OpenSubKey(subKeyName))
            {
                Console.WriteLine("\nThere are {0} values for {1}.",
                    tempKey.ValueCount.ToString(), tempKey.Name);
                foreach(string valueName in tempKey.GetValueNames())
                {
                    Console.WriteLine("{0,-8}: {1}", valueName,
                        tempKey.GetValue(valueName).ToString());
                }
            }
        }
​
        using(RegistryKey
            testSettings = test9999.OpenSubKey("TestSettings", true))
        {
            // Delete the ID value.
            testSettings.DeleteValue("id");
​
            // Verify the deletion.
            Console.WriteLine((string)testSettings.GetValue(
                "id", "ID not found."));
        }
​
        // Delete or close the new subkey.
        Console.Write("\nDelete newly created registry key? (Y/N) ");
        if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
        {
            Registry.CurrentUser.DeleteSubKeyTree("Test9999");
            Console.WriteLine("\nRegistry key {0} deleted.",
                test9999.Name);
        }
        else
        {
            Console.WriteLine("\nRegistry key {0} closed.",
                test9999.ToString());
            test9999.Close();
        }
    }
}

 

 4、做一個測試清理Navicat登錄檔的demo,當作學習使用。

核心程式碼:

RegistryKey regKey = Registry.CurrentUser;
​
RegistryKey regKey2 = regKey.OpenSubKey(@"Software\Classes\CLSID",true);
    string[] keyNameColl;
    keyNameColl = regKey2.GetSubKeyNames();
    List<string> keyNames = new List<string>();
    foreach (string key in keyNameColl) { 
        var subKeys = regKey2.OpenSubKey(key,true);
        var subKeyNames = subKeys.GetSubKeyNames();
        if(subKeyNames!=null && subKeyNames.Length==1 && subKeyNames[0] == "Info")
        {
            keyNames.Add(key);
        }
        subKeys?.Close();
    }
    foreach (var key in keyNames) {
        regKey2.DeleteSubKeyTree(key,false);
        Console.WriteLine($"[HKEY_CURRENT_USER\\Software\\Classes\\CLSID\\{key}]");
    }
    regKey2.Close();
    regKey.Close();

 

 

 

5、寫好程式碼以後,編譯成功即可使用。

 

 

6、執行完畢以後,進行重寫開啟Navicat15驗證下,發現試用期被重置了,說明登錄檔操作是成功的。

 

 

【警告】該操作僅用於學習用途,旨在幫助大家熟悉使用C#進行登錄檔的操作,嚴禁用於其他非法用途。

如果需要登錄檔操作的原始碼,可以在公眾號【Dotnet Dancer】後臺回覆【清理登錄檔】即可獲取。

相關文章