VB.NET的出现可以说很大程度上改变了开发人员原有的摆弄些代码方式,为编程人员带来了一个良好的编程环境。对于VB.NET操作注册表, 和VS STUDIO 时代,真是不可同日而语, .NET 之前的操作, 要引入大量的 API 函数,含有键值类型, 错误类型等, 繁琐的要死, 而.NET中,实现起来的简单程度,已经和 DELPHI 基本相同了,下面通过例子体验一下: #t#
VB.NET操作注册表代码:
- Imports Microsoft.Win32.Registry
- Public Class frmMain
- Inherits System.Windows.Forms.Form
- Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button2.Click - Me.Dispose(True)
- End Sub
- Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button1.Click - Dim reg As Microsoft.Win32.RegistryKey
- reg = CurrentUser.OpenSubKey("Software\Microsoft
\Windows\CurrentVersion\Run", True) - If reg.GetValue("test") <> "" Then
- MsgBox("改键值已经存在", MsgBoxStyle.Information, "提示")
- Else
- reg.SetValue("test", txtKeyValue.Text)
- MsgBox("设置成功!", MsgBoxStyle.Information, "提示")
- End If
- reg.Close()
- reg = Nothing
- End Sub
- Private Sub Button3_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button3.Click - Dim reg As Microsoft.Win32.RegistryKey
- reg = CurrentUser.OpenSubKey("Software\
Microsoft\Windows\CurrentVersion\Run", True) - If reg.GetValue("test") <> "" Then
- reg.DeleteValue("test")
- MsgBox("已删除")
- Else
- MsgBox("不存在")
- End If
- reg.Close()
- reg = Nothing
- End Sub
- Private Sub chkAutoStart_CheckedChanged
(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles chkAutoStart.
CheckedChanged - Dim regKey As Microsoft.Win32.RegistryKey
- regKey = CurrentUser.OpenSubKey("Software
\Microsoft\Windows\CurrentVersion\Run", True) - If chkAutoStart.Checked Then
- If regKey.GetValue("sjcatsoft") = "" Then
- regKey.SetValue("sjcatsoft",
Application.ExecutablePath) - End If
- Else
- If regKey.GetValue("sjcatsoft") <> "" Then
- regKey.DeleteValue("sjcatsoft")
- End If
- End If
- regKey.Close()
- regKey = Nothing
- End Sub
- End Class
VB.NET操作注册表的具体实现方式就为大家介绍到这里。