Vérifier l'existence clé registre

Résolu
edwinzap Messages postés 149 Date d'inscription samedi 29 septembre 2012 Statut Membre Dernière intervention 21 juin 2016 - 20 déc. 2013 à 12:19
edwinzap Messages postés 149 Date d'inscription samedi 29 septembre 2012 Statut Membre Dernière intervention 21 juin 2016 - 20 déc. 2013 à 17:15
Bonjour,

Voici mon problème. En cherchant comment faire pour manipuler le registre, j'ai réussi à faire ce code:
Private Sub DémarrageAuto_True()
        Try

            Dim Cle As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
            Cle.SetValue("Compteur", My.Application.Info.DirectoryPath & "\Compteur.exe")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


Je crée ainsi la clé dans le registre.

Je voudrais vérifier l'existence de cette clé avec ce code:
Try
                DémarrageAuto_True()
                Dim test As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Compteur", True)

                If test Is Nothing Then
                    MsgBox("la clé n'existe pas")
                Else
                    MsgBox("la clé existe")
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try


Le test me renvoit à chaque fois Nothing alors que je viens juste de la créer !!!

Aidez moi svp, merci !

5 réponses

jordane45 Messages postés 38162 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 mai 2024 344
20 déc. 2013 à 12:48
Bonjour,
Essayes peut-être de doubler les anti-Slash.
0
jordane45 Messages postés 38162 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 6 mai 2024 344
20 déc. 2013 à 12:57
0
edwinzap Messages postés 149 Date d'inscription samedi 29 septembre 2012 Statut Membre Dernière intervention 21 juin 2016 2
20 déc. 2013 à 14:38
J'ai tout regarder, et ça ne marche tjrs pas !
J'ai modifié le code:

Création de la clé:
            Dim Cle As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")
            Cle.SetValue("Compteur", My.Application.Info.DirectoryPath & "\Compteur.exe", RegistryValueKind.String)


Vérification que la clé existe:
                Dim value As String
                value = Microsoft.Win32.Registry.CurrentUser.GetValue("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\Compteur")

                If value IsNot Nothing Then
                    MsgBox("la clé existe")
                Else
                    MsgBox("la clé n'existe pas")
                End If


Il me renvoi tjrs Nothing !
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
20 déc. 2013 à 15:33
Bonjour,

Essaie cela:

 Dim test As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run\Compteur",False)
If test Is Nothing Then
MsgBox("la clé existe")
Else
MsgBox("la clé n'existe pas")
End If

Il faut essayer d'ouvrir la clé pour voir si elle existe!
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
edwinzap Messages postés 149 Date d'inscription samedi 29 septembre 2012 Statut Membre Dernière intervention 21 juin 2016 2
20 déc. 2013 à 17:15
Effectivement, il fallait essayer d'ouvrir la clé. Merci à Jordane45 pour les liens. Cela m'a fortement aidé.
En fait je ne comprenais pas correctement la hiérarchie de la base de registre. Voici mon code:

Dim key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", True)
            Dim value = key.GetValue("compteur")
            If value Is Nothing Then
                key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")
                key.SetValue("Compteur", My.Application.Info.DirectoryPath & "\Compteur.exe", RegistryValueKind.String)
                key.Close()
                Me.DémarrageAutoToolStripMenuItem.Text = "&Désactiver"
            Else
                key.DeleteValue("Compteur")
                Me.DémarrageAutoToolStripMenuItem.Text = "&Activer"
            End If


Encore merci !

Sujet résolu, mais je suis tjrs preneur pour des améliorations du code !
0
Rejoignez-nous