2012-01-12 7 views
1

私はコードCreateSubKeyを呼び出すときにいつでもUnauthorizedAccessExceptionを取得します。レジストリキーのUnauthorizedAccessException CreateSubKey

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社会に同様の問題>> HERE <<を持った記事を見つけましたが、そこに解決策は、(オーバーロードさOpenSubKeyメソッドを使用する)だけ私のためにNULL RegistryKeyを返しました。

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

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

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

ありがとうございます!

screen shot

+0

をあなたがしたい場合は、管理者としてアプリケーションを実行する必要がありますLocalMachineに値を設定します。 CurrentUserを使用するか、インストールの一環としてキーを作成し、すべての人がキーとサブキーにアクセスできるようにします。 – Casperah

+0

@Casperah - 今後質問をお読みください。あなたのコメントは、Windows Mobile 5のコンテキストでは意味がありません。 –

+0

'CurrentUser'は' LocalMachine'と同じ例外を生成します。 – jp2code

答えて

2

これらの3つはすべてWinMo 6エミュレータでうまく動作します。

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

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")) 
    { 
    } 
} 
+0

OK、それらの3つは私のために働いた。私のループにはバグがあるはずです。前のキーが閉じられる前にサブキーを開こうとしているのかもしれません。 ??? – jp2code

+0

これはおそらく、再帰と関係があると思います。私の賭けは何かが2度書き込むために開かれたもので、それを開こうとするのは2度目の失敗です。 – ctacke

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); 
    } 
} 

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

+0

デバイスのテストにロールアウトすることが許可される前に、このアプリケーションをエミュレータで実行する必要があります。 'Registry.LocalMachine'についての提案を使用して、元のコードを' Registry.CurrentUser'を指すように変更しましたが、同じエラーが発生します。 – jp2code

関連する問題