2 // RegistryKeyTest.cs - NUnit Test Cases for Microsoft.Win32.RegistryKey
5 // mei (mei@work.email.ne.jp)
6 // Robert Jordan (robertj@gmx.net)
7 // Gert Driesen (drieseng@users.sourceforge.net)
10 // (C) 2004, 2005 Novell (http://www.novell.com)
15 using System.Runtime.InteropServices;
17 using Microsoft.Win32;
18 using Microsoft.Win32.SafeHandles;
20 using NUnit.Framework;
22 namespace MonoTests.Microsoft.Win32
25 public class RegistryKeyTest
27 private const string mimeroot = @"MIME\Database\Content Type";
30 [Category ("NotWorking")] // this will not work on Linux ever
31 public void TestGetValue ()
33 RegistryKey root = Registry.ClassesRoot;
36 key = root.OpenSubKey (mimeroot + @"\audio/wav");
37 Assert.AreEqual (".wav", key.GetValue ("Extension"), "GetValue #1");
38 key = root.OpenSubKey (mimeroot + @"\text/x-scriptlet");
39 Assert.AreEqual (null, key.GetValue ("Extension"), "GetValue #2");
43 public void TestHandle ()
45 // this test is for Windows only
49 // this regpath always exists under windows
50 RegistryKey k = Registry.CurrentUser
51 .OpenSubKey ("Software", false)
52 .OpenSubKey ("Microsoft", false)
53 .OpenSubKey ("Windows", false);
55 Assert.IsNotNull (k, "#01");
59 public void OpenSubKey ()
61 RegistryKey key = Registry.LocalMachine;
63 // HKEY_LOCAL_MACHINE\software should always exist on Windows
64 // and is automatically created on Linux
65 Assert.IsNotNull (key.OpenSubKey ("Software"), "#A1");
66 Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#A2");
68 key = Registry.CurrentUser;
70 // HKEY_CURRENT_USER\software should always exist on Windows
71 // and is automatically created on Linux
72 Assert.IsNotNull (key.OpenSubKey ("Software"), "#B1");
73 Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#B2");
77 // HKEY_USERS\software should not exist on Windows, and should not
78 // be created automatically on Linux
79 Assert.IsNull (key.OpenSubKey ("Software"), "#C1");
80 Assert.IsNull (key.OpenSubKey ("soFtware"), "#C2");
84 public void OpenSubKey_Key_DoesNotExist ()
86 string subKeyName = Guid.NewGuid ().ToString ();
87 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#1"); // read-only
88 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#2"); // writable
92 public void OpenSubKey_Key_Removed ()
94 string subKeyName = Guid.NewGuid ().ToString ();
97 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
98 // check if key was successfully created
99 Assert.IsNotNull (createdKey, "#1");
100 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
103 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
104 Assert.IsNotNull (createdKey, "#2");
105 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
106 Assert.IsNotNull (createdKey, "#3");
108 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
111 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
112 Assert.IsNull (createdKey.OpenSubKey ("monotemp"), "#5"); // read-only
114 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#6");
115 Assert.IsNull (createdKey.OpenSubKey ("monotemp", true), "#7");
119 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
120 if (createdKey != null) {
122 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
130 [Category ("NotWorking")] // MS should not allow this
131 public void OpenSubKey_Name_Empty ()
134 using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty)) {
135 Assert.IsNotNull (emptyKey, "#1");
138 using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty, true)) {
139 Assert.IsNotNull (emptyKey, "#1");
144 public void OpenSubKey_Name_MaxLength ()
146 string name = new string ('a', 254);
148 Assert.IsNull (Registry.CurrentUser.OpenSubKey (name), "#A1");
150 name = new string ('a', 255);
152 Assert.IsNull (Registry.CurrentUser.OpenSubKey (name), "#B1");
154 name = new string ('a', 256);
157 Registry.CurrentUser.OpenSubKey (name);
159 } catch (ArgumentException ex) {
160 // 1.x: Registry subkeys should not be
161 // greater than or equal to 255 characters
163 // 2.x: Registry subkeys should not be
164 // greater than 255 characters
165 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
166 Assert.IsNull (ex.InnerException, "#C3");
167 Assert.IsNotNull (ex.Message, "#c4");
168 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
169 Assert.IsNull (ex.ParamName, "#C6");
174 public void OpenSubKey_Name_Null ()
177 Registry.CurrentUser.OpenSubKey (null);
179 } catch (ArgumentNullException ex) {
180 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
181 Assert.IsNull (ex.InnerException, "#A3");
182 Assert.IsNotNull (ex.Message, "#A4");
183 Assert.AreEqual ("name", ex.ParamName, "#A5");
187 Registry.CurrentUser.OpenSubKey (null, true);
189 } catch (ArgumentNullException ex) {
190 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
191 Assert.IsNull (ex.InnerException, "#B3");
192 Assert.IsNotNull (ex.Message, "#B4");
193 Assert.AreEqual ("name", ex.ParamName, "#B5");
198 public void Close_Local_Hive ()
200 RegistryKey hive = Registry.CurrentUser;
203 Assert.IsNotNull (hive.GetSubKeyNames (), "#1");
204 Assert.IsNull (hive.GetValue ("doesnotexist"), "#2");
205 Assert.IsNotNull (hive.GetValueNames (), "#3");
206 Assert.IsNull (hive.OpenSubKey ("doesnotexist"), "#4");
207 Assert.IsNotNull (hive.SubKeyCount, "#5");
208 Assert.IsNotNull (hive.ToString (), "#6");
210 // closing key again does not have any effect
215 public void Close_Local_Key ()
217 RegistryKey key = Registry.CurrentUser.OpenSubKey ("SOFTWARE");
220 // closing a key twice does not have any effect
224 key.CreateSubKey ("a");
226 } catch (ObjectDisposedException) {
230 key.DeleteSubKey ("doesnotexist");
232 } catch (ObjectDisposedException) {
236 key.DeleteSubKeyTree ("doesnotexist");
238 } catch (ObjectDisposedException) {
242 key.DeleteValue ("doesnotexist");
244 } catch (ObjectDisposedException) {
247 // flushing a closed key does not have any effect
251 key.GetSubKeyNames ();
253 } catch (ObjectDisposedException) {
257 key.GetValue ("doesnotexist");
259 } catch (ObjectDisposedException) {
263 key.GetValueNames ();
265 } catch (ObjectDisposedException) {
269 key.OpenSubKey ("doesnotexist");
271 } catch (ObjectDisposedException) {
275 key.SetValue ("doesnotexist", "something");
277 } catch (ObjectDisposedException) {
281 int x = key.SubKeyCount;
282 Assert.Fail ("#10:" + x);
283 } catch (ObjectDisposedException) {
289 } catch (ObjectDisposedException) {
293 int x = key.ValueCount;
294 Assert.Fail ("#12:" + x);
295 } catch (ObjectDisposedException) {
300 public void Close_Remote_Hive ()
302 // access to registry of remote machines is not implemented on unix
306 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
307 RegistryHive.CurrentUser, Environment.MachineName);
310 // closing a remote hive twice does not have any effect
314 hive.CreateSubKey ("a");
316 } catch (ObjectDisposedException) {
320 hive.DeleteSubKey ("doesnotexist");
322 } catch (ObjectDisposedException) {
326 hive.DeleteSubKeyTree ("doesnotexist");
328 } catch (ObjectDisposedException) {
332 hive.DeleteValue ("doesnotexist");
334 } catch (ObjectDisposedException) {
337 // flushing a closed hive does not have any effect
341 hive.GetSubKeyNames ();
343 } catch (ObjectDisposedException) {
347 hive.GetValue ("doesnotexist");
349 } catch (ObjectDisposedException) {
353 hive.GetValueNames ();
355 } catch (ObjectDisposedException) {
359 hive.OpenSubKey ("doesnotexist");
361 } catch (ObjectDisposedException) {
365 hive.SetValue ("doesnotexist", "something");
367 } catch (ObjectDisposedException) {
371 int x = hive.SubKeyCount;
372 Assert.Fail ("#10:" + x);
373 } catch (ObjectDisposedException) {
379 } catch (ObjectDisposedException) {
383 int x = hive.ValueCount;
384 Assert.Fail ("#12:" + x);
385 } catch (ObjectDisposedException) {
390 public void Close_Remote_Key ()
392 // access to registry of remote machines is not implemented on unix
396 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
397 RegistryHive.CurrentUser, Environment.MachineName);
398 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
401 // closing a remote key twice does not have any effect
405 key.CreateSubKey ("a");
407 } catch (ObjectDisposedException) {
411 key.DeleteSubKey ("doesnotexist");
413 } catch (ObjectDisposedException) {
417 key.DeleteSubKeyTree ("doesnotexist");
419 } catch (ObjectDisposedException) {
423 key.DeleteValue ("doesnotexist");
425 } catch (ObjectDisposedException) {
428 // flushing a closed key does not have any effect
432 key.GetSubKeyNames ();
434 } catch (ObjectDisposedException) {
438 key.GetValue ("doesnotexist");
440 } catch (ObjectDisposedException) {
444 key.GetValueNames ();
446 } catch (ObjectDisposedException) {
450 key.OpenSubKey ("doesnotexist");
452 } catch (ObjectDisposedException) {
456 key.SetValue ("doesnotexist", "something");
458 } catch (ObjectDisposedException) {
462 int x = key.SubKeyCount;
463 Assert.Fail ("#10:" + x);
464 } catch (ObjectDisposedException) {
470 } catch (ObjectDisposedException) {
474 int x = key.ValueCount;
475 Assert.Fail ("#12:" + x);
476 } catch (ObjectDisposedException) {
483 public void CreateSubKey ()
485 string subKeyName = Guid.NewGuid ().ToString ();
488 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
489 // check if key was successfully created
490 Assert.IsNotNull (createdKey, "#A1");
491 // software subkey should not be created automatically
492 Assert.IsNull (createdKey.OpenSubKey ("software"), "#A2");
495 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
496 // check if key was successfully created
497 Assert.IsNotNull (createdKey, "#B1");
498 // software subkey should not be created automatically
499 Assert.IsNull (createdKey.OpenSubKey ("software"), "#B2");
503 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
506 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
508 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
509 // check if key was successfully created
510 Assert.IsNotNull (createdKey, "#C1");
511 // software subkey should not be created automatically
512 Assert.IsNull (softwareKey.OpenSubKey ("software"), "#C2");
515 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
516 // check if key was successfully created
517 Assert.IsNotNull (createdKey, "#D1");
518 // software subkey should not be created automatically
519 Assert.IsNull (softwareKey.OpenSubKey ("software"), "#D2");
523 softwareKey.DeleteSubKeyTree (subKeyName);
529 public void CreateSubKey_Key_ReadOnly ()
531 string subKeyName = Guid.NewGuid ().ToString ();
533 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
534 RegistryKey createdKey = null;
537 createdKey = softwareKey.CreateSubKey (subKeyName);
539 } catch (UnauthorizedAccessException ex) {
540 // Cannot write to the registry key
541 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
542 Assert.IsNotNull (ex.Message, "#3");
543 Assert.IsNull (ex.InnerException, "#4");
546 if (createdKey != null)
553 public void CreateSubKey_Key_Removed ()
555 string subKeyName = Guid.NewGuid ().ToString ();
557 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
559 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
560 softwareKey.DeleteSubKeyTree (subKeyName);
561 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
563 createdKey.CreateSubKey ("test");
565 } catch (IOException ex) {
566 // Illegal operation attempted on a registry key that
567 // has been marked for deletion
568 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
569 Assert.IsNotNull (ex.Message, "#4");
570 Assert.IsNull (ex.InnerException, "#5");
575 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
576 if (createdKey != null) {
578 softwareKey.DeleteSubKeyTree (subKeyName);
587 [Category ("NotWorking")] // MS should not allow this
588 public void CreateSubKey_Name_Empty ()
590 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
591 using (RegistryKey emptyKey = softwareKey.CreateSubKey (string.Empty)) {
592 Assert.IsNotNull (emptyKey, "#1");
593 emptyKey.SetValue ("name1", "value1");
599 public void CreateSubKey_Name_MaxLength ()
601 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
602 string subKeyName = new string ('a', 254);
605 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
606 Assert.IsNotNull (createdKey, "#A1");
607 Assert.IsNotNull (softwareKey.OpenSubKey (subKeyName), "#A2");
610 softwareKey.DeleteSubKeyTree (subKeyName);
613 subKeyName = new string ('a', 255);
616 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
617 Assert.IsNotNull (createdKey, "#B1");
618 Assert.IsNotNull (softwareKey.OpenSubKey (subKeyName), "#B2");
621 softwareKey.DeleteSubKey (subKeyName);
624 subKeyName = new string ('a', 256);
627 softwareKey.CreateSubKey (subKeyName);
629 } catch (ArgumentException ex) {
630 // 1.x: Registry subkeys should not be
631 // greater than or equal to 255 characters
633 // 2.x: Registry subkeys should not be
634 // greater than 255 characters
635 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
636 Assert.IsNull (ex.InnerException, "#C3");
637 Assert.IsNotNull (ex.Message, "#C4");
638 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
639 Assert.IsNull (ex.ParamName, "#C6");
645 public void CreateSubKey_Name_Null ()
647 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
649 softwareKey.CreateSubKey (null);
651 } catch (ArgumentNullException ex) {
652 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
653 Assert.IsNull (ex.InnerException, "#3");
654 Assert.IsNotNull (ex.Message, "#4");
655 Assert.AreEqual ("name", ex.ParamName, "#5");
661 // Unfortunately we can't test that the scenario where a volatile
662 // key is not alive after a reboot, but we can test other bits.
664 public void CreateSubKey_Volatile ()
666 RegistryKey key = null;
667 RegistryKey subkey = null;
668 string subKeyName = "VolatileKey";
671 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
672 subkey = key.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
675 key = Registry.CurrentUser.OpenSubKey (subKeyName);
676 subkey = key.OpenSubKey ("Child");
677 Assert.AreEqual (true, subkey != null, "#A1");
687 public void CreateSubKey_Volatile_Child ()
689 RegistryKey key = null;
690 RegistryKey subkey = null;
691 string subKeyName = "VolatileKey";
694 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
695 subkey = key.CreateSubKey ("Child"); // Non volatile child
696 Assert.Fail ("#Exc");
697 } catch (IOException) {
707 public void CreateSubKey_Volatile_Conflict ()
709 RegistryKey key = null;
710 RegistryKey key2 = null;
711 RegistryKey subkey = null;
712 string subKeyName = "VolatileKey";
716 // Create a volatile key and try to open it as a normal one
718 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
719 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
720 Assert.AreEqual (key.Name, key2.Name, "A0");
722 subkey = key2.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
723 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#A1");
724 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#A2");
731 // Create a non-volatile key and try to open it as a volatile one
733 subKeyName = "NonVolatileKey";
734 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
735 key2.SetValue ("Name", "Mono");
736 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
737 Assert.AreEqual (key.Name, key2.Name, "B0");
738 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#B1");
739 Assert.AreEqual ("Mono", key2.GetValue ("Name"), "#B2");
741 key.CreateSubKey ("Child");
742 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#B3");
743 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#B4");
746 // Close the non-volatile key and try to re-open it as a volatile one
750 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
751 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#C0");
752 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#C1");
764 public void DeleteSubKey_Volatile ()
766 RegistryKey key = null;
767 RegistryKey subkey = null;
768 string subKeyName = "VolatileKey";
771 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
772 key.CreateSubKey ("VolatileKeyChild", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
773 key.SetValue ("Name", "Mono");
776 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
778 key = Registry.CurrentUser.OpenSubKey (subKeyName);
779 Assert.AreEqual (null, key, "#A0");
788 // Define a normal key, and create a normal and a volatile key under it, and retrieve their names.
790 public void GetSubKeyNames_Volatile ()
792 RegistryKey key = null;
793 RegistryKey subkey = null;
794 string subKeyName = Guid.NewGuid ().ToString ();
795 string volChildKeyName = "volatilechildkey";
796 string childKeyName = "childkey";
799 key = Registry.CurrentUser.CreateSubKey (subKeyName);
800 key.CreateSubKey (volChildKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
801 key.CreateSubKey (childKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
804 key = Registry.CurrentUser.OpenSubKey (subKeyName);
805 string [] keyNames = key.GetSubKeyNames ();
807 // we can guarantee the order of the child keys, so we sort the two of them
808 Array.Sort (keyNames);
810 Assert.AreEqual (2, keyNames.Length, "#A0");
811 Assert.AreEqual (childKeyName, keyNames [0], "#A1");
812 Assert.AreEqual (volChildKeyName, keyNames [1], "#A2");
824 public void DeleteSubKey ()
826 string subKeyName = Guid.NewGuid ().ToString ();
829 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
830 // check if key was successfully created
831 Assert.IsNotNull (createdKey, "#1");
833 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
834 Assert.IsNotNull (createdKey, "#2");
835 Registry.CurrentUser.DeleteSubKey (subKeyName);
837 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
838 Assert.IsNull (createdKey, "#3");
842 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
843 if (createdKey != null) {
845 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
853 public void DeleteSubKey_Key_HasChildKeys ()
855 string subKeyName = Guid.NewGuid ().ToString ();
858 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
859 // check if key was successfully created
860 Assert.IsNotNull (createdKey, "#1");
861 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
865 Registry.CurrentUser.DeleteSubKey (subKeyName);
867 } catch (InvalidOperationException ex) {
868 // Registry key has subkeys and recursive removes are not
869 // supported by this method
870 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
871 Assert.IsNotNull (ex.Message, "#4");
872 Assert.IsNull (ex.InnerException, "#5");
876 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
877 if (createdKey != null) {
879 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
887 public void DeleteSubKey_Key_ReadOnly ()
889 string subKeyName = Guid.NewGuid ().ToString ();
892 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
893 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
897 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
899 softwareKey.DeleteSubKey (subKeyName);
901 } catch (UnauthorizedAccessException ex) {
902 // Cannot write to the registry key
903 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
904 Assert.IsNotNull (ex.Message, "#3");
905 Assert.IsNull (ex.InnerException, "#4");
910 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
911 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
912 if (createdKey != null) {
914 softwareKey.DeleteSubKeyTree (subKeyName);
923 public void DeleteSubKey_Key_DoesNotExist ()
925 string subKeyName = Guid.NewGuid ().ToString ();
928 Registry.CurrentUser.DeleteSubKey (subKeyName);
930 } catch (ArgumentException ex) {
931 // Cannot delete a subkey tree because the subkey does not exist
932 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
933 Assert.IsNull (ex.InnerException, "#A3");
934 Assert.IsNotNull (ex.Message, "#A4");
935 Assert.IsNull (ex.ParamName, "#A5");
939 Registry.CurrentUser.DeleteSubKey (subKeyName, true);
941 } catch (ArgumentException ex) {
942 // Cannot delete a subkey tree because the subkey does not exist
943 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
944 Assert.IsNull (ex.InnerException, "#B3");
945 Assert.IsNotNull (ex.Message, "#B4");
946 Assert.IsNull (ex.ParamName, "#B5");
949 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
953 public void DeleteSubKey_Key_Removed ()
955 string subKeyName = Guid.NewGuid ().ToString ();
958 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
959 // check if key was successfully created
960 Assert.IsNotNull (createdKey, "#1");
961 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
964 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
965 Assert.IsNotNull (createdKey, "#2");
966 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
967 Assert.IsNotNull (createdKey, "#3");
969 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
970 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
972 createdKey.DeleteSubKey ("monotemp");
974 } catch (ArgumentException ex) {
975 // Cannot delete a subkey tree because the subkey does
977 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
978 Assert.IsNull (ex.InnerException, "#7");
979 Assert.IsNotNull (ex.Message, "#8");
980 Assert.IsNull (ex.ParamName, "#9");
985 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
986 if (createdKey != null) {
988 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
996 [Category ("NotWorking")] // MS should not allow this
997 public void DeleteSubKey_Name_Empty ()
999 string subKeyName = Guid.NewGuid ().ToString ();
1001 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1003 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1004 createdKey.DeleteSubKey (string.Empty);
1005 createdKey.Close ();
1007 createdKey = softwareKey.OpenSubKey (subKeyName);
1008 Assert.IsNull (createdKey, "#1");
1011 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1012 if (createdKey != null)
1013 createdKey.Close ();
1014 softwareKey.DeleteSubKeyTree (subKeyName);
1022 public void DeleteSubKey_Name_MaxLength ()
1024 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1025 string subKeyName = new string ('a', 254);
1027 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1028 createdKey.Close ();
1030 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1031 Assert.IsNotNull (createdKey, "#A1");
1033 softwareKey.DeleteSubKey (subKeyName);
1034 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1035 Assert.IsNull (createdKey, "#A2");
1038 subKeyName = new string ('a', 256);
1041 softwareKey.DeleteSubKey (subKeyName);
1042 Assert.Fail ("#B1");
1043 } catch (ArgumentException ex) {
1044 // 1.x: Registry subkeys should not be
1045 // greater than or equal to 255 characters
1047 // 2.x: Registry subkeys should not be
1048 // greater than 255 characters
1049 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1050 Assert.IsNull (ex.InnerException, "#B3");
1051 Assert.IsNotNull (ex.Message, "#B4");
1052 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1053 Assert.IsNull (ex.ParamName, "#B6");
1059 public void DeleteSubKey_Name_Null ()
1061 string subKeyName = Guid.NewGuid ().ToString ();
1063 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1065 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1067 createdKey.DeleteSubKey (null);
1069 } catch (ArgumentNullException ex) {
1070 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1071 Assert.IsNull (ex.InnerException, "#3");
1072 Assert.IsNotNull (ex.Message, "#4");
1073 Assert.AreEqual ("name", ex.ParamName, "#5");
1077 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1078 if (createdKey != null)
1079 createdKey.Close ();
1080 softwareKey.DeleteSubKeyTree (subKeyName);
1088 public void DeleteSubKeyTree ()
1091 // - remove key with subkeys
1092 // - remove key of which some subkeys are marked for deletion
1093 // - remove key with values
1097 public void DeleteSubKeyTree_Key_DoesNotExist ()
1099 // Cannot delete a subkey tree because the subkey does not exist
1100 string subKeyName = Guid.NewGuid ().ToString ();
1102 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1104 } catch (ArgumentException ex) {
1105 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1106 Assert.IsNull (ex.InnerException, "#3");
1107 Assert.IsNotNull (ex.Message, "#4");
1108 Assert.IsNull (ex.ParamName, "#5");
1114 public void DeleteSubKeyTree_Key_DoesNotExist_Overload ()
1116 // Cannot delete a subkey tree because the subkey does not exist
1117 string subKeyName = Guid.NewGuid ().ToString ();
1119 Registry.CurrentUser.DeleteSubKeyTree (subKeyName, true);
1121 } catch (ArgumentException ex) {
1122 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1123 Assert.IsNull (ex.InnerException, "#3");
1124 Assert.IsNotNull (ex.Message, "#4");
1125 Assert.IsNull (ex.ParamName, "#5");
1128 // It's enough to know this line is not throwing an exception.
1129 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
1134 public void DeleteSubKeyTree_Key_ReadOnly ()
1136 string subKeyName = Guid.NewGuid ().ToString ();
1139 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1140 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1141 createdKey.Close ();
1144 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
1146 softwareKey.DeleteSubKeyTree (subKeyName);
1148 } catch (UnauthorizedAccessException ex) {
1149 // Cannot write to the registry key
1150 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1151 Assert.IsNotNull (ex.Message, "#3");
1152 Assert.IsNull (ex.InnerException, "#4");
1157 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1158 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1159 if (createdKey != null)
1160 createdKey.Close ();
1161 softwareKey.DeleteSubKeyTree (subKeyName);
1169 public void DeleteSubKeyTree_Key_Removed ()
1171 string subKeyName = Guid.NewGuid ().ToString ();
1174 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1175 // check if key was successfully created
1176 Assert.IsNotNull (createdKey, "#1");
1177 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
1180 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1181 Assert.IsNotNull (createdKey, "#2");
1182 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
1183 Assert.IsNotNull (createdKey, "#3");
1185 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1186 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
1188 createdKey.DeleteSubKeyTree ("monotemp");
1190 } catch (ArgumentException ex) {
1191 // Cannot delete a subkey tree because the subkey does
1193 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
1194 Assert.IsNull (ex.InnerException, "#7");
1195 Assert.IsNotNull (ex.Message, "#8");
1196 Assert.IsNull (ex.ParamName, "#9");
1201 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1202 if (createdKey != null) {
1203 createdKey.Close ();
1204 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1212 [Category ("NotWorking")] // MS should not allow this
1213 public void DeleteSubKeyTree_Name_Empty ()
1215 string subKeyName = Guid.NewGuid ().ToString ();
1217 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1219 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1220 createdKey.DeleteSubKeyTree (string.Empty);
1221 createdKey.Close ();
1223 createdKey = softwareKey.OpenSubKey (subKeyName);
1224 Assert.IsNull (createdKey, "#1");
1227 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1228 if (createdKey != null)
1229 createdKey.Close ();
1230 softwareKey.DeleteSubKeyTree (subKeyName);
1238 public void DeleteSubKeyTree_Name_MaxLength ()
1240 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1241 string subKeyName = new string ('a', 254);
1243 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1244 createdKey.Close ();
1246 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1247 Assert.IsNotNull (createdKey, "#A1");
1249 softwareKey.DeleteSubKeyTree (subKeyName);
1250 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1251 Assert.IsNull (createdKey, "#A2");
1255 subKeyName = new string ('a', 255);
1257 subKeyName = new string ('a', 256);
1261 softwareKey.DeleteSubKeyTree (subKeyName);
1262 Assert.Fail ("#B1");
1263 } catch (ArgumentException ex) {
1264 // 1.x: Registry subkeys should not be
1265 // greater than or equal to 255 characters
1267 // 2.x: Registry subkeys should not be
1268 // greater than 255 characters
1269 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1270 Assert.IsNull (ex.InnerException, "#B3");
1271 Assert.IsNotNull (ex.Message, "#B4");
1272 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1273 Assert.IsNull (ex.ParamName, "#B6");
1279 public void DeleteSubKeyTree_Name_Null ()
1281 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1283 softwareKey.DeleteSubKeyTree (null);
1285 } catch (ArgumentNullException ex) {
1286 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1287 Assert.IsNull (ex.InnerException, "#3");
1288 Assert.IsNotNull (ex.Message, "#4");
1289 Assert.AreEqual ("name", ex.ParamName, "#5");
1295 public void DeleteValue ()
1297 string subKeyName = Guid.NewGuid ().ToString ();
1300 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1301 // check if key was successfully created
1302 Assert.IsNotNull (createdKey, "#A1");
1303 createdKey.SetValue ("name1", "value1");
1304 createdKey.SetValue ("name2", "value2");
1305 string [] names = createdKey.GetValueNames ();
1306 Assert.IsNotNull (names, "#A2");
1307 Assert.AreEqual (2, names.Length, "#A3");
1308 Assert.IsNotNull (names [0], "#A4");
1309 Assert.AreEqual ("name1", names [0], "#A5");
1310 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A6");
1311 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A7");
1312 Assert.AreEqual ("name2", names [1], "#A8");
1313 Assert.IsNotNull (createdKey.GetValue ("name2"), "#A9");
1314 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#A10");
1316 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1317 Assert.IsNotNull (createdKey, "#B1");
1318 createdKey.DeleteValue ("name1");
1319 string [] names = createdKey.GetValueNames ();
1320 Assert.IsNotNull (names, "#B2");
1321 Assert.AreEqual (1, names.Length, "#B3");
1322 Assert.IsNotNull (names [0], "#B4");
1323 Assert.AreEqual ("name2", names [0], "#B5");
1324 Assert.IsNotNull (createdKey.GetValue ("name2"), "#B6");
1325 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#B7");
1326 createdKey.DeleteValue (new string ('a', 400), false);
1328 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1329 string [] names = createdKey.GetValueNames ();
1330 Assert.IsNotNull (names, "#C1");
1331 Assert.AreEqual (1, names.Length, "#C2");
1332 Assert.IsNotNull (names [0], "#C3");
1333 Assert.AreEqual ("name2", names [0], "#C4");
1334 Assert.IsNotNull (createdKey.GetValue ("name2"), "#C5");
1335 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#C6");
1339 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1340 if (createdKey != null) {
1341 createdKey.Close ();
1342 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1350 public void DeleteValue_Key_ReadOnly ()
1352 string subKeyName = Guid.NewGuid ().ToString ();
1355 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1356 createdKey.SetValue ("name1", "value1");
1359 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1361 // deleting value that exists
1362 createdKey.DeleteValue ("name1");
1363 Assert.Fail ("#A1");
1364 } catch (UnauthorizedAccessException ex) {
1365 // Cannot write to the registry key
1366 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#A2");
1367 Assert.IsNotNull (ex.Message, "#A3");
1368 Assert.IsNull (ex.InnerException, "#A4");
1372 // deleting value that exists
1373 createdKey.DeleteValue ("name1", true);
1374 Assert.Fail ("#B1");
1375 } catch (UnauthorizedAccessException ex) {
1376 // Cannot write to the registry key
1377 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#B2");
1378 Assert.IsNotNull (ex.Message, "#B3");
1379 Assert.IsNull (ex.InnerException, "#B4");
1383 // deleting value that exists
1384 createdKey.DeleteValue ("name1", false);
1385 Assert.Fail ("#C1");
1386 } catch (UnauthorizedAccessException ex) {
1387 // Cannot write to the registry key
1388 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#C2");
1389 Assert.IsNotNull (ex.Message, "#C3");
1390 Assert.IsNull (ex.InnerException, "#C4");
1394 // deleting value that does not exist
1395 createdKey.DeleteValue ("name2");
1396 Assert.Fail ("#D1");
1397 } catch (UnauthorizedAccessException ex) {
1398 // Cannot write to the registry key
1399 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#D2");
1400 Assert.IsNotNull (ex.Message, "#D3");
1401 Assert.IsNull (ex.InnerException, "#D4");
1405 // deleting value that does not exist
1406 createdKey.DeleteValue ("name2", true);
1407 Assert.Fail ("#E1");
1408 } catch (UnauthorizedAccessException ex) {
1409 // Cannot write to the registry key
1410 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#E2");
1411 Assert.IsNotNull (ex.Message, "#E3");
1412 Assert.IsNull (ex.InnerException, "#E4");
1416 // deleting value that does not exist
1417 createdKey.DeleteValue ("name2", false);
1418 Assert.Fail ("#F1");
1419 } catch (UnauthorizedAccessException ex) {
1420 // Cannot write to the registry key
1421 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#F2");
1422 Assert.IsNotNull (ex.Message, "#F3");
1423 Assert.IsNull (ex.InnerException, "#F4");
1428 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1429 if (createdKey != null) {
1430 createdKey.Close ();
1431 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1439 public void DeleteValue_Key_Removed ()
1441 string subKeyName = Guid.NewGuid ().ToString ();
1444 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1445 // check if key was successfully created
1446 Assert.IsNotNull (createdKey, "#1");
1447 createdKey.SetValue ("name1", "value1");
1449 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1450 Assert.IsNotNull (createdKey, "#2");
1451 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1452 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#3");
1454 createdKey.DeleteValue ("name1");
1455 createdKey.DeleteValue ("name1", true);
1456 createdKey.DeleteValue ("name1", false);
1460 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1461 if (createdKey != null) {
1462 createdKey.Close ();
1463 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1471 public void DeleteValue_Value_DoesNotExist ()
1473 string subKeyName = Guid.NewGuid ().ToString ();
1476 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1477 // check if key was successfully created
1478 Assert.IsNotNull (createdKey, "#A1");
1479 createdKey.SetValue ("name1", "value1");
1482 createdKey.DeleteValue ("name2");
1483 Assert.Fail ("#B1");
1484 } catch (ArgumentException ex) {
1485 // No value exists with that name
1486 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1487 Assert.IsNull (ex.InnerException, "#B3");
1488 Assert.IsNotNull (ex.Message, "#B4");
1489 Assert.IsNull (ex.ParamName, "#B5");
1493 createdKey.DeleteValue ("name2", true);
1494 Assert.Fail ("#C1");
1495 } catch (ArgumentException ex) {
1496 // No value exists with that name
1497 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1498 Assert.IsNull (ex.InnerException, "#C3");
1499 Assert.IsNotNull (ex.Message, "#C4");
1500 Assert.IsNull (ex.ParamName, "#C5");
1503 createdKey.DeleteValue ("name2", false);
1507 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1508 if (createdKey != null) {
1509 createdKey.Close ();
1510 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1518 public void DeleteValue_Name_Empty ()
1520 string subKeyName = Guid.NewGuid ().ToString ();
1523 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1524 createdKey.SetValue ("name1", "value1");
1525 createdKey.SetValue (string.Empty, "value2");
1527 string [] names = createdKey.GetValueNames ();
1528 Assert.IsNotNull (names, "#A1");
1529 Assert.AreEqual (2, names.Length, "#A2");
1530 Assert.IsNotNull (names [0], "#A3");
1532 Assert.AreEqual ("name1", names [0], "#A4");
1534 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1535 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1536 Assert.IsNotNull (names [1], "#A7");
1538 Assert.AreEqual (string.Empty, names [1], "#A8");
1540 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A9");
1541 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#A10");
1543 createdKey.DeleteValue (string.Empty);
1545 names = createdKey.GetValueNames ();
1546 Assert.IsNotNull (names, "#B1");
1547 Assert.AreEqual (1, names.Length, "#B2");
1548 Assert.IsNotNull (names [0], "#B3");
1549 Assert.AreEqual ("name1", names [0], "#B4");
1550 Assert.IsNotNull (createdKey.GetValue ("name1"), "#B5");
1551 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#B6");
1554 createdKey.DeleteValue (string.Empty);
1555 Assert.Fail ("#C1");
1556 } catch (ArgumentException ex) {
1557 // No value exists with that name
1558 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1559 Assert.IsNull (ex.InnerException, "#C3");
1560 Assert.IsNotNull (ex.Message, "#C4");
1561 Assert.IsNull (ex.ParamName, "#C5");
1565 createdKey.DeleteValue (string.Empty, true);
1566 Assert.Fail ("#D1");
1567 } catch (ArgumentException ex) {
1568 // No value exists with that name
1569 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
1570 Assert.IsNull (ex.InnerException, "#D3");
1571 Assert.IsNotNull (ex.Message, "#D4");
1572 Assert.IsNull (ex.ParamName, "#D5");
1575 createdKey.DeleteValue (string.Empty, false);
1577 names = createdKey.GetValueNames ();
1578 Assert.IsNotNull (names, "#E1");
1579 Assert.AreEqual (1, names.Length, "#E2");
1580 Assert.IsNotNull (names [0], "#E3");
1581 Assert.AreEqual ("name1", names [0], "#E4");
1582 Assert.IsNotNull (createdKey.GetValue ("name1"), "#E5");
1583 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#E6");
1587 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1588 if (createdKey != null) {
1589 createdKey.Close ();
1590 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1598 public void DeleteValue_Name_Null ()
1600 string subKeyName = Guid.NewGuid ().ToString ();
1603 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1604 createdKey.SetValue ("name1", "value1");
1605 createdKey.SetValue (null, "value2");
1607 string [] names = createdKey.GetValueNames ();
1608 Assert.IsNotNull (names, "#A1");
1609 Assert.AreEqual (2, names.Length, "#A2");
1610 Assert.IsNotNull (names [0], "#A3");
1612 Assert.AreEqual ("name1", names [0], "#A4");
1614 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1615 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1616 Assert.IsNotNull (names [1], "#A7");
1618 Assert.AreEqual (string.Empty, names [1], "#A8");
1620 Assert.IsNotNull (createdKey.GetValue (null), "#A9");
1621 Assert.AreEqual ("value2", createdKey.GetValue (null), "#A10");
1624 createdKey.DeleteValue (null);
1625 Assert.Fail ("#B1");
1626 } catch (ArgumentNullException ex) {
1627 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1628 Assert.IsNull (ex.InnerException, "#B3");
1629 Assert.IsNotNull (ex.Message, "#B4");
1630 Assert.AreEqual ("name", ex.ParamName, "#B5");
1634 createdKey.DeleteValue (null, true);
1635 Assert.Fail ("#C1");
1636 } catch (ArgumentNullException ex) {
1637 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
1638 Assert.IsNull (ex.InnerException, "#C3");
1639 Assert.IsNotNull (ex.Message, "#C4");
1640 Assert.AreEqual ("name", ex.ParamName, "#C5");
1644 createdKey.DeleteValue (null, false);
1645 Assert.Fail ("#D1");
1646 } catch (ArgumentNullException ex) {
1647 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
1648 Assert.IsNull (ex.InnerException, "#D3");
1649 Assert.IsNotNull (ex.Message, "#D4");
1650 Assert.AreEqual ("name", ex.ParamName, "#D5");
1655 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1656 if (createdKey != null) {
1657 createdKey.Close ();
1658 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1666 [DllImport ("advapi32.dll", CharSet = CharSet.Unicode)]
1667 static extern int RegOpenKeyEx (IntPtr keyBase, string keyName, IntPtr reserved, int access, out IntPtr keyHandle);
1669 const int RegCurrentUserHive = -2147483647;
1670 const int RegAccessRead = 0x00020019;
1672 // FromHandle is specially designed to retrieve a RegistryKey instance based on a IntPtr
1673 // retrieved using any unmanaged registry function, so we use directly RegOpenKeyEx to load
1674 // our handle and then pass it to the new 4.0 method.
1676 public void FromHandle ()
1678 // Not supported on Unix
1682 string subKeyName = Guid.NewGuid ().ToString ();
1684 using (RegistryKey key = Registry.CurrentUser.CreateSubKey (subKeyName))
1685 key.SetValue ("Name", "Mono001");
1688 int res = RegOpenKeyEx (new IntPtr (RegCurrentUserHive), subKeyName, IntPtr.Zero, RegAccessRead, out handle);
1690 using (RegistryKey key = RegistryKey.FromHandle (new SafeRegistryHandle (handle, true))) {
1691 Assert.AreEqual (String.Empty, key.Name, "#A0");
1692 Assert.AreEqual ("Mono001", key.GetValue ("Name"), "#A1");
1695 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1696 if (createdKey != null) {
1697 createdKey.Close ();
1698 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1704 public void FromHandle_InvalidHandle ()
1706 // Not supported on Unix
1710 string subKeyName = Guid.NewGuid ().ToString ();
1712 using (RegistryKey key = RegistryKey.FromHandle (new SafeRegistryHandle (IntPtr.Zero, true))) {
1713 Assert.AreEqual (String.Empty, key.Name, "#A0");
1715 // Any operation should throw a IOException, since even if we have a RegistryKey instance,
1716 // the handle is not valid.
1717 key.CreateSubKey ("ChildSubKey");
1718 Assert.Fail ("#Exc0");
1720 } catch (IOException) {
1722 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1723 if (createdKey != null) {
1724 createdKey.Close ();
1725 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1731 public void Handle ()
1736 string subKeyName = Guid.NewGuid ().ToString ();
1737 RegistryKey subkey = null;
1739 subkey = Registry.CurrentUser.CreateSubKey (subKeyName);
1740 Assert.AreEqual (true, subkey.Handle != null, "#A0");
1741 Assert.AreEqual (false, subkey.Handle.IsClosed, "#A1");
1742 Assert.AreEqual (false, subkey.Handle.IsInvalid, "#A2");
1746 if (subkey.Handle != null)
1747 Console.WriteLine (); // Avoids a warning at compile time
1748 Assert.Fail ("#Disposed");
1749 } catch (ObjectDisposedException) {
1753 if (subkey != null) {
1755 Registry.CurrentUser.DeleteSubKeyTree (subKeyName, false);
1762 public void GetValue ()
1764 string subKeyName = Guid.NewGuid ().ToString ();
1767 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1768 createdKey.SetValue ("name1", "value1");
1769 createdKey.SetValue ("name2", "value2");
1772 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1773 Assert.IsNotNull (createdKey.GetValue ("name1"), "#1");
1774 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#2");
1775 Assert.IsNotNull (createdKey.GetValue ("name2"), "#3");
1776 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#4");
1777 Assert.IsNull (createdKey.GetValue ("name3"), "#5");
1778 Assert.AreEqual ("value3", createdKey.GetValue ("name3", "value3"), "#6");
1779 Assert.IsNull (createdKey.GetValue ("name3", null), "#7");
1780 Assert.IsNull (createdKey.GetValue (new string ('a', 400)), "#8");
1784 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1785 if (createdKey != null) {
1786 createdKey.Close ();
1787 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1795 public void GetValue_Key_Removed ()
1797 string subKeyName = Guid.NewGuid ().ToString ();
1800 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1801 createdKey.SetValue ("name1", "value1");
1802 createdKey.SetValue ("name2", "value2");
1805 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1806 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1808 Assert.IsNull (createdKey.GetValue ("name1"), "#1");
1809 Assert.IsNotNull (createdKey.GetValue ("name1", "default"), "#2");
1810 Assert.AreEqual ("default", createdKey.GetValue ("name1", "default"), "#3");
1811 Assert.IsNull (createdKey.GetValue ("name3"), "#3");
1812 Assert.IsNotNull (createdKey.GetValue ("name3", "default"), "#4");
1813 Assert.AreEqual ("default", createdKey.GetValue ("name3", "default"), "#5");
1817 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1818 if (createdKey != null) {
1819 createdKey.Close ();
1820 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1828 public void GetValue_Name_Empty ()
1830 string subKeyName = Guid.NewGuid ().ToString ();
1833 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1834 createdKey.SetValue ("name1", "value1");
1835 createdKey.SetValue ("name2", "value2");
1837 Assert.IsNull (createdKey.GetValue (string.Empty), "#A1");
1838 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#A2");
1839 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#A3");
1840 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#A4");
1843 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1844 Assert.IsNull (createdKey.GetValue (string.Empty), "#B1");
1845 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#B2");
1846 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#B3");
1847 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#B4");
1850 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1851 createdKey.SetValue (string.Empty, "value1");
1852 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#C1");
1853 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#C2");
1854 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#C3");
1855 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#C4");
1858 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1859 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#D1");
1860 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#D2");
1861 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#D3");
1862 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#D4");
1866 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1867 if (createdKey != null) {
1868 createdKey.Close ();
1869 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1877 public void GetValue_Name_Null ()
1879 string subKeyName = Guid.NewGuid ().ToString ();
1882 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1883 createdKey.SetValue ("name1", "value1");
1884 createdKey.SetValue ("name2", "value2");
1886 Assert.IsNull (createdKey.GetValue (null), "#A1");
1887 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#A2");
1888 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#A3");
1889 Assert.IsNull (createdKey.GetValue (null, null), "#A4");
1892 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1893 Assert.IsNull (createdKey.GetValue (null), "#B1");
1894 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#B2");
1895 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#B3");
1896 Assert.IsNull (createdKey.GetValue (null, null), "#B4");
1899 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1900 createdKey.SetValue (string.Empty, "value1");
1901 Assert.IsNotNull (createdKey.GetValue (null), "#C1");
1902 Assert.AreEqual ("value1", createdKey.GetValue (null), "#C2");
1903 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#C3");
1904 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#C4");
1907 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1908 Assert.IsNotNull (createdKey.GetValue (null), "#D1");
1909 Assert.AreEqual ("value1", createdKey.GetValue (null), "#D2");
1910 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#D3");
1911 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#D4");
1915 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1916 if (createdKey != null) {
1917 createdKey.Close ();
1918 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1926 public void GetValue_Expand ()
1928 string subKeyName = Guid.NewGuid ().ToString ();
1931 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1932 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1933 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1935 createdKey.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1936 RegistryValueKind.ExpandString);
1937 createdKey.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1938 createdKey.SetValue ("name3", "just some text",
1939 RegistryValueKind.ExpandString);
1941 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#A1");
1942 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#A2");
1943 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#A3");
1944 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1945 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A4");
1946 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1947 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A5");
1948 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1949 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A6");
1950 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1951 null, RegistryValueOptions.None), "#A7");
1952 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1953 null, RegistryValueOptions.None), "#A8");
1954 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1955 null, RegistryValueOptions.None), "#A9");
1957 Environment.SetEnvironmentVariable ("MONO_TEST1", "789");
1958 Environment.SetEnvironmentVariable ("MONO_TEST2", "666");
1960 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#B1");
1961 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#B2");
1962 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#B3");
1963 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1964 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B4");
1965 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1966 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B5");
1967 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1968 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B6");
1969 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1970 null, RegistryValueOptions.None), "#B7");
1971 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1972 null, RegistryValueOptions.None), "#B8");
1973 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1974 null, RegistryValueOptions.None), "#B9");
1976 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1977 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#C1");
1978 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#C2");
1979 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#C3");
1980 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1981 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C4");
1982 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1983 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C5");
1984 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1985 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C6");
1986 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1987 null, RegistryValueOptions.None), "#C7");
1988 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1989 null, RegistryValueOptions.None), "#C8");
1990 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1991 null, RegistryValueOptions.None), "#C9");
1993 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1994 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1996 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#D1");
1997 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#D2");
1998 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#D3");
1999 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
2000 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D4");
2001 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
2002 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D5");
2003 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
2004 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D6");
2005 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
2006 null, RegistryValueOptions.None), "#D7");
2007 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
2008 null, RegistryValueOptions.None), "#D8");
2009 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
2010 null, RegistryValueOptions.None), "#D9");
2014 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2015 if (createdKey != null) {
2016 createdKey.Close ();
2017 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2025 public void GetValueNames ()
2027 string subKeyName = Guid.NewGuid ().ToString ();
2030 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2031 string [] names = createdKey.GetValueNames ();
2032 Assert.IsNotNull (names, "#A1");
2033 Assert.AreEqual (0, names.Length, "#A2");
2035 createdKey.SetValue ("name1", "value1");
2036 createdKey.SetValue ("name2", "value2");
2037 createdKey.SetValue ("namelong", "value3");
2038 createdKey.SetValue ("name3", "value4");
2040 Assert.AreEqual (4, createdKey.ValueCount, "#B1");
2041 names = createdKey.GetValueNames ();
2042 Assert.IsNotNull (names, "#B2");
2043 Assert.AreEqual (4, names.Length, "#B3");
2046 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2047 string [] names = createdKey.GetValueNames ();
2048 Assert.IsNotNull (names, "#C1");
2049 Assert.AreEqual (4, names.Length, "#C2");
2051 // Mono's Unix registry API uses a hashtable to store the
2052 // values (and their names), so names are not returned in
2055 // to test whether the names returned by GetValueNames
2056 // match what we expect, we use these names to remove the
2057 // the values from the created keys and such we should end
2058 // up with zero values
2059 for (int i = 0; i < names.Length; i++) {
2060 string valueName = names [i];
2061 createdKey.DeleteValue (valueName);
2064 // all values should be removed now
2065 Assert.AreEqual (0, createdKey.ValueCount, "#C3");
2069 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2070 if (createdKey != null) {
2071 createdKey.Close ();
2072 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2080 public void GetValueNames_Key_Removed ()
2082 string subKeyName = Guid.NewGuid ().ToString ();
2085 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2086 createdKey.SetValue ("name1", "value1");
2087 createdKey.SetValue ("name2", "value2");
2089 string [] names = createdKey.GetValueNames ();
2090 Assert.IsNotNull (names, "#A1");
2091 Assert.AreEqual (2, names.Length, "#A2");
2094 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2095 string [] names = createdKey.GetValueNames ();
2096 Assert.IsNotNull (names, "#B1");
2097 Assert.AreEqual (2, names.Length, "#B2");
2099 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2102 createdKey.GetValueNames ();
2103 Assert.Fail ("#C1");
2104 } catch (IOException ex) {
2105 // Illegal operation attempted on a registry key that
2106 // has been marked for deletion
2107 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
2108 Assert.IsNotNull (ex.Message, "#C3");
2109 Assert.IsNull (ex.InnerException, "#C4");
2114 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2115 if (createdKey != null) {
2116 createdKey.Close ();
2117 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2124 [Test] // bug #78519
2125 public void GetSubKeyNamesTest ()
2127 string subKeyName = Guid.NewGuid ().ToString ();
2129 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2131 // check if key was successfully created
2132 Assert.IsNotNull (createdKey, "#A");
2134 RegistryKey subKey = createdKey.CreateSubKey ("foo");
2135 Assert.IsNotNull (subKey, "#B1");
2136 Assert.AreEqual (1, createdKey.SubKeyCount, "#B2");
2137 string[] subKeyNames = createdKey.GetSubKeyNames ();
2138 Assert.IsNotNull (subKeyNames, "#B3");
2139 Assert.AreEqual (1, subKeyNames.Length, "#B4");
2140 Assert.AreEqual ("foo", subKeyNames[0], "#B5");
2142 subKey = createdKey.CreateSubKey ("longfoo");
2143 Assert.IsNotNull (subKey, "#C1");
2144 Assert.AreEqual (2, createdKey.SubKeyCount, "#C2");
2145 subKeyNames = createdKey.GetSubKeyNames ();
2146 Assert.IsNotNull (subKeyNames, "#C3");
2147 Assert.AreEqual (2, subKeyNames.Length, "#C4");
2148 Assert.AreEqual ("foo", subKeyNames [0], "#C5");
2149 Assert.AreEqual ("longfoo", subKeyNames [1], "#C6");
2151 subKey = createdKey.CreateSubKey ("sfoo");
2152 Assert.IsNotNull (subKey, "#D1");
2153 Assert.AreEqual (3, createdKey.SubKeyCount, "#D2");
2154 subKeyNames = createdKey.GetSubKeyNames ();
2155 Assert.IsNotNull (subKeyNames, "#D3");
2156 Assert.AreEqual (3, subKeyNames.Length, "#D4");
2157 Assert.AreEqual ("foo", subKeyNames [0], "#D5");
2158 Assert.AreEqual ("longfoo", subKeyNames [1], "#D6");
2159 Assert.AreEqual ("sfoo", subKeyNames [2], "#D7");
2161 foreach (string name in subKeyNames) {
2162 createdKey.DeleteSubKeyTree (name);
2164 Assert.AreEqual (0, createdKey.SubKeyCount, "#E");
2167 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2172 public void OpenRemoteBaseKey ()
2174 // access to registry of remote machines is not implemented on unix
2178 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
2179 RegistryHive.CurrentUser, Environment.MachineName);
2180 Assert.IsNotNull (hive, "#1");
2182 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
2183 Assert.IsNotNull (key, "#2");
2190 public void OpenRemoteBaseKey_MachineName_Null ()
2193 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser, null);
2195 } catch (ArgumentNullException ex) {
2196 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2197 Assert.IsNull (ex.InnerException, "#3");
2198 Assert.IsNotNull (ex.Message, "#4");
2199 Assert.AreEqual ("machineName", ex.ParamName, "#5");
2204 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
2206 // access to registry of remote machines is not implemented on unix
2211 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
2214 } catch (IOException ex) {
2215 // The network path was not found
2216 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2217 Assert.IsNotNull (ex.Message, "#3");
2218 Assert.IsNull (ex.InnerException, "#4");
2222 [Test] // bug #322839
2223 public void SetValue1_EntityReferences ()
2225 string subKeyName = Guid.NewGuid ().ToString ();
2228 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2229 // we created a new subkey, so value should not exist
2230 Assert.IsNull (createdKey.GetValue ("FirstName&\"<LastName>\""), "#A1");
2232 createdKey.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
2234 object name = createdKey.GetValue ("FirstName&\"<LastName>\"");
2235 // value should exist
2236 Assert.IsNotNull (name, "#A2");
2237 // type of value should be string
2238 Assert.AreEqual (typeof (string), name.GetType (), "#A3");
2239 // ensure value matches
2240 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#A4");
2242 // we created a new subkey, so value should not exist
2243 Assert.IsNull (createdKey.GetValue ("Info"), "#B1");
2245 createdKey.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" });
2247 object info = createdKey.GetValue ("Info");
2248 // value should exist
2249 Assert.IsNotNull (info, "#B2");
2250 // type of value should be string
2251 Assert.AreEqual (typeof (string []), info.GetType (), "#B3");
2252 // ensure value matches
2253 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#B4");
2256 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2257 object name = openedKey.GetValue ("FirstName&\"<LastName>\"");
2258 Assert.IsNotNull (name, "#C1");
2259 Assert.AreEqual (typeof (string), name.GetType (), "#C2");
2260 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#C3");
2262 object info = openedKey.GetValue ("Info");
2263 Assert.IsNotNull (info, "#D1");
2264 Assert.AreEqual (typeof (string []), info.GetType (), "#D2");
2265 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#D3");
2269 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2273 [Test] // SetValue (String, Object)
2274 public void SetValue1_Name_Null ()
2276 string subKeyName = Guid.NewGuid ().ToString ();
2278 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2280 createdKey.SetValue (null, "value1");
2281 string [] names = createdKey.GetValueNames ();
2282 Assert.IsNotNull (names, "#A1");
2283 Assert.AreEqual (1, names.Length, "#A2");
2284 Assert.IsNotNull (names [0], "#A3");
2285 Assert.AreEqual (string.Empty, names [0], "#A4");
2286 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2287 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2288 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2289 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2291 createdKey.SetValue (string.Empty, "value2");
2292 names = createdKey.GetValueNames ();
2293 Assert.IsNotNull (names, "#B1");
2294 Assert.AreEqual (1, names.Length, "#B2");
2295 Assert.IsNotNull (names [0], "#B3");
2296 Assert.AreEqual (string.Empty, names [0], "#B4");
2297 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2298 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2299 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2300 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2303 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2307 [Test] // SetValue (String, Object)
2308 public void SetValue1_Name_Empty ()
2310 string subKeyName = Guid.NewGuid ().ToString ();
2312 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2314 createdKey.SetValue (string.Empty, "value1");
2315 string [] names = createdKey.GetValueNames ();
2316 Assert.IsNotNull (names, "#A1");
2317 Assert.AreEqual (1, names.Length, "#A2");
2318 Assert.IsNotNull (names [0], "#A3");
2319 Assert.AreEqual (string.Empty, names [0], "#A4");
2320 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2321 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2322 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2323 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2325 createdKey.SetValue (null, "value2");
2326 names = createdKey.GetValueNames ();
2327 Assert.IsNotNull (names, "#B1");
2328 Assert.AreEqual (1, names.Length, "#B2");
2329 Assert.IsNotNull (names [0], "#B3");
2330 Assert.AreEqual (string.Empty, names [0], "#B4");
2331 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2332 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2333 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2334 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2337 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2341 [Test] // SetValue (String, Object)
2342 public void SetValue1_Name_MaxLength ()
2344 string subKeyName = Guid.NewGuid ().ToString ();
2347 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2348 string name = new string ('a', 254);
2350 createdKey.SetValue (name, "value1");
2351 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2352 createdKey.DeleteValue (name);
2353 Assert.IsNull (createdKey.GetValue (name), "#A2");
2355 name = new string ('a', 255);
2357 createdKey.SetValue (name, "value2");
2358 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2359 createdKey.DeleteValue (name);
2360 Assert.IsNull (createdKey.GetValue (name), "#B2");
2362 name = new string ('a', 256);
2365 createdKey.SetValue (name, "value2");
2366 Assert.Fail ("#C1");
2367 } catch (ArgumentException ex) {
2368 // 1.x: Registry subkeys should not be
2369 // greater than or equal to 255 characters
2371 // 2.x: Registry subkeys should not be
2372 // greater than 255 characters
2373 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2374 Assert.IsNull (ex.InnerException, "#C3");
2375 Assert.IsNotNull (ex.Message, "#C4");
2376 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2377 Assert.IsNull (ex.ParamName, "#C6");
2382 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2383 if (createdKey != null) {
2384 createdKey.Close ();
2385 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2392 [Test] // SetValue (String, Object)
2393 public void SetValue1_Value_Null ()
2395 string subKeyName = Guid.NewGuid ().ToString ();
2397 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2400 createdKey.SetValue ("Name", null);
2402 } catch (ArgumentNullException ex) {
2403 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2404 Assert.IsNull (ex.InnerException, "#3");
2405 Assert.IsNotNull (ex.Message, "#4");
2406 Assert.AreEqual ("value", ex.ParamName, "#5");
2410 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2414 [Test] // SetValue (String, Object)
2415 public void SetValue1_Boolean ()
2417 string subKeyName = Guid.NewGuid ().ToString ();
2420 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2421 // we created a new subkey, so value should not exist
2422 Assert.IsNull (createdKey.GetValue ("Installed"), "#A1");
2424 createdKey.SetValue ("Installed", true);
2426 object value = createdKey.GetValue ("Installed");
2427 // value should exist
2428 Assert.IsNotNull (value, "#A2");
2429 // type of value should be string
2430 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2431 // ensure value matches
2432 Assert.AreEqual (true.ToString (), value, "#A4");
2435 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2436 object value = openedKey.GetValue ("Installed");
2437 Assert.IsNotNull (value, "#B1");
2438 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2439 Assert.AreEqual (true.ToString (), value, "#B3");
2443 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2447 [Test] // SetValue (String, Object)
2448 public void SetValue1_Byte ()
2450 string subKeyName = Guid.NewGuid ().ToString ();
2453 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2454 // we created a new subkey, so value should not exist
2455 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2457 createdKey.SetValue ("Flags", (byte) 5);
2459 object value = createdKey.GetValue ("Flags");
2460 // value should exist
2461 Assert.IsNotNull (value, "#A2");
2462 // type of value should be string
2463 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2464 // ensure value matches
2465 Assert.AreEqual ("5", value, "#A4");
2468 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2469 object value = openedKey.GetValue ("Flags");
2470 Assert.IsNotNull (value, "#B1");
2471 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2472 Assert.AreEqual ("5", value, "#B3");
2476 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2480 [Test] // SetValue (String, Object)
2481 public void SetValue1_ByteArray ()
2483 string subKeyName = Guid.NewGuid ().ToString ();
2486 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2487 // we created a new subkey, so value should not exist
2488 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2490 createdKey.SetValue ("Flags", new byte [] { 1, 5 });
2492 object value = createdKey.GetValue ("Flags");
2493 // value should exist
2494 Assert.IsNotNull (value, "#A2");
2495 // type of value should be string
2496 Assert.AreEqual (typeof (byte []), value.GetType (), "#3");
2497 // ensure value matches
2498 Assert.AreEqual (new byte [] { 1, 5 }, value, "#4");
2501 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2502 object value = openedKey.GetValue ("Flags");
2503 Assert.IsNotNull (value, "#B1");
2504 Assert.AreEqual (typeof (byte []), value.GetType (), "#B2");
2505 Assert.AreEqual (new byte [] { 1, 5 }, value, "#B3");
2509 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2513 [Test] // SetValue (String, Object)
2514 public void SetValue1_DateTime ()
2516 string subKeyName = Guid.NewGuid ().ToString ();
2519 object rawValue = DateTime.Now;
2521 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2522 // we created a new subkey, so value should not exist
2523 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2525 createdKey.SetValue ("Path", rawValue);
2527 object value = createdKey.GetValue ("Path");
2528 // value should exist
2529 Assert.IsNotNull (value, "#A2");
2530 // type of value should be string
2531 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2532 // ensure value matches
2533 Assert.AreEqual (rawValue.ToString (), value, "#A4");
2536 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2537 object value = openedKey.GetValue ("Path");
2538 Assert.IsNotNull (value, "#B1");
2539 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2540 Assert.AreEqual (rawValue.ToString (), value, "#B3");
2544 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2549 public void SetValue_Int32 ()
2551 string subKeyName = Guid.NewGuid ().ToString ();
2554 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2555 // we created a new subkey, so value should not exist
2556 Assert.IsNull (createdKey.GetValue ("RefCount"), "#A1");
2558 createdKey.SetValue ("RefCount", 5);
2560 object value = createdKey.GetValue ("RefCount");
2561 // value should exist
2562 Assert.IsNotNull (value, "#A2");
2563 // type of value should be int
2564 Assert.AreEqual (typeof (int), value.GetType (), "#A3");
2565 // ensure value matches
2566 Assert.AreEqual (5, value, "#A4");
2569 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2570 object value = openedKey.GetValue ("RefCount");
2571 Assert.IsNotNull (value, "#B1");
2572 Assert.AreEqual (typeof (int), value.GetType (), "#B2");
2573 Assert.AreEqual (5, value, "#B3");
2577 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2581 [Test] // SetValue (String, Object)
2582 public void SetValue1_Int64 ()
2584 string subKeyName = Guid.NewGuid ().ToString ();
2587 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2588 // we created a new subkey, so value should not exist
2589 Assert.IsNull (createdKey.GetValue ("Ticks"), "#A1");
2591 createdKey.SetValue ("Ticks", 500L);
2593 object value = createdKey.GetValue ("Ticks");
2594 // value should exist
2595 Assert.IsNotNull (value, "#A2");
2596 // type of value should be string
2597 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2598 // ensure value matches
2599 Assert.AreEqual ("500", value, "#A4");
2602 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2603 object value = openedKey.GetValue ("Ticks");
2604 Assert.IsNotNull (value, "#B1");
2605 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2606 Assert.AreEqual ("500", value, "#B3");
2610 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2614 [Test] // SetValue (String, Object)
2615 public void SetValue1_String ()
2617 string subKeyName = Guid.NewGuid ().ToString ();
2620 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2621 // we created a new subkey, so value should not exist
2622 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2624 createdKey.SetValue ("Path", "/usr/lib/whatever");
2626 object path = createdKey.GetValue ("Path");
2627 // value should exist
2628 Assert.IsNotNull (path, "#A2");
2629 // type of value should be string
2630 Assert.AreEqual (typeof (string), path.GetType (), "#A3");
2631 // ensure value matches
2632 Assert.AreEqual ("/usr/lib/whatever", path, "#A4");
2635 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2636 object path = openedKey.GetValue ("Path");
2637 Assert.IsNotNull (path, "#B1");
2638 Assert.AreEqual (typeof (string), path.GetType (), "#B2");
2639 Assert.AreEqual ("/usr/lib/whatever", path, "#B3");
2643 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2647 [Test] // SetValue (String, Object)
2648 public void SetValue1_StringArray ()
2650 string subKeyName = Guid.NewGuid ().ToString ();
2653 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2654 // we created a new subkey, so value should not exist
2655 Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#A1");
2657 createdKey.SetValue ("DependsOnGroup", new string [] { "A", "B" });
2659 object value = createdKey.GetValue ("DependsOnGroup");
2660 // value should exist
2661 Assert.IsNotNull (value, "#A2");
2662 // type of value should be string
2663 Assert.AreEqual (typeof (string []), value.GetType (), "#A3");
2664 // ensure value matches
2665 Assert.AreEqual (new string [] { "A", "B" }, value, "#A4");
2668 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2669 object value = openedKey.GetValue ("DependsOnGroup");
2670 Assert.IsNotNull (value, "#B1");
2671 Assert.AreEqual (typeof (string []), value.GetType (), "#B2");
2672 Assert.AreEqual (new string [] { "A", "B" }, value, "#B3");
2676 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2680 [Test] // SetValue (String, Object)
2681 public void SetValue1_Key_ReadOnly ()
2683 string subKeyName = Guid.NewGuid ().ToString ();
2685 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2687 softwareKey.SetValue ("name1", "value1");
2689 } catch (UnauthorizedAccessException ex) {
2690 // Cannot write to the registry key
2691 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2692 Assert.IsNotNull (ex.Message, "#3");
2693 Assert.IsNull (ex.InnerException, "#4");
2697 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2699 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2702 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2704 createdKey.SetValue ("name1", "value1");
2706 } catch (UnauthorizedAccessException ex) {
2707 // Cannot write to the registry key
2708 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2709 Assert.IsNotNull (ex.Message, "#3");
2710 Assert.IsNull (ex.InnerException, "#4");
2715 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2716 if (createdKey != null) {
2717 createdKey.Close ();
2718 softwareKey.DeleteSubKeyTree (subKeyName);
2726 [Test] // SetValue (String, Object)
2727 public void SetValue1_Key_Removed ()
2729 string subKeyName = Guid.NewGuid ().ToString ();
2731 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2733 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2734 softwareKey.DeleteSubKeyTree (subKeyName);
2735 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2737 createdKey.SetValue ("name1", "value1");
2739 } catch (IOException ex) {
2740 // Illegal operation attempted on a registry key that
2741 // has been marked for deletion
2742 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2743 Assert.IsNotNull (ex.Message, "#4");
2744 Assert.IsNull (ex.InnerException, "#5");
2749 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2750 if (createdKey != null) {
2751 createdKey.Close ();
2752 softwareKey.DeleteSubKeyTree (subKeyName);
2760 [Test] // SetValue (String, Object, RegistryValueKind)
2761 public void SetValue2_Key_ReadOnly ()
2763 string subKeyName = Guid.NewGuid ().ToString ();
2765 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2767 softwareKey.SetValue ("name1", "value1",
2768 RegistryValueKind.String);
2770 } catch (UnauthorizedAccessException ex) {
2771 // Cannot write to the registry key
2772 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2773 Assert.IsNotNull (ex.Message, "#3");
2774 Assert.IsNull (ex.InnerException, "#4");
2778 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2780 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2783 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2785 createdKey.SetValue ("name1", "value1",
2786 RegistryValueKind.String);
2788 } catch (UnauthorizedAccessException ex) {
2789 // Cannot write to the registry key
2790 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2791 Assert.IsNotNull (ex.Message, "#3");
2792 Assert.IsNull (ex.InnerException, "#4");
2797 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2798 if (createdKey != null) {
2799 createdKey.Close ();
2800 softwareKey.DeleteSubKeyTree (subKeyName);
2808 [Test] // SetValue (String, Object, RegistryValueKind)
2809 public void SetValue2_Key_Removed ()
2811 string subKeyName = Guid.NewGuid ().ToString ();
2813 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2815 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2816 softwareKey.DeleteSubKeyTree (subKeyName);
2817 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2819 createdKey.SetValue ("name1", "value1",
2820 RegistryValueKind.String);
2822 } catch (IOException ex) {
2823 // Illegal operation attempted on a registry key that
2824 // has been marked for deletion
2825 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2826 Assert.IsNotNull (ex.Message, "#4");
2827 Assert.IsNull (ex.InnerException, "#5");
2832 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2833 if (createdKey != null) {
2834 createdKey.Close ();
2835 softwareKey.DeleteSubKeyTree (subKeyName);
2843 [Test] // SetValue (String, Object, RegistryValueKind)
2844 public void SetValue2_Name_Empty ()
2846 string subKeyName = Guid.NewGuid ().ToString ();
2848 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2850 createdKey.SetValue (string.Empty, "value1",
2851 RegistryValueKind.String);
2852 string [] names = createdKey.GetValueNames ();
2853 Assert.IsNotNull (names, "#A1");
2854 Assert.AreEqual (1, names.Length, "#A2");
2855 Assert.IsNotNull (names [0], "#A3");
2856 Assert.AreEqual (string.Empty, names [0], "#A4");
2857 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2858 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2859 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2860 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2862 createdKey.SetValue (null, "value2",
2863 RegistryValueKind.String);
2864 names = createdKey.GetValueNames ();
2865 Assert.IsNotNull (names, "#B1");
2866 Assert.AreEqual (1, names.Length, "#B2");
2867 Assert.IsNotNull (names [0], "#B3");
2868 Assert.AreEqual (string.Empty, names [0], "#B4");
2869 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2870 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2871 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2872 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2875 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2879 [Test] // SetValue (String, Object, RegistryValueKind)
2880 public void SetValue2_Name_MaxLength ()
2882 string subKeyName = Guid.NewGuid ().ToString ();
2885 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2886 string name = new string ('a', 254);
2888 createdKey.SetValue (name, "value1",
2889 RegistryValueKind.String);
2890 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2891 createdKey.DeleteValue (name);
2892 Assert.IsNull (createdKey.GetValue (name), "#A2");
2894 name = new string ('a', 255);
2896 createdKey.SetValue (name, "value2",
2897 RegistryValueKind.String);
2898 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2899 createdKey.DeleteValue (name);
2900 Assert.IsNull (createdKey.GetValue (name), "#B2");
2902 name = new string ('a', 256);
2905 createdKey.SetValue (name, "value2",
2906 RegistryValueKind.String);
2907 Assert.Fail ("#C1");
2908 } catch (ArgumentException ex) {
2909 // Registry subkeys should not be
2910 // greater than 255 characters
2911 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2912 Assert.IsNull (ex.InnerException, "#C3");
2913 Assert.IsNotNull (ex.Message, "#C4");
2914 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2915 Assert.IsNull (ex.ParamName, "#C6");
2920 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2921 if (createdKey != null) {
2922 createdKey.Close ();
2923 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2930 [Test] // SetValue (String, Object, RegistryValueKind)
2931 public void SetValue2_Name_Null ()
2933 string subKeyName = Guid.NewGuid ().ToString ();
2935 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2937 createdKey.SetValue (null, "value1",
2938 RegistryValueKind.String);
2939 string [] names = createdKey.GetValueNames ();
2940 Assert.IsNotNull (names, "#A1");
2941 Assert.AreEqual (1, names.Length, "#A2");
2942 Assert.IsNotNull (names [0], "#A3");
2943 Assert.AreEqual (string.Empty, names [0], "#A4");
2944 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2945 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2946 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2947 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2949 createdKey.SetValue (string.Empty, "value2",
2950 RegistryValueKind.String);
2951 names = createdKey.GetValueNames ();
2952 Assert.IsNotNull (names, "#B1");
2953 Assert.AreEqual (1, names.Length, "#B2");
2954 Assert.IsNotNull (names [0], "#B3");
2955 Assert.AreEqual (string.Empty, names [0], "#B4");
2956 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2957 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2958 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2959 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2962 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2966 [Test] // SetValue (String, Object, RegistryValueKind)
2967 public void SetValue2_Value_Null ()
2969 string subKeyName = Guid.NewGuid ().ToString ();
2971 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2974 createdKey.SetValue ("Name", null,
2975 RegistryValueKind.String);
2977 } catch (ArgumentNullException ex) {
2978 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2979 Assert.IsNull (ex.InnerException, "#3");
2980 Assert.IsNotNull (ex.Message, "#4");
2981 Assert.AreEqual ("value", ex.ParamName, "#5");
2985 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2990 public void SubKeyCount ()
2992 string subKeyName = Guid.NewGuid ().ToString ();
2995 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2996 // check if key was successfully created
2997 Assert.IsNotNull (createdKey, "#A1");
2998 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3001 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3002 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3005 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3007 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3008 Assert.IsNotNull (createdKey, "#B1");
3009 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3011 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3012 Assert.IsNotNull (createdKey2, "#B3");
3013 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
3014 createdKey2.DeleteSubKey ("monotemp1");
3015 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
3017 Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
3021 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3022 if (createdKey != null) {
3023 createdKey.Close ();
3024 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3032 public void SubKeyCount_Key_Removed ()
3034 string subKeyName = Guid.NewGuid ().ToString ();
3037 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3038 // check if key was successfully created
3039 Assert.IsNotNull (createdKey, "#A1");
3040 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3043 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3044 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3047 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3049 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3050 Assert.IsNotNull (createdKey, "#B1");
3051 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3053 // remove created key
3054 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3057 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
3058 } catch (IOException ex) {
3059 // Illegal operation attempted on a registry key that
3060 // has been marked for deletion
3061 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3062 Assert.IsNotNull (ex.Message, "#15");
3063 Assert.IsNull (ex.InnerException, "#16");
3068 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3069 if (createdKey != null) {
3070 createdKey.Close ();
3071 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3079 public void ValueCount ()
3081 string subKeyName = Guid.NewGuid ().ToString ();
3084 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3085 // check if key was successfully created
3086 Assert.IsNotNull (createdKey, "#A1");
3087 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3088 createdKey.SetValue ("name1", "value1");
3089 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3090 createdKey.SetValue ("name2", "value2");
3091 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3092 createdKey.SetValue ("name2", "value2b");
3093 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3094 createdKey.SetValue ("name3", "value3");
3095 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3097 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3098 Assert.IsNotNull (createdKey, "#B1");
3099 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3101 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3102 Assert.IsNotNull (createdKey2, "#B3");
3103 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
3104 createdKey2.DeleteValue ("name2");
3105 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
3107 Assert.AreEqual (2, createdKey.ValueCount, "#B6");
3111 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3112 if (createdKey != null) {
3113 createdKey.Close ();
3114 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3122 public void ValueCount_Key_Removed ()
3124 string subKeyName = Guid.NewGuid ().ToString ();
3127 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3128 // check if key was successfully created
3129 Assert.IsNotNull (createdKey, "#A1");
3130 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3131 createdKey.SetValue ("name1", "value1");
3132 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3133 createdKey.SetValue ("name2", "value2");
3134 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3135 createdKey.SetValue ("name2", "value2b");
3136 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3137 createdKey.SetValue ("name3", "value3");
3138 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3140 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3141 Assert.IsNotNull (createdKey, "#B1");
3142 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3144 // remove created key
3145 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3148 Assert.Fail ("#C1: " + createdKey.ValueCount);
3149 } catch (IOException ex) {
3150 // Illegal operation attempted on a registry key that
3151 // has been marked for deletion
3152 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3153 Assert.IsNotNull (ex.Message, "#15");
3154 Assert.IsNull (ex.InnerException, "#16");
3159 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3160 if (createdKey != null) {
3161 createdKey.Close ();
3162 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3170 public void bug79051 ()
3172 string subKeyName = Guid.NewGuid ().ToString ();
3174 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3176 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3177 createdKey.SetValue ("test", "whatever");
3178 createdKey.Close ();
3179 softwareKey.DeleteSubKeyTree (subKeyName);
3183 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3184 if (createdKey != null) {
3185 createdKey.Close ();
3186 softwareKey.DeleteSubKeyTree (subKeyName);
3196 public void TypeCastTests ()
3198 string subKeyName = Guid.NewGuid ().ToString ();
3199 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3201 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3202 createdKey.SetValue ("test-int", (int) 1, RegistryValueKind.DWord);
3203 createdKey.SetValue ("test-uint", (uint) 1, RegistryValueKind.DWord);
3204 createdKey.SetValue ("test-byte", (byte) 1, RegistryValueKind.DWord);
3205 createdKey.SetValue ("test-sbyte", (sbyte) 1, RegistryValueKind.DWord);
3206 createdKey.SetValue ("test-short", (short) 1, RegistryValueKind.DWord);
3207 createdKey.SetValue ("test-ushort", (ushort) 1, RegistryValueKind.DWord);
3208 createdKey.SetValue ("test-long", (long) 1, RegistryValueKind.DWord);
3209 createdKey.SetValue ("test-ulong", (ulong) 1, RegistryValueKind.DWord);
3210 createdKey.SetValue ("test-decimal", (decimal) 1, RegistryValueKind.DWord);
3211 createdKey.SetValue ("test-float", (float) 1, RegistryValueKind.DWord);
3212 createdKey.SetValue ("test-bool", true, RegistryValueKind.DWord);
3214 createdKey.SetValue ("dtest-int", (int) 1, RegistryValueKind.QWord);
3215 createdKey.SetValue ("dtest-uint", (uint) 1, RegistryValueKind.QWord);
3216 createdKey.SetValue ("dtest-byte", (byte) 1, RegistryValueKind.QWord);
3217 createdKey.SetValue ("dtest-sbyte", (sbyte) 1, RegistryValueKind.QWord);
3218 createdKey.SetValue ("dtest-short", (short) 1, RegistryValueKind.QWord);
3219 createdKey.SetValue ("dtest-ushort", (ushort) 1, RegistryValueKind.QWord);
3220 createdKey.SetValue ("dtest-long", (long) 1, RegistryValueKind.QWord);
3221 createdKey.SetValue ("dtest-ulong", (ulong) 1, RegistryValueKind.QWord);
3222 createdKey.SetValue ("dtest-decimal", (decimal) 1, RegistryValueKind.QWord);
3223 createdKey.SetValue ("dtest-float", (float) 1, RegistryValueKind.QWord);
3224 createdKey.SetValue ("dtest-bool", true, RegistryValueKind.QWord);
3226 object r = createdKey.GetValue ("test-int");
3227 Assert.AreEqual (r is int, true);
3228 Assert.AreEqual ((int) r, 1);
3230 r = createdKey.GetValue ("test-uint");
3231 Assert.AreEqual (r is int, true);
3232 Assert.AreEqual ((int) r, 1);
3233 r = createdKey.GetValue ("test-byte");
3234 Assert.AreEqual (r is int, true);
3235 Assert.AreEqual ((int) r, 1);
3236 r = createdKey.GetValue ("test-sbyte");
3237 Assert.AreEqual (r is int, true);
3238 Assert.AreEqual ((int) r, 1);
3239 r = createdKey.GetValue ("test-short");
3240 Assert.AreEqual (r is int, true);
3241 Assert.AreEqual ((int) r, 1);
3242 r = createdKey.GetValue ("test-ushort");
3243 Assert.AreEqual (r is int, true);
3244 Assert.AreEqual ((int) r, 1);
3245 r = createdKey.GetValue ("test-long");
3246 Assert.AreEqual (r is int, true);
3247 Assert.AreEqual ((int) r, 1);
3248 r = createdKey.GetValue ("test-ulong");
3249 Assert.AreEqual (r is int, true);
3250 Assert.AreEqual ((int) r, 1);
3252 r = createdKey.GetValue ("dtest-int");
3253 Assert.AreEqual (r is long, true);
3254 Assert.AreEqual ((long) r, 1);
3255 r = createdKey.GetValue ("dtest-uint");
3256 Assert.AreEqual (r is long, true);
3257 Assert.AreEqual ((long) r, 1);
3258 r = createdKey.GetValue ("dtest-byte");
3259 Assert.AreEqual (r is long, true);
3260 Assert.AreEqual ((long) r, 1);
3261 r = createdKey.GetValue ("dtest-sbyte");
3262 Assert.AreEqual (r is long, true);
3263 Assert.AreEqual ((long) r, 1);
3264 r = createdKey.GetValue ("dtest-short");
3265 Assert.AreEqual (r is long, true);
3266 Assert.AreEqual ((long) r, 1);
3267 r = createdKey.GetValue ("dtest-ushort");
3268 Assert.AreEqual (r is long, true);
3269 Assert.AreEqual ((long) r, 1);
3270 r = createdKey.GetValue ("dtest-long");
3271 Assert.AreEqual (r is long, true);
3272 Assert.AreEqual ((long) r, 1);
3273 r = createdKey.GetValue ("dtest-ulong");
3274 Assert.AreEqual (r is long, true);
3275 Assert.AreEqual ((long) r, 1);
3276 r = createdKey.GetValue ("dtest-decimal");
3277 Assert.IsTrue (r is long);
3278 Assert.AreEqual ((long) r, 1);
3279 r = createdKey.GetValue ("dtest-float");
3280 Assert.IsTrue (r is long);
3281 Assert.AreEqual ((long) r, 1);
3282 r = createdKey.GetValue ("dtest-bool");
3283 Assert.AreEqual (typeof (long), r.GetType ());
3286 createdKey.SetValue ("test-int", uint.MaxValue, RegistryValueKind.DWord);
3287 Assert.Fail ("#100");
3289 createdKey.SetValue ("test-int", ulong.MaxValue, RegistryValueKind.QWord);
3290 Assert.Fail ("#101");
3291 } catch (ArgumentException) {
3294 createdKey.Close ();
3295 softwareKey.DeleteSubKeyTree (subKeyName);
3299 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3300 if (createdKey != null) {
3301 createdKey.Close ();
3302 softwareKey.DeleteSubKeyTree (subKeyName);
3311 public void bug79059 ()
3313 string subKeyName = Guid.NewGuid ().ToString ();
3315 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3317 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3318 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
3320 createdKey.Close ();
3321 softwareKey.DeleteSubKeyTree (subKeyName);
3325 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3326 if (createdKey != null) {
3327 createdKey.Close ();
3328 softwareKey.DeleteSubKeyTree (subKeyName);
3337 public void bugnew1 ()
3339 string subKeyName = Guid.NewGuid ().ToString ();
3341 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3343 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3344 createdKey.SetValue ("name1", "value1");
3346 RegistryKey testKey = null;
3348 testKey = createdKey.OpenSubKey ("test", true);
3349 if (testKey == null)
3350 testKey = createdKey.CreateSubKey ("test");
3351 testKey.SetValue ("another", "one");
3353 if (testKey != null)
3357 createdKey.SetValue ("name2", "value2");
3358 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
3359 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
3360 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
3361 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
3363 string [] names = createdKey.GetValueNames ();
3364 Assert.IsNotNull (names, "#6");
3365 Assert.AreEqual (2, names.Length, "#7");
3366 Assert.AreEqual ("name1", names [0], "#8");
3367 Assert.AreEqual ("name2", names [1], "#9");
3369 softwareKey.DeleteSubKeyTree (subKeyName);
3371 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
3372 Assert.IsNull (openedKey, "#10");
3375 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
3376 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
3379 createdKey.GetValueNames ();
3380 Assert.Fail ("#13");
3381 } catch (IOException ex) {
3382 // Illegal operation attempted on a registry key that
3383 // has been marked for deletion
3384 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3385 Assert.IsNotNull (ex.Message, "#15");
3386 Assert.IsNull (ex.InnerException, "#16");
3390 createdKey.SetValue ("name1", "value1");
3391 Assert.Fail ("#17");
3392 } catch (IOException ex) {
3393 // Illegal operation attempted on a registry key that
3394 // has been marked for deletion
3395 Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
3396 Assert.IsNotNull (ex.Message, "#19");
3397 Assert.IsNull (ex.InnerException, "#20");
3401 createdKey.SetValue ("newname", "value1");
3402 Assert.Fail ("#21");
3403 } catch (IOException ex) {
3404 // Illegal operation attempted on a registry key that
3405 // has been marked for deletion
3406 Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
3407 Assert.IsNotNull (ex.Message, "#23");
3408 Assert.IsNull (ex.InnerException, "#24");
3411 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
3412 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
3413 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
3414 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
3417 createdKey.CreateSubKey ("new");
3418 Assert.Fail ("#29");
3419 } catch (IOException ex) {
3420 // Illegal operation attempted on a registry key that
3421 // has been marked for deletion
3422 Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
3423 Assert.IsNotNull (ex.Message, "#31");
3424 Assert.IsNull (ex.InnerException, "#32");
3429 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3430 if (createdKey != null) {
3431 createdKey.Close ();
3432 softwareKey.DeleteSubKeyTree (subKeyName);
3441 public void bugnew2 () // values cannot be written on registry root (hive)
3443 string [] names = Registry.CurrentUser.GetValueNames ();
3444 Assert.IsNotNull (names, "#1");
3445 Registry.CurrentUser.SetValue ("name1", "value1");
3446 Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
3447 Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
3448 string [] newNames = Registry.CurrentUser.GetValueNames ();
3449 Assert.IsNotNull (newNames, "#4");
3450 Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
3451 Registry.CurrentUser.DeleteValue ("name1");
3455 public void bugnew3 () // on Windows, key cannot be closed twice
3457 string subKeyName = Guid.NewGuid ().ToString ();
3460 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3461 createdKey.Close ();
3464 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3469 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3470 if (createdKey != null) {
3471 createdKey.Close ();
3472 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3480 public void bugnew4 () // Key cannot be flushed once it has been closed
3482 string subKeyName = Guid.NewGuid ().ToString ();
3485 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3486 createdKey.Close ();
3489 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3494 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3495 if (createdKey != null) {
3496 createdKey.Close ();
3497 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3504 private bool RunningOnUnix {
3506 int p = (int) Environment.OSVersion.Platform;
3507 return ((p == 4) || (p == 128) || (p == 6));