2012-08-04 2 views
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 
    .. 
} 

답변

5

문제는 클래스를 사용하여 열거 형 값을 초기화하고 할 수없는 클래스로 열거 형 값을 사용하려고한다는 것입니다. MSDN에서 :

열거의 승인 유형 바이트, sbyte, 짧은, USHORT, INT, UINT, 장기, 또는 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); 
    } 
+0

HMH는, 나는 그것을 얻을 생각하지만, 당신은 열거 형에 비 순차적 숫자 값을 할당하는 데 사용할 수 있습니다 – user1071461

+1

무엇인지 정확히 = 다음 열거에서 사용할 수 있습니다. 예를 들어, -1에서 열거 형 시작을 원한다면 거기에서 위로 진행하여 첫 번째 항목 = -1을 설정합니다. 예 : InvalidSelection = -1, NoSelection (값 0), ValidSelection (값 1). –

+0

아, 알았어, 고마워, 정보 – user1071461