1

コードでCreateSubKeyを呼び出そうとすると、常にUnauthorizedAccessExceptionが発生します。

const string regKeyPath = @"Software\Apps\jp2code.net\FTMaint";

private void BuildRegistry() {
  string[] split = regKeyPath.Split('\\');
  keyMaker(Registry.LocalMachine, split, 0);
}

private static void keyMaker(RegistryKey key, string[] path, int index) {
  string keyValue = path[index++];
  RegistryKey key2;
  if (!String.IsNullOrEmpty(keyValue)) {
    string subKey = null;
    string[] subKeyNames = key.GetSubKeyNames();
    foreach (var item in subKeyNames) {
      if (keyValue == item) {
        subKey = item;
      }
    }
    if (String.IsNullOrEmpty(subKey)) {
      key2 = key.CreateSubKey(keyValue);
    } else {
      key2 = key.OpenSubKey(subKey);
    }
    //key2 = key.OpenSubKey(keyValue, String.IsNullOrEmpty(subKey));
  } else {
    key2 = key;
  }
  if (index < path.Length) {
    try {
      keyMaker(key2, path, index + 1);
    } finally {
      key2.Close();
    }
  }
}

MSDN Social で>> HERE <<で誰かが同様の問題を抱えている投稿を見つけましたが、そこでの解決策 (オーバーロードされた OpenSubKey メソッドを使用する) では、NULL RegistryKeyしか返されませんでした。

これは、Windows Mobile 5 デバイス エミュレータ用です。

誰かが私が間違っていることを見ることができますか?

コードが存在しないキーに初めて到達し、それを作成しようとすると、エラーがスローされます。

ありがとう!

スクリーンショット

4

3 に答える 3

2

WinMo 6エミュレーターでは、これら3つすべてが正常に機能します。

ルートキーを作成します。

using (var swKey = Registry.LocalMachine.CreateSubKey("foo"))
{
    using (var subkey = swKey.CreateSubKey("OpenNETCF"))
    {
    }
}

パスを介してサブキーを作成する

using (var swKey = Registry.LocalMachine.CreateSubKey("software\\foo"))
{
    using (var subkey = swKey.CreateSubKey("OpenNETCF"))
    {
    }
}

サブキーを直接作成します。

using (var swKey = Registry.LocalMachine.OpenSubKey("software", true))
{
    using (var subkey = swKey.CreateSubKey("OpenNETCF"))
    {
    }
}
于 2012-01-12T19:20:36.090 に答える
0

インストール中に LocalMachine にキーを作成するには、次のようにします。

[RunInstaller(true)]
public class InstallRegistry : Installer
{
    public override void Install(System.Collections.IDictionary stateSaver)
    {
        base.Install(stateSaver);

        using (RegistryKey key = Registry.LocalMachine.CreateSubKey(@"software\..."))
        {
            RegistrySecurity rs = new RegistrySecurity();
            rs.AddAccessRule(new RegistryAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), RegistryRights.FullControl, InheritanceFlags.None, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
            key.SetAccessControl(rs);
        }
    }
    public override void Rollback(System.Collections.IDictionary savedState)
    {
        base.Rollback(savedState);
    }
}

これがあなたを助けることを願っています。

于 2012-01-12T18:16:04.523 に答える