由于要適配xp,win7,win10等系統,選擇了.framework3.5 ,編譯選項是Mixed Platforms
查詢的資料是32位的程式無法直接讀取64位系統的注冊表,網上找的代碼跑的結果讀取不到對應的值
class ReadRegedit64
{
#region 32位程式讀寫64注冊表
static UIntPtr HKEY_CLASSES_ROOT = (UIntPtr)0x80000000;
static UIntPtr HKEY_CURRENT_USER = (UIntPtr)0x80000001;
static UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
static UIntPtr HKEY_USERS = (UIntPtr)0x80000003;
static UIntPtr HKEY_CURRENT_CONFIG = (UIntPtr)0x80000005;
// 關閉64位(檔案系統)的操作轉向
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
// 開啟64位(檔案系統)的操作轉向
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
// 獲取操作Key值句柄
[DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out IntPtr phkResult);
//關閉注冊表轉向(禁用特定項的注冊表反射)
[DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern long RegDisableReflectionKey(IntPtr hKey);
//使能注冊表轉向(開啟特定項的注冊表反射)
[DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern long RegEnableReflectionKey(IntPtr hKey);
//獲取Key值(即:Key值句柄所標志的Key物件的值)
[DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved,
out uint lpType, System.Text.StringBuilder lpData,
ref uint lpcbData);
private static UIntPtr TransferKeyName(string keyName)
{
switch (keyName)
{
case "HKEY_CLASSES_ROOT":
return HKEY_CLASSES_ROOT;
case "HKEY_CURRENT_USER":
return HKEY_CURRENT_USER;
case "HKEY_LOCAL_MACHINE":
return HKEY_LOCAL_MACHINE;
case "HKEY_USERS":
return HKEY_USERS;
case "HKEY_CURRENT_CONFIG":
return HKEY_CURRENT_CONFIG;
}
return HKEY_CLASSES_ROOT;
}
public static string Get64BitRegistryKey(string parentKeyName, string subKeyName, string keyName)
{
int KEY_QUERY_VALUE = (0x0001);
int KEY_WOW64_64KEY = (0x0100);
int KEY_ALL_WOW64 = (KEY_QUERY_VALUE | KEY_WOW64_64KEY);
try
{
//將Windows注冊表主鍵名轉化成為不帶正負號的整形句柄(與平臺是32或者64位有關)
UIntPtr hKey = TransferKeyName(parentKeyName);
//宣告將要獲取Key值的句柄
IntPtr pHKey = IntPtr.Zero;
//記錄讀取到的Key值
StringBuilder result = new StringBuilder("".PadLeft(1024));
uint resultSize = 1024;
uint lpType = 0;
//關閉檔案系統轉向
IntPtr oldWOW64State = new IntPtr();
if (Wow64DisableWow64FsRedirection(ref oldWOW64State))
{
//獲得操作Key值的句柄
RegOpenKeyEx(hKey, subKeyName, 0, KEY_ALL_WOW64, out pHKey);
//關閉注冊表轉向(禁止特定項的注冊表反射)
RegDisableReflectionKey(pHKey);
//獲取訪問的Key值
RegQueryValueEx(pHKey, keyName, 0, out lpType, result, ref resultSize);
//打開注冊表轉向(開啟特定項的注冊表反射)
RegEnableReflectionKey(pHKey);
}
//打開檔案系統轉向
Wow64RevertWow64FsRedirection(oldWOW64State);
//回傳Key值
return result.ToString().Trim();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return null;
}
}
#endregion
}
有大佬給點建議怎么做么
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/31311.html
