私はコード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デバイスエミュレータ用です。
私が間違っていることは誰でも見ることができますか?
エラーは、コードが存在しないキーに初めて到達し、作成しようとするとスローされます。
ありがとうございます!
をあなたがしたい場合は、管理者としてアプリケーションを実行する必要がありますLocalMachineに値を設定します。 CurrentUserを使用するか、インストールの一環としてキーを作成し、すべての人がキーとサブキーにアクセスできるようにします。 – Casperah
@Casperah - 今後質問をお読みください。あなたのコメントは、Windows Mobile 5のコンテキストでは意味がありません。 –
'CurrentUser'は' LocalMachine'と同じ例外を生成します。 – jp2code