如果我不知道它的类型,如何从远程计算机获取注册表值? (C#)

前端之家收集整理的这篇文章主要介绍了如果我不知道它的类型,如何从远程计算机获取注册表值? (C#)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
根据存储的值类型,我必须使用以下方法之一:
GetBinaryValue
GetDWORDValue
GetExpandedStringValue
GetMultiStringValue
GetStringValue

如果我所知道的值是hive,key和value name,我不想尝试所有五个.有没有办法做到这一点?我正在使用WMI,但如果唯一的方法是使用advapi32.dll方法,我对此持开放态度.

您可以使用 StdRegProv.EnumValues方法枚举键的值,找出值的类型并确定要调用的Get方法.

这样的事情(抱歉,我不太了解C#所以这段代码可能远非完美):

using System;
    using System.Management;
    using System.Management.Instrumentation;

    namespace ConsoleApplication1
    {
        public enum RegHive : uint
        {
            HKEY_CLASSES_ROOT = 0x80000000,HKEY_CURRENT_USER = 0x80000001,HKEY_LOCAL_MACHINE = 0x80000002,HKEY_USERS = 0x80000003,HKEY_CURRENT_CONFIG = 0x80000005
        }

        public enum RegType
        {
            REG_SZ = 1,REG_EXPAND_SZ,REG_BINARY,REG_DWORD,REG_MULTI_SZ = 7
        }

        class Program
        {
            static void Main(string[] args)
            {
                const string strComputer = "computername";

                ConnectionOptions options = new ConnectionOptions();
                options.Impersonation = ImpersonationLevel.Impersonate;
                options.EnablePrivileges = true;
                options.Username = "username";
                options.Password = "password";

                ManagementScope myScope = new ManagementScope("\\\\" + strComputer + "\\root\\default",options);
                ManagementPath mypath = new ManagementPath("StdRegProv");
                ManagementClass mc = new ManagementClass(myScope,mypath,null);

                object oValue = GetValue(mc,RegHive.HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion","ProgramFilesDir");
                Console.WriteLine(oValue.ToString());
            }

            public static object GetValue(ManagementClass mc,RegHive hDefKey,string sSubKeyName,string sValueName)
            {
                RegType rType = GetValueType(mc,hDefKey,sSubKeyName,sValueName);

                ManagementBaSEObject inParams = mc.GetMethodParameters("GetStringValue");
                inParams["hDefKey"] = hDefKey;
                inParams["sSubKeyName"] = sSubKeyName;
                inParams["sValueName"] = sValueName;

                object oValue = null;

                switch (rType)
                {
                    case RegType.REG_SZ:
                        ManagementBaSEObject outParams = mc.InvokeMethod("GetStringValue",inParams,null);

                        if (Convert.ToUInt32(outParams["ReturnValue"]) == 0)
                        {
                            oValue = outParams["sValue"];
                        }
                        else
                        {
                            // GetStringValue call Failed
                        }
                        break;

                    case RegType.REG_EXPAND_SZ:
                        outParams = mc.InvokeMethod("GetExpandedStringValue",null);

                        if (Convert.ToUInt32(outParams["ReturnValue"]) == 0)
                        {
                            oValue = outParams["sValue"];
                        }
                        else
                        {
                            // GetExpandedStringValue call Failed
                        }
                        break;

                    case RegType.REG_MULTI_SZ:
                        outParams = mc.InvokeMethod("GetMultiStringValue",null);

                        if (Convert.ToUInt32(outParams["ReturnValue"]) == 0)
                        {
                            oValue = outParams["sValue"];
                        }
                        else
                        {
                            // GetMultiStringValue call Failed
                        }
                        break;

                    case RegType.REG_DWORD:
                        outParams = mc.InvokeMethod("GetDWORDValue",null);

                        if (Convert.ToUInt32(outParams["ReturnValue"]) == 0)
                        {
                            oValue = outParams["uValue"];
                        }
                        else
                        {
                            // GetDWORDValue call Failed
                        }
                        break;

                    case RegType.REG_BINARY:
                        outParams = mc.InvokeMethod("GetBinaryValue",null);

                        if (Convert.ToUInt32(outParams["ReturnValue"]) == 0)
                        {
                            oValue = outParams["uValue"] as byte[];
                        }
                        else
                        {
                            // GetBinaryValue call Failed
                        }
                        break;
                }

                return oValue;
            }

            public static RegType GetValueType(ManagementClass mc,string sValueName)
            {
                ManagementBaSEObject inParams = mc.GetMethodParameters("EnumValues");
                inParams["hDefKey"] = hDefKey;
                inParams["sSubKeyName"] = sSubKeyName;

                ManagementBaSEObject outParams = mc.InvokeMethod("EnumValues",null);

                if (Convert.ToUInt32(outParams["ReturnValue"]) == 0)
                {
                    string[] sNames = outParams["sNames"] as String[];
                    int[] iTypes = outParams["Types"] as int[];

                    for (int i = 0; i < sNames.Length; i++)
                    {
                        if (sNames[i] == sValueName)
                        {
                            return (RegType)iTypes[i];
                        }
                    }
                    // value not found
                }
                else
                {
                    // EnumValues call Failed
                }
            }
        }
    }
原文链接:https://www.f2er.com/windows/364759.html

猜你在找的Windows相关文章