首页 > 解决方案 > 需要在远程计算机上获取默认程序关联

问题描述

我正在使用下面的代码来获取有关远程计算机的一些信息。基本上,我需要获取操作系统版本(运行良好)和 .pdf 文件的默认程序和默认浏览器。我可以获取操作系统版本,但代码无法获取默认程序关联。远程注册表已启用并启动,但即使我收到“访问被拒绝”错误消息。有什么线索吗?希望你能帮助我。谢谢

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Const HKEY_LOCAL_MACHINE As String = "80000002"
        Const HKEY_current_user1 As String = "80000001"

        'Dim remoteRegistryController As New System.ServiceProcess.ServiceController("RemoteRegistry", computerNameTB.Text)
        'remoteRegistryController.Start()


        Dim osName, pdf, browser As String
        Dim options As New ConnectionOptions
        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True
        options.Username = "hd_juann"
        options.Password = "Diosteamo42="

        Dim myScope As New ManagementScope("\\" & computerNameTB.Text & "\root\default", options)
        Dim mypath As New ManagementPath("StdRegProv")
        Dim mc As New ManagementClass(myScope, mypath, Nothing)

        Try
            Dim inParams As ManagementBaseObject = mc.GetMethodParameters("GetDWORDValue")
            inParams("hDefKey") = UInt32.Parse(HKEY_LOCAL_MACHINE, System.Globalization.NumberStyles.HexNumber) 'RegistryHive.LocalMachine
            inParams("sSubKeyName") = "Software\Microsoft\Windows NT\currentVersion"
            inParams("sValueName") = "ProductName"

            'Dim osName As String

            Dim outParams As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParams, Nothing)

            If (outParams("ReturnValue").ToString() = "0") Then
                'MessageBox.Show(outParams("sValue").ToString())
                osName = outParams("sValue").ToString()
            Else
                MessageBox.Show("Error retrieving value : " + outParams("ReturnValue").ToString())
            End If
            'get pdf
            Dim inParamsPDF As ManagementBaseObject = mc.GetMethodParameters("GetDWORDValue")
            inParamsPDF("hDefKey") = UInt32.Parse(HKEY_current_user1, System.Globalization.NumberStyles.HexNumber) 'RegistryHive.LocalMachine
            inParamsPDF("sSubKeyName") = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.pdf\UserChoice"
            inParamsPDF("sValueName") = "ProgId"

            'Dim pdf As String

            Dim outParamsPDF As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParamsPDF, Nothing)

            If (outParamsPDF("ReturnValue").ToString() = "0") Then

                pdf = outParamsPDF("sValue").ToString()
            Else
                pdf = "No value Set"
            End If
            'MessageBox.Show(pdf)
            'remoteRegistryController.Stop()

            'get browser
            Dim inParamsBrowser As ManagementBaseObject = mc.GetMethodParameters("GetDWORDValue")
            inParamsBrowser("hDefKey") = UInt32.Parse(HKEY_current_user1, System.Globalization.NumberStyles.HexNumber) 'RegistryHive.LocalMachine
            inParamsBrowser("sSubKeyName") = "SOFTWARE\Microsoft\Windows\Shell\Associations\URLAssociations\https\UserChoice"
            inParamsBrowser("sValueName") = "ProgId"

            'Dim browser As String

            Dim outParamsBrowser As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParamsBrowser, Nothing)

            If (outParamsBrowser("ReturnValue").ToString() = "0") Then

                browser = outParamsBrowser("sValue").ToString()
            Else
                browser = "No value Set"
            End If
            MsgBox(osName & " is installed on computer " & computerNameTB.Text & vbCrLf & "PDF Default App: " & pdf & vbCrLf & "Default Browser: " & browser,, "ComputerInfo W10 Upgrade Project")

        Catch err As Exception
            If osName <> "" Then
                MsgBox(osName & " is installed on computer: " & computerNameTB.Text & ". No default program association information could be accessed   " & err.Message,, "ComputerInfo W10 Upgrade Project")
            Else
                MsgBox("It was no possible to query computer: " & computerNameTB.Text,, "ComputerInfo W10 Upgrade Project")
            End If
            'MsgBox("It was no possible to query computer: " & computerNameTB.Text,, "ComputerInfo W10 Upgrade Project")
        End Try

    End Sub

厄尔

标签: vb.net

解决方案


推荐阅读