从vb添加reg密钥

时间:2014-08-03 12:13:01

标签: vb.net cmd key registry

我必须从vb exe添加一个reg键。

我从cmd运行此字符串并正常工作。

REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "bit" /t REG_SZ /d C:\ProgramData\i2.exe

所以在vb文件中我这样做了:

Shell("REG ADD ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"" /v ""bit"" /t REG_SZ /d C:\ProgramData\i2.exe")

但是这会在错误的路径中添加一个reg键。

也许是一个sintax问题。哪里我失败了?在vb中有一个更正确的命令吗?

1 个答案:

答案 0 :(得分:0)

.NET Framework 4具有RegistryKey类,能够以独立于目标平台的方式处理32/64位注册表访问:

RegistryKey regkey =
    RegistryKey.OpenBaseKey(
        RegistryHive.LocalMachine,
        RegistryView.Registry64)
    .OpenSubKey("SOFTWARE")

可在此处找到更多信息http://sander.rijken.info/post/46536267427/reading-64-bit-registry-from-32bit-app

如果您想知道系统是32位还是64位:

Private is64BitProcess As Boolean = (IntPtr.Size = 8)
Private is64BitOperatingSystem As Boolean = is64BitProcess OrElse InternalCheckIsWow64()

<DllImport("Kernel32.dll", SetLastError:=True, CallingConvention:=CallingConvention.Winapi)> _
Public Shared Function IsWow64Process( _
    ByVal hProcess As System.IntPtr, _
    ByRef wow64Process As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean

End Function

Public Shared Function InternalCheckIsWow64() As Boolean
    If (Environment.OSVersion.Version.Major = 5 AndAlso Environment.OSVersion.Version.Minor >= 1) OrElse Environment.OSVersion.Version.Major >= 6 Then
        Using p As Process = Process.GetCurrentProcess()
            Dim retVal As Boolean
            If Not IsWow64Process(p.Handle, retVal) Then
                Return False
            End If
            Return retVal
        End Using
    Else
        Return False
    End If
End Function