我有一個函式 ( OpenSubKeySymLink) 接收這種變數:this RegistryKey key.
我不知道如何初始化它并將其傳遞給函式。
public static RegistryKey OpenSubKeySymLink(this RegistryKey key, string name, RegistryRights rights = RegistryRights.ReadKey, RegistryView view = 0)
{
var error = RegOpenKeyExW(key.Handle, name, REG_OPTION_OPEN_LINK, ((int)rights) | ((int)view), out var subKey);
if (error != 0)
{
subKey.Dispose();
throw new Win32Exception(error);
}
return RegistryKey.FromHandle(subKey); // RegistryKey will dispose subKey
}
static void Main(string[] args)
{
RegistryKey key; // how to initialize it?
OpenSubKeySymLink(key, @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\myKey", RegistryRights.ReadKey, 0);
}
uj5u.com熱心網友回復:
抱歉,我在之前的回答中沒有完全解釋如何使用此代碼。
我將其撰寫為擴展,因此您可以在現有子鍵或主鍵之一上呼叫它,例如Registry.CurrentUser.
例如,您可以像這樣使用它:
using (var key = Registry.CurrentUser.OpenSubKeySymLink(@"SOFTWARE\Microsoft\myKey", RegistryRights.ReadKey))
{
// do stuff with key
}
您顯然仍然可以將其用作非擴展功能
using (var key = OpenSubKeySymLink(Registry.CurrentUser, @"SOFTWARE\Microsoft\myKey", RegistryRights.ReadKey))
{
// do stuff with key
}
- 注意可選引數,這意味著某些引數不需要傳遞,因為它們具有默認值。
- 請注意在
using回傳的 上的使用key,以便正確處理它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411751.html
標籤:
