0

我使用我不太了解的管理类,只是为了读取远程注册表:

        string regKeyToGet = @"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources";
        string keyToRead = "Description";
        ConnectionOptions oConn = new ConnectionOptions();
        oConn.Username = "user1";
        oConn.Password = "user1password";

        ManagementScope scope = new ManagementScope(@"//" + "PC1" + @"/root/default", oConn);
        ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);

        ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue");
        inParams["sSubKeyName"] = regKeyToGet;
        inParams["sValueName"] = keyToRead;
        ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue", inParams, null);
        MessageBox.Show(outParams["sValue"].ToString());

此代码为我返回指定键的特定值,但我需要返回一个名称值数组,如下所示:

RegistryKey regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, PC1, RegistryView.Registry64).OpenSubKey("SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources");
regKey.GetValueNames();
4

1 回答 1

0

经过一番研究,我找到了方法,这里是代码:

        ConnectionOptions oConn = new ConnectionOptions();
        oConn.Username = "user1";
        oConn.Password = "user1password";
        ManagementScope scope = new ManagementScope(@"//" + "PC1" + @"/root/default", oConn);
        scope.Connect();

        ManagementClass mc = new ManagementClass("stdRegProv");
        mc.Scope = scope;

        ManagementBaseObject mbo;
        mbo = mc.GetMethodParameters("EnumValues");

        mbo.SetPropertyValue("sSubKeyName", @"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources");

        string[] subkeys = (string[])mc.InvokeMethod("EnumValues", mbo, null).Properties["sNames"].Value;

        foreach (string strKey in subkeys)
        {
            MessageBox.Show(strKey);
        } 

我在页面上找到了帮助:如何读取远程注册表项?

于 2012-06-20T13:41:07.670 回答