RegistryKey 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示Windows注册表中的键级节点。 此类是注册表封装。
public ref class RegistryKey sealed : MarshalByRefObject, IDisposable
public ref class RegistryKey sealed : IDisposable
public sealed class RegistryKey : MarshalByRefObject, IDisposable
public sealed class RegistryKey : IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class RegistryKey : MarshalByRefObject, IDisposable
type RegistryKey = class
inherit MarshalByRefObject
interface IDisposable
type RegistryKey = class
interface IDisposable
[<System.Runtime.InteropServices.ComVisible(true)>]
type RegistryKey = class
inherit MarshalByRefObject
interface IDisposable
Public NotInheritable Class RegistryKey
Inherits MarshalByRefObject
Implements IDisposable
Public NotInheritable Class RegistryKey
Implements IDisposable
- 继承
- 继承
-
RegistryKey
- 属性
- 实现
示例
下面的代码示例演示如何在HKEY_CURRENT_USER下创建子项,操作其内容,然后删除子项。
using namespace System;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;
int main()
{
// Create a subkey named Test9999 under HKEY_CURRENT_USER.
RegistryKey ^ test9999 = Registry::CurrentUser->CreateSubKey( "Test9999" );
// Create two subkeys under HKEY_CURRENT_USER\Test9999.
test9999->CreateSubKey( "TestName" )->Close();
RegistryKey ^ testSettings = test9999->CreateSubKey( "TestSettings" );
// Create data for the TestSettings subkey.
testSettings->SetValue( "Language", "French" );
testSettings->SetValue( "Level", "Intermediate" );
testSettings->SetValue( "ID", 123 );
testSettings->Close();
// Print the information from the Test9999 subkey.
Console::WriteLine( "There are {0} subkeys under Test9999.", test9999->SubKeyCount.ToString() );
array<String^>^subKeyNames = test9999->GetSubKeyNames();
for ( int i = 0; i < subKeyNames->Length; i++ )
{
RegistryKey ^ tempKey = test9999->OpenSubKey( subKeyNames[ i ] );
Console::WriteLine( "\nThere are {0} values for {1}.", tempKey->ValueCount.ToString(), tempKey->Name );
array<String^>^valueNames = tempKey->GetValueNames();
for ( int j = 0; j < valueNames->Length; j++ )
{
Console::WriteLine( "{0,-8}: {1}", valueNames[ j ], tempKey->GetValue( valueNames[ j ] )->ToString() );
}
}
// Delete the ID value.
testSettings = test9999->OpenSubKey( "TestSettings", true );
testSettings->DeleteValue( "id" );
// Verify the deletion.
Console::WriteLine( dynamic_cast<String^>(testSettings->GetValue( "id", "ID not found." )) );
testSettings->Close();
// 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();
}
}
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();
}
}
}
Imports System.Security.Permissions
Imports Microsoft.Win32
Public Class RegKey
Shared Sub Main()
' Create a subkey named Test9999 under HKEY_CURRENT_USER.
Dim test9999 As RegistryKey = _
Registry.CurrentUser.CreateSubKey("Test9999")
' Create two subkeys under HKEY_CURRENT_USER\Test9999.
test9999.CreateSubKey("TestName").Close()
Dim testSettings As RegistryKey = _
test9999.CreateSubKey("TestSettings")
' Create data for the TestSettings subkey.
testSettings.SetValue("Language", "French")
testSettings.SetValue("Level", "Intermediate")
testSettings.SetValue("ID", 123)
testSettings.Close()
' Print the information from the Test9999 subkey.
Console.WriteLine("There are {0} subkeys under Test9999.", _
test9999.SubKeyCount.ToString())
For Each subKeyName As String In test9999.GetSubKeyNames()
Dim tempKey As RegistryKey = _
test9999.OpenSubKey(subKeyName)
Console.WriteLine(vbCrLf & "There are {0} values for " & _
"{1}.", tempKey.ValueCount.ToString(), tempKey.Name)
For Each valueName As String In tempKey.GetValueNames()
Console.WriteLine("{0,-8}: {1}", valueName, _
tempKey.GetValue(valueName).ToString())
Next
Next
' Delete the ID value.
testSettings = test9999.OpenSubKey("TestSettings", True)
testSettings.DeleteValue("id")
' Verify the deletion.
Console.WriteLine(CType(testSettings.GetValue( _
"id", "ID not found."), String))
testSettings.Close()
' Delete or close the new subkey.
Console.Write(vbCrLf & "Delete newly created " & _
"registry key? (Y/N) ")
If Char.ToUpper(Convert.ToChar(Console.Read())) = "Y"C Then
Registry.CurrentUser.DeleteSubKeyTree("Test9999")
Console.WriteLine(vbCrLf & "Registry key {0} deleted.", _
test9999.Name)
Else
Console.WriteLine(vbCrLf & "Registry key {0} closed.", _
test9999.ToString())
test9999.Close()
End If
End Sub
End Class
注解
若要获取其实例 RegistryKey,请使用类的 Registry 一个静态成员。
注册表充当计算机上的操作系统和应用程序的信息的中央存储库。 注册表按分层格式进行组织,具体取决于存储在其中的元素的逻辑顺序(请参阅 Registry 此层次结构中的基级别项)。 在注册表中存储信息时,请根据要存储的信息类型选择适当的位置。 请务必避免销毁其他应用程序创建的信息,因为这可能会导致这些应用程序出现意外行为,并且也会对自己的应用程序产生负面影响。
Important
此类型实现 IDisposable 接口。 使用完该类型后,应直接或间接处理它。 若要直接释放类型,请在块中Disposetry/调用其catch方法。 若要间接释放它,请使用语言构造,例如 using (在 C# 中)或 Using (在 Visual Basic 中)。 有关详细信息,请参阅接口主题中的 IDisposable “使用实现 IDisposable 的对象”部分。
注册表项是注册表中组织的基本单元,可与文件资源管理器中的文件夹进行比较。 特定键可以具有子项,就像文件夹可以具有子文件夹一样。 只要用户具有相应的权限即可删除每个密钥,并且密钥不是基键或直接位于基键下的级别。 每个键还可以具有与其关联的多个值(一个值可以与一个文件进行比较),该文件用于存储信息 -例如,有关计算机上安装的应用程序的信息。 每个值都包含一条特定的信息,如果需要,可以检索或更新这些信息。 例如,可以在公司的密钥 HKEY_LOCAL_MACHINE\Software下为公司创建一个 RegistryKey 子项,然后为公司创建的每个应用程序创建一个子项。 每个子项保存特定于该应用程序的信息,例如颜色设置、屏幕位置和大小或识别的文件扩展名。
请注意,存储在注册表中的信息可供其他应用程序和用户使用,因此不应用于存储安全数据或关键应用程序信息。
Caution
不要以恶意程序可以创建数千个毫无意义的子项或键/值对的方式公开 RegistryKey 对象。 例如,不允许调用方输入任意键或值。
从 .NET Framework 4 开始,注册表项的长度不再限制为 255 个字符。
属性
| 名称 | 说明 |
|---|---|
| Handle |
获取一个 SafeRegistryHandle 对象,该对象表示当前 RegistryKey 对象封装的注册表项。 |
| Name |
检索密钥的名称。 |
| SubKeyCount |
检索当前键的子项计数。 |
| ValueCount |
检索键中的值计数。 |
| View |
获取用于创建注册表项的视图。 |
方法
显式接口实现
| 名称 | 说明 |
|---|---|
| IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。 对当前键执行 a Close() 。 |
扩展方法
| 名称 | 说明 |
|---|---|
| GetAccessControl(RegistryKey, AccessControlSections) |
返回注册表项的安全信息。 |
| GetAccessControl(RegistryKey) |
返回注册表项的安全信息。 |
| SetAccessControl(RegistryKey, RegistrySecurity) |
更改现有注册表项的安全属性。 |