如何将注册表值读入标签或消息框

How to read a registry value into a label or msgbox

我只需要将 windows 原始安装日期的注册表中的 DWord 值读取到标签或文本框中,以便我可以在其他地方使用它。我正在尝试使用安装日期创建倒计时。

我在 Google 上搜索了很多,观看了 YouTube 视频,复制并粘贴了一堆示例代码,看看是否可以理解我需要什么来实现这个目标以及什么是可行的

这可能是我第 20 次尝试

Dim InstallDate As Long

    With New RegRead
        .OpenKey(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", KEY64)
        InstallDate =("InstallDate")
        .CloseKey()
    End With

    Label1.text=("Install date: " & Format$(DateAdd("s", InstallDate, #1/1/1970#), "Short Date"))

我希望原始安装日期以十进制或十六进制值形式出现在标签 1 中。

但它说 'HKEY_LOCAL_MACHINE' 未声明。由于其保护级别,它可能无法访问,Microsoft 是一个命名空间,不能用作表达式。我在 Windows 7 64 位,注册表版本 5。如果有帮助。

我迷路了。请帮忙(我只是个菜鸟)

试试这个:

Dim readValue = My.Computer.Registry.GetValue(
    "HKEY_CURRENT_USER\Software\MyApp", "Name", Nothing)
MsgBox("The value is " & readValue)

来源:https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/computer-resources/how-to-read-a-value-from-a-registry-key

自从你在编辑中回复后,我做了更多的研究并找到了一篇文章来解决你的问题。作者在其中指出:我发现注册表中已经包含一个值,该值表示从 1970 年 1 月 1 日起经过的秒数 @12:00 是安装操作系统的时间。这是他的解决方案:

Public Shared Function GetWindowsInstallationDateTime(computerName as String) as DateTime
    Dim key as Microsoft.Win32.RegistryKey
    key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, computerName)
    key = key.OpenSubKey("SOFTWAREMicrosoftWindows NTCurrentVersion", False)
    If key IsNot Nothing Then
        Dim startDate as DateTime
        Dim regVal as Int64

        startDate = new DateTime(1970, 1, 1, 0, 0, 0)
        regVal = Convert.ToInt64(key.GetValue("InstallDate").ToString())

        Return startDate.AddSeconds(regVal)
    End If

    Return DateTime.MinValue
End Function

来源:https://alanjuden.com/2009/09/09/get-windows-installation-date-in-csharp-or-vb-net/

希望这能帮助您解决问题。

您可以使用DateTimeOffset.FromUnixTimeSeconds

例如=>

Using rkLocalMachine As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    Using sk As RegistryKey = rkLocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion", False)
        Dim nValue As Integer = CType(sk.GetValue("InstallDate"), Integer)
        Dim dInstallDate As Date = DateTimeOffset.FromUnixTimeSeconds(nValue).DateTime
        MessageBox.Show("InstallDate = " + dInstallDate.ToString, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Using
End Using