1.下の画像にある[0]~[3]のレジストリの値を読み込み
2.プログラムを5分間待機させている間に
[0]~[3]のデータを手動で消し
3.1で読み込んだ値を利用して
再度[0]~[3]のレジストリを復活させる
というプログラムを作ろうと思っています。
レジストリを編集するコードはこのサイトを参考にしました。
レジストリを手動で編集した場合なにも異変は起きませんでした。
コードの詳細です
まず、listcountの値と[0]~[3]の値を読み込みます
C#
1 //タイトル 2 Console.Title = "aを変更する"; 3 4 5 6 7 //確認メッセージ 8 9 DialogResult result = MessageBox.Show("aを変更します", 10 "aを変更する", MessageBoxButtons.OKCancel); 11 12 13 14 15 // OKボタンがクリックされた時の処理 16 if (result == DialogResult.OK) 17 { 18 //listcountを読み込む 19 Microsoft.Win32.RegistryKey regkey0 = 20 Microsoft.Win32.Registry.CurrentUser.OpenSubKey 21 (@"HKEY_CURRENT_USER\Software\a", false); 22 //キーが存在しないときは null が返される 23 if (regkey0 == null) return; 24 string list = (string)regkey0.GetValue("listcount"); 25 regkey0.Close(); 26 27 28 29 30 31 //[0]~[3]を読みこむ 32 Microsoft.Win32.RegistryKey regkey = 33 Microsoft.Win32.Registry.CurrentUser.OpenSubKey 34 (@"Software\a", false); 35 //キーが存在しないときは null が返される 36 37 string[] array = { }; 38 int countlist = int.Parse(list); 39 for (int i = 0; i <= countlist - 1; i++) 40 { 41 if (regkey == null) return; 42 string[] aaray = array.Concat 43 (new string[] { (string)regkey.GetValue("[" + i + "]") }). 44 ToArray(); 45 } 46 //閉じる 47 regkey.Close();
つぎにプログラムを待機させます
C#
1//5分待機する 2 for (int i = 300; 0 <= i; i--) 3 { 4 Console.CursorLeft = 0; 5 Console.Write("しばらくお待ちください:{0:D3}", i); 6 Thread.Sleep(1000); 7 } 8
読み込んだ値を代入して元通りにしようと考えていました。
C#
1 2 //読み込んだ[0]~[3]の項目を再度追加する 3 Microsoft.Win32.RegistryKey regkeyd = 4 Microsoft.Win32.Registry.CurrentUser.OpenSubKey 5 (@"Software\a", false); 6 if (regkeyd == null) return; 7 Microsoft.Win32.RegistryKey regkeya = 8 Microsoft.Win32.Registry.CurrentUser.CreateSubKey 9 (@"Software\a"); 10 11 12 for (int j = 0; j <= aarray.Length; j++) 13 { 14 regkeya.SetValue("[" + j + "]", aarray[j]); 15 } 16 regkeya.Close(); 17 18 19 20 21 22 //処理完了のメッセージを表示する 23 24 DialogResult resulta = MessageBox.Show("処理が完了しました");
このコードまでたどり着いて下の画像のようになることが理想だったのですが、
最後の3つ目のコードにおいてエラーが起きました。
C#
1 for (int j = 0; j <= aarray.Length; j++) 2 { 3 regkeya.SetValue("[" + j + "]", aarray[j]); 4 }
この部分に対して
現在のコンテキストにaarrayという名前は存在しません
というエラー画面が2連続出てきました。
aarrayは普通に存在しているのにもかかわらずこのようなエラーが出てくるのが全く理解できません。
回答お願いします。
回答2件
あなたの回答
tips
プレビュー