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 !!!
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
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!
Vous n’avez pas trouvé la réponse que vous recherchez ?
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 !