VB.NET輕鬆操作註冊表

來源:互聯網
上載者:User
VB.NET輕鬆操作註冊表

在.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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.