Registry - Auslesen funktioniert nicht

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von TVX.

    Registry - Auslesen funktioniert nicht

    VB.NET-Quellcode

    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2. Using Key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player", RegistryKeyPermissionCheck.ReadSubTree)
    3. Dim DisplayVersion As String = CStr(Key.GetValue("DisplayVersion"))
    4. Label1.Text = DisplayVersion
    5. End Using
    6. End Sub


    Dieser Code funtzt leider nicht, ich kann einfach keine Werte aus diesem Teil der Registry auslesen, es kommt eine System.NullReferenceException.


    VB.NET-Quellcode

    1. DisplayVersion As String = CStr(Key.GetValue("DisplayVersion"))



    Dieser Teil wird dabei gelb hinterlegt O.o

    Wäre cool wenn ihr weiter helfen könntet :)

    Danke!

    P.S.: Ich verwende Visual Studio 2012, habe Windows 8.1 64-Bit, oben steht aber trotzdem x86, bin Admin und kann andere Keys auslesen, brauche aber unbedingt diesen hier!

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „reditec“ ()

    @reditec Key is Nothing.
    Du hast eine 64-Bit-Maschine und Dein Programm läuft als x64 oder AnyCPU.
    Schalte es um auf x86 und es läuft. :thumbsup:
    Bilder
    • VLC.png

      16,62 kB, 1.246×99, 337 mal angesehen
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    ErfinderDesRades schrieb:

    die NullReferenceException sagt dir, dass die Variable key Nothing ist.
    Daraus folgt, dass der von dir angegebene SchlüsselPfad (höchstwahrscheinlich) nicht existiert.

    Wozu gibt es wohl regedit...

    RodFromGermany schrieb:

    @reditec Key is Nothing.Du hast eine 64-Bit-Maschine und Dein Programm läuft als x64 oder AnyCPU.Schalte es um auf x86 und es läuft. :thumbsup:

    Läuft längst auf x86, ist aber eine x64 Maschiene.
    @RodFromGermany Dein Code hilft mir mehr, wenn man ihn kopieren kann...

    VB.NET-Quellcode

    1. Imports System.Net 'To be able to use network related functions.
    2. Imports Microsoft.Win32
    3. Public Class Form1
    4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5. Dim web As New WebClient 'To be able to download the content from the latest version file you have stored.
    6. Dim LatestVersion As String = web.DownloadString("http://www.comptalk.de/version.txt") 'To download the Lastest Version from a specified URL.
    7. Dim CurrentApp As String = My.Application.Info.Version.ToString 'Gets the applications current version
    8. Using Key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player", RegistryKeyPermissionCheck.ReadSubTree)
    9. Dim DisplayVersion As String = CStr(Key.GetValue("DisplayVersion"))
    10. MsgBox(DisplayVersion)
    11. End Using
    12. If CurrentApp < LatestVersion Then 'If the applications current version is less than the Latest version Then it will update, otherwise just do nothing or w/e you want.
    13. If MessageBox.Show("A new update is available!" & vbNewLine & "Would you like to update?", "LE21 - Auto Updater", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then 'Message box dialog asking the user if they wish to update or not.
    14. My.Computer.Network.DownloadFile("http://ftp.free.org/mirrors/videolan/vlc/last/win64/vlc-2.1.5-win64.exe", Application.StartupPath & "\Updates\update.exe", "", "", True, "2000", True) 'If they choose Yes, it will download the latest version exe
    15. MsgBox("The update has been downloaded!" & vbNewLine & "The application will now Exit.") 'telling the user the app will close
    16. System.Diagnostics.Process.Start("Updates\update.exe")
    17. End 'exits application
    18. Else
    19. 'If they choose No
    20. End
    21. End If
    22. Else
    23. MsgBox("Program is up to date") 'anything other than the current applications version being less than the latest version.
    24. End If
    25. End Sub
    26. End Class

    Nach 2 Sekunden stürtzt das Programm ab:
    Zusätzliche Informationen: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
    Was mache ich falsch?

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „reditec“ ()

    32 bit installation auslesen

    VB.NET-Quellcode

    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2. Using RegTyp As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
    3. Dim Key As RegistryKey = RegTyp.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player", False)
    4. Dim DisplayVersion As String = CStr(Key.GetValue("DisplayVersion"))
    5. Label1.Text = DisplayVersion
    6. End Using
    7. End Sub​


    64 Version - Zeile ändern !

    VB.NET-Quellcode

    1. Using RegTyp As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)​
    Dein Code hilft mir mehr, wenn man ihn kopieren kann...

    Wenn du fertig-Code haben willst, frag im Marktplatz. Des Weiteren lernst du mehr, wenn du es selbsr machst statt einfach nur alles zu kopieren ;)

    Was mache ich falsch?

    So ziemlich alles.
    - Du benutzt Option Strict Off -> Über die Imports oben bitte "Option Strict On" schreiben und alle neuen Fehler verbessern.
    - Du vergleichst Strings als wären es Zahlen -> Parse die Strings in Zahlen (Int.TryParse, etc)
    - Du benutzt den My-Namespace -> Statt My.Computer.Network.DownloadFile einfach den Webclient zum downloaden benutzen (web.DownloadFile)
    - Du benutzt MsgBox -> Benutz stattdessen MessageBox.Show("Nachricht")

    ​Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

    In welcher Zeile tritt dieser Fehler auf?

    reditec schrieb:

    Dein Code hilft mir mehr, wenn man ihn kopieren kann...
    C&P-Code fände ich auch hilfreich. ;(
    Ich hatte exakt Deinen Code.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Versuchs mal so :D und Any CPU ^^
    Benötigt nur - ProgressBar1 + Button1

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Strict On
    2. Imports Microsoft.Win32
    3. Imports System.Net
    4. Public Class Form1
    5. Private Installierte_VLC_Version, Neuste_VLC_Version As String
    6. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    7. TopMost = True
    8. ' Label1.Text = "-"
    9. ' Label2.Text = "-"
    10. End Sub
    11. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    12. Using RegTyp32 As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
    13. Using RegTyp64 As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    14. Dim Key32 As RegistryKey = RegTyp32.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player", False)
    15. If Key32 IsNot Nothing Then
    16. Installierte_VLC_Version = CStr(Key32.GetValue("DisplayVersion"))
    17. Else
    18. Dim Key64 As RegistryKey = RegTyp64.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player", False)
    19. If Key64 IsNot Nothing Then
    20. Installierte_VLC_Version = CStr(Key64.GetValue("DisplayVersion"))
    21. Else
    22. Installierte_VLC_Version = "0"
    23. End If
    24. End If
    25. End Using
    26. End Using
    27. Dim DownloadString As New Net.WebClient()
    28. DownloadString.Proxy = Nothing
    29. AddHandler DownloadString.DownloadStringCompleted, AddressOf DownloadStringCompleted
    30. DownloadString.DownloadStringAsync(New Uri("http://www.videolan.org/vlc/download-windows.de.html"))
    31. End Sub
    32. Private Sub DownloadStringCompleted(sender As Object, e As System.Net.DownloadStringCompletedEventArgs)
    33. If e.Error IsNot Nothing Then
    34. If Installierte_VLC_Version = "0" Then
    35. Dim result As DialogResult = MessageBox.Show("Es ist kein VLC Player installiert !" & vbNewLine & "Die neuste VLC Player Version konnte nicht ermittelt werden !" & vbNewLine & "Möchten Sie das VLC Player Update manuell laden ?", "Info !", MessageBoxButtons.YesNo)
    36. If result = DialogResult.Yes Then
    37. Process.Start("http://www.videolan.org/vlc/download-windows.de.html")
    38. End If
    39. Else
    40. Dim result As DialogResult = MessageBox.Show("Die installierte VLC Player Version ist " & Installierte_VLC_Version & vbNewLine & "Die neuste VLC Player Version konnte nicht ermittelt werden !" & vbNewLine & "Möchten Sie das VLC Player Update manuell laden ?", "Info !", MessageBoxButtons.YesNo)
    41. If result = DialogResult.Yes Then
    42. Process.Start("http://www.videolan.org/vlc/download-windows.de.html")
    43. End If
    44. End If
    45. Else
    46. If e.Result IsNot Nothing Then
    47. Neuste_VLC_Version = e.Result.Split({"<h1>Download latest VLC - "}, StringSplitOptions.None)(1).Split({"</h1>"}, StringSplitOptions.None)(CInt(("0")))
    48. If CInt(Installierte_VLC_Version.Replace(".", "")) < CInt(Neuste_VLC_Version.Replace(".", "")) Then
    49. If Installierte_VLC_Version = "0" Then
    50. Dim result As DialogResult = MessageBox.Show("Es ist kein VLC Player installiert !" & vbNewLine & "Soll die VLC Player Version " & Neuste_VLC_Version & " installiert werden werden ?", "Info !", MessageBoxButtons.YesNo)
    51. If result = DialogResult.Yes Then
    52. Download_new_VLC()
    53. End If
    54. Else
    55. Dim result As DialogResult = MessageBox.Show("Es ist ein neues VLC Player Update verfügbar !" & vbNewLine & "Von Version " & Installierte_VLC_Version & " auf Version " & Neuste_VLC_Version & vbNewLine & "Soll das Update gestartet werden ?", "Info !", MessageBoxButtons.YesNo)
    56. If result = DialogResult.Yes Then
    57. Download_new_VLC()
    58. End If
    59. End If
    60. Else
    61. Dim result As DialogResult = MessageBox.Show("Es ist kein VLC Player Update nötig !" & vbNewLine & "Soll das VLC Player Update den noch gestartet werden ?", "Info !", MessageBoxButtons.YesNo)
    62. If result = DialogResult.Yes Then
    63. Download_new_VLC()
    64. End If
    65. End If
    66. Else
    67. If Installierte_VLC_Version = "0" Then
    68. Dim result As DialogResult = MessageBox.Show("Es ist kein VLC Player installiert !" & vbNewLine & "Die neuste VLC Player Version konnte nicht ermittelt werden !" & vbNewLine & "Möchten Sie das VLC Player Update manuell laden ?", "Info !", MessageBoxButtons.YesNo)
    69. If result = DialogResult.Yes Then
    70. Process.Start("http://www.videolan.org/vlc/download-windows.de.html")
    71. End If
    72. Else
    73. Dim result As DialogResult = MessageBox.Show("Die installierte VLC Player Version ist " & Installierte_VLC_Version & vbNewLine & "Die neuste VLC Player Version konnte nicht ermittelt werden !" & vbNewLine & "Möchten Sie das VLC Player Update manuell laden ?", "Info !", MessageBoxButtons.YesNo)
    74. If result = DialogResult.Yes Then
    75. Process.Start("http://www.videolan.org/vlc/download-windows.de.html")
    76. End If
    77. End If
    78. End If
    79. End If
    80. End Sub
    81. Private WithEvents DownloadClient As New WebClient
    82. Private Sub Download_new_VLC()
    83. Try
    84. If Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\0").GetValue("Identifier").ToString.Contains("64") = True Then '32 - 64 bit bestimmen und downloaden
    85. DownloadClient.DownloadFileAsync(New Uri("http://mirror.netcologne.de/videolan.org/vlc/" & Neuste_VLC_Version & "/win64/vlc-" & Neuste_VLC_Version & "-win64.exe"), IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "VLC_Player_Update_64_bit.exe"))
    86. Else
    87. DownloadClient.DownloadFileAsync(New Uri("http://mirror.netcologne.de/videolan.org/vlc/" & Neuste_VLC_Version & "/win32/vlc-" & Neuste_VLC_Version & "-win32.exe"), IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "VLC_Player_Update_32_bit.exe"))
    88. End If
    89. Catch ex As Exception
    90. MessageBox.Show(ex.Message, "Download Fehler !")
    91. End Try
    92. End Sub
    93. Private Sub DownloadClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles DownloadClient.DownloadProgressChanged
    94. ' Label1.Text = e.ProgressPercentage & " %"
    95. ProgressBar1.Value = e.ProgressPercentage
    96. ' Label2.Text = (e.BytesReceived / 1024 / 1024).ToString("0.##") & " MB von " & (e.TotalBytesToReceive / 1024 / 1024).ToString("0.##") & " MB geladen"
    97. End Sub
    98. Private Sub DownloadClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles DownloadClient.DownloadFileCompleted
    99. Dim timeOut As Date = Date.Now().AddMilliseconds(1000)
    100. While timeOut > Date.Now
    101. Application.DoEvents()
    102. End While
    103. ProgressBar1.Value = 0
    104. ' Label1.Text = "-"
    105. ' Label2.Text = "-"
    106. Try
    107. If Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\0").GetValue("Identifier").ToString.Contains("64") = True Then
    108. Process.Start(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "VLC_Player_Update_64_bit.exe"))
    109. Else
    110. Process.Start(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "VLC_Player_Update_32_bit.exe"))
    111. End If
    112. Catch ex As Exception
    113. MessageBox.Show(ex.Message, "Info !")
    114. End Try
    115. Me.Close()
    116. End Sub
    117. End Class

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „TVX“ ()