5

現在、レジストリに関するいくつかの作業を行っています。

で列挙型を確認しRegistryRightsましたSystem.Security.AccessControl

public enum RegistryRights
{
    QueryValues = 1,
    SetValue = 2,
    CreateSubKey = 4,
    EnumerateSubKeys = 8,
    Notify = 16,
    CreateLink = 32,
    Delete = 65536,
    ReadPermissions = 131072,
    WriteKey = 131078,
    ExecuteKey = 131097,
    ReadKey = 131097,
    ChangePermissions = 262144,
    TakeOwnership = 524288,
    FullControl = 983103,
}

この列挙型はビット単位であり、列挙型には重複する値が含まれる可能性があることを知っています。私はこのコードで列挙型を反復しようとしていました:

 foreach (System.Security.AccessControl.RegistryRights regItem in Enum.GetValues(typeof(System.Security.AccessControl.RegistryRights)))
        {
            System.Diagnostics.Debug.WriteLine(regItem.ToString() + "  " + ((int)regItem).ToString());
        }

Enum.GetName(typeof(RegistryRights),regItem) も同じキー名を返します。

そして、私が得た出力は次のとおりです。


QueryValues  1
SetValue  2
CreateSubKey  4
EnumerateSubKeys  8
Notify  16
CreateLink  32
Delete  65536
ReadPermissions  131072
WriteKey  131078
ReadKey  131097
ReadKey  131097
ChangePermissions  262144
TakeOwnership  524288
FullControl  983103

重複したキーを取得する理由を教えてください(「ExecuteKey」ではなく「ReadKey」) int を値の 2 番目のキーに強制的にキャストするにはどうすればよいですか? ToString が実際のキー値を返さないのはなぜですか?

4

2 に答える 2

4

値ではなく、列挙型の名前を反復処理する必要があると思います。何かのようなもの:

foreach (string regItem in Enum.GetNames(typeof(RegistryRights)))
{
    var value = Enum.Parse(typeof(RegistryRights), regItem);

    System.Diagnostics.Debug.WriteLine(regItem + "  " + ((int)value).ToString());
}

なぜこれが起こるのかというと、値が重複している場合にどの名前を返すかをランタイムが知る方法がありません。これが、名前 (一意であることが保証されている) を反復処理すると、探している結果が得られる理由です。

于 2013-09-01T12:57:31.903 に答える