CSDN博客

img slightboy

VB.NET 轻松操作注册表

发表于2004/10/29 12:24:00  696人阅读

在.NET中操作注册表, 和VS STUDIO 时代,真是不可同日而语, .NET 之前的操作, 要引入大量的 API 函数,含有键值类型, 错误类型等, 繁琐的要死, 而.NET中,实现起来的简单程度,已经和 DELPHI 基本相同了,下面通过例子体验一下:



代码:
------------------------------------------------------------------------------------
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

0 0

相关博文

我的热门文章

img
取 消
img