在VB.NET中,我可以在 Windows注册表中创建一个键,如下所示: My.Computer.Registry.CurrentUser.CreateSubKey("TestKey") 我可以检查一个键中是否存在这样的值: If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHIN
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,而不是检查密钥存在并在事后创建它.
