如何用VB.NET查询Windows注册表中是否存在特定密钥?
- 内容介绍
- 文章标签
- 相关推荐
本文共计253个文字,预计阅读时间需要2分钟。
在VB.NET中,可以在Windows注册表中创建一个键,例如:`My.Computer.Registry.CurrentUser.CreateSubKey(TestKey)`。同时,可以检查一个键是否存在,如下所示:`If My.Computer.Registry.GetValue(HKEY_LOCAL_MACHINE, , 0) IsNot Nothing Then`。
在VB.NET中,我可以在 Windows注册表中创建一个键,如下所示:My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
我可以检查一个键中是否存在这样的值:
If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\MyKey", _ "TestValue", Nothing) Is Nothing Then MsgBox("Value does not exist.") Else MsgBox("Value exist.") End If
但是,如何检查注册表中是否存在具有特定名称的密钥?
一种方法是使用Registry.OpenSubKey方法If Microsoft.Win32.Registry.LocalMachine.OpenSubKey("TestKey") Is Nothing Then ' Key doesn't exist Else ' Key existed End If
但是我会建议你不要走这条路.返回Nothing的OpenSubKey方法意味着密钥在过去的某个时刻不存在.当方法返回另一个程序中的另一个操作时,可能导致创建密钥.
我会直接去CreateSubKey,而不是检查密钥存在并在事后创建它.
本文共计253个文字,预计阅读时间需要2分钟。
在VB.NET中,可以在Windows注册表中创建一个键,例如:`My.Computer.Registry.CurrentUser.CreateSubKey(TestKey)`。同时,可以检查一个键是否存在,如下所示:`If My.Computer.Registry.GetValue(HKEY_LOCAL_MACHINE, , 0) IsNot Nothing Then`。
在VB.NET中,我可以在 Windows注册表中创建一个键,如下所示:My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
我可以检查一个键中是否存在这样的值:
If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\MyKey", _ "TestValue", Nothing) Is Nothing Then MsgBox("Value does not exist.") Else MsgBox("Value exist.") End If
但是,如何检查注册表中是否存在具有特定名称的密钥?
一种方法是使用Registry.OpenSubKey方法If Microsoft.Win32.Registry.LocalMachine.OpenSubKey("TestKey") Is Nothing Then ' Key doesn't exist Else ' Key existed End If
但是我会建议你不要走这条路.返回Nothing的OpenSubKey方法意味着密钥在过去的某个时刻不存在.当方法返回另一个程序中的另一个操作时,可能导致创建密钥.
我会直接去CreateSubKey,而不是检查密钥存在并在事后创建它.

