2

私は列挙型がどのように機能するかを理解しようとしています、レジストリのルートに列挙型を使用してレジストリに書き込む関数を作成しようとしていますが、ちょっと混乱しています

public enum RegistryLocation
        {
            ClassesRoot = Registry.ClassesRoot,
            CurrentUser = Registry.CurrentUser,
            LocalMachine = Registry.LocalMachine,
            Users = Registry.Users,
            CurrentConfig = Registry.CurrentConfig
        }

public void RegistryWrite(RegistryLocation location, string path, string keyname, string value)
{
     // Here I want to do something like this, so it uses the value from the enum
     RegistryKey key;
     key = location.CreateSubKey(path);
     // so that it basically sets Registry.CurrentConfig for example, or am i doing it wrong
     ..
}
4

1 に答える 1

4

問題は、クラスを使用して列挙値を初期化し、列挙値をクラスとして使用しようとしていることですが、これはできません。MSDNから:

列挙型に承認されているタイプは、byte、sbyte、short、ushort、int、uint、long、またはulongです。

できることは、列挙型を標準の列挙型として使用し、メソッドに列挙型に基づいて正しいRegistryKeyを返すようにすることです。

例えば:

    public enum RegistryLocation
    {
        ClassesRoot,
        CurrentUser,
        LocalMachine,
        Users,
        CurrentConfig
    }

    public RegistryKey GetRegistryLocation(RegistryLocation location)
    {
        switch (location)
        {
            case RegistryLocation.ClassesRoot:
                return Registry.ClassesRoot;

            case RegistryLocation.CurrentUser:
                return Registry.CurrentUser;

            case RegistryLocation.LocalMachine:
                return Registry.LocalMachine;

            case RegistryLocation.Users:
                return Registry.Users;

            case RegistryLocation.CurrentConfig:
                return Registry.CurrentConfig;

            default:
                return null;

        }
    }

    public void RegistryWrite(RegistryLocation location, string path, string keyname, string value) {
         RegistryKey key;
         key = GetRegistryLocation(location).CreateSubKey(path);
    }
于 2012-08-04T03:21:14.980 に答える