Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.574
  • Views Posts: 542.202
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

VB.NET - FUNZIONE CHE CANCELLA IL VALORE DELLA CHIAVE DI REGISTRO (CODICE)

VB.NET - FUNZIONE CHE CANCELLA IL VALORE DELLA CHIAVE DI REGISTRO (CODICE)

  

Oggi vi voglio mettere a disposizione tre metodi di Cancellazione delle chiavi di Registro, sfruttando la Funzione My.Computer.Registry o Microsoft.Win32.Registry.

Esempio di richiamo della funzione :

        CancellaChiaveDiRegistroValoreSpecifico(ValoriChiaveRegistro.LocalMachine, "SOFTWARE\programalpha", "prova")
        CancellaChiaveDiRegistroValoreSpecificoNew(ValoriChiaveRegistro.LocalMachine, "SOFTWARE\programalpha", "prova")
        CancellaSottoChiaveDiRegistroNew(ValoriChiaveRegistro.LocalMachine, "SOFTWARE\programalpha")
   

CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        CancellaChiaveDiRegistroValoreSpecifico(ValoriChiaveRegistro.LocalMachine, "SOFTWARE\programalpha", "prova")
        CancellaChiaveDiRegistroValoreSpecificoNew(ValoriChiaveRegistro.LocalMachine, "SOFTWARE\programalpha", "prova")
        CancellaSottoChiaveDiRegistroNew(ValoriChiaveRegistro.LocalMachine, "SOFTWARE\programalpha")
   
    End Sub

   Private Enum ValoriChiaveRegistro As Integer
        ClassesRoot = 0
        CurrentUser = 1
        LocalMachine = 2
        Users = 3
        CurrentConfig = 4
        PerformanceData = 5
    End Enum


    Private Sub CancellaChiaveDiRegistroValoreSpecifico(ByVal CartellaRoot As ValoriChiaveRegistro, ByVal PercorsoChiaveRegistro As String, ByVal ChiaveValore As String)

        Dim regKey As Microsoft.Win32.RegistryKey = Nothing
        Try
            Select Case CartellaRoot
                Case 0
                    regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(PercorsoChiaveRegistro, True)
                Case 1
                    regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(PercorsoChiaveRegistro, True)
                Case 2
                    regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(PercorsoChiaveRegistro, True)
                Case 3
                    regKey = Microsoft.Win32.Registry.Users.OpenSubKey(PercorsoChiaveRegistro, True)
                Case 4
                    regKey = Microsoft.Win32.Registry.CurrentConfig.OpenSubKey(PercorsoChiaveRegistro, True)
                Case 5
                    regKey = Microsoft.Win32.Registry.PerformanceData.OpenSubKey(PercorsoChiaveRegistro, True)
                Case Else
                    regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(PercorsoChiaveRegistro, True)
            End Select
            If regKey IsNot Nothing Then
                regKey.DeleteValue(ChiaveValore)
            End If
        Catch ex As Exception
        Finally
            If regKey IsNot Nothing Then regKey.Close()
        End Try
    End Sub

    Private Function CancellaSottoChiaveDiRegistroNew(ByVal CartellaRoot As ValoriChiaveRegistro, ByVal PercorsoChiaveRegistro As String) As Boolean

        Try
            Select Case CartellaRoot
                Case 0
                    My.Computer.Registry.ClassesRoot.DeleteSubKey(PercorsoChiaveRegistro, True)
                Case 1
                    My.Computer.Registry.CurrentUser.DeleteSubKey(PercorsoChiaveRegistro, True)
                Case 2
                    My.Computer.Registry.LocalMachine.DeleteSubKey(PercorsoChiaveRegistro, True)
                Case 3
                    My.Computer.Registry.Users.DeleteSubKey(PercorsoChiaveRegistro, True)
                Case 4
                    My.Computer.Registry.CurrentConfig.DeleteSubKey(PercorsoChiaveRegistro, True)
                Case 5
                    My.Computer.Registry.PerformanceData.DeleteSubKey(PercorsoChiaveRegistro, True)
                Case Else
                    My.Computer.Registry.LocalMachine.DeleteSubKey(PercorsoChiaveRegistro, True)
            End Select
            Return True

        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CancellaChiaveDiRegistroValoreSpecificoNew(ByVal CartellaRoot As ValoriChiaveRegistro, ByVal PercorsoChiaveRegistro As String, ByVal ValoreChiave As String) As Boolean

        Try
            Select Case CartellaRoot
                Case 0
                    My.Computer.Registry.ClassesRoot.OpenSubKey(PercorsoChiaveRegistro, True).DeleteValue(ValoreChiave, True)
                Case 1
                    My.Computer.Registry.CurrentUser.OpenSubKey(PercorsoChiaveRegistro, True).DeleteValue(ValoreChiave, True)
                Case 2
                    My.Computer.Registry.LocalMachine.OpenSubKey(PercorsoChiaveRegistro, True).DeleteValue(ValoreChiave, True)
                Case 3
                    My.Computer.Registry.Users.OpenSubKey(PercorsoChiaveRegistro, True).DeleteValue(ValoreChiave, True)
                Case 4
                    My.Computer.Registry.CurrentConfig.OpenSubKey(PercorsoChiaveRegistro, True).DeleteValue(ValoreChiave, True)
                Case 5
                    My.Computer.Registry.PerformanceData.OpenSubKey(PercorsoChiaveRegistro, True).DeleteValue(ValoreChiave, True)
                Case Else
                    My.Computer.Registry.LocalMachine.OpenSubKey(PercorsoChiaveRegistro, True).DeleteValue(ValoreChiave, True)
            End Select
            Return True

        Catch ex As Exception
            Return False
        End Try
    End Function
    
End Class

By ImaginSystems & Queen GIn
Categoria: VB.NET
martedì, 17 lug 2012 Ore. 18.52

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003