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 // This hangs on windows
2205 [Category ("NotWorking")]
2206 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
2208 // access to registry of remote machines is not implemented on unix
2213 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
2216 } catch (IOException ex) {
2217 // The network path was not found
2218 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2219 Assert.IsNotNull (ex.Message, "#3");
2220 Assert.IsNull (ex.InnerException, "#4");
2224 [Test] // bug #322839
2225 public void SetValue1_EntityReferences ()
2227 string subKeyName = Guid.NewGuid ().ToString ();
2230 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2231 // we created a new subkey, so value should not exist
2232 Assert.IsNull (createdKey.GetValue ("FirstName&\"<LastName>\""), "#A1");
2234 createdKey.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
2236 object name = createdKey.GetValue ("FirstName&\"<LastName>\"");
2237 // value should exist
2238 Assert.IsNotNull (name, "#A2");
2239 // type of value should be string
2240 Assert.AreEqual (typeof (string), name.GetType (), "#A3");
2241 // ensure value matches
2242 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#A4");
2244 // we created a new subkey, so value should not exist
2245 Assert.IsNull (createdKey.GetValue ("Info"), "#B1");
2247 createdKey.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" });
2249 object info = createdKey.GetValue ("Info");
2250 // value should exist
2251 Assert.IsNotNull (info, "#B2");
2252 // type of value should be string
2253 Assert.AreEqual (typeof (string []), info.GetType (), "#B3");
2254 // ensure value matches
2255 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#B4");
2258 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2259 object name = openedKey.GetValue ("FirstName&\"<LastName>\"");
2260 Assert.IsNotNull (name, "#C1");
2261 Assert.AreEqual (typeof (string), name.GetType (), "#C2");
2262 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#C3");
2264 object info = openedKey.GetValue ("Info");
2265 Assert.IsNotNull (info, "#D1");
2266 Assert.AreEqual (typeof (string []), info.GetType (), "#D2");
2267 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#D3");
2271 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2275 [Test] // SetValue (String, Object)
2276 public void SetValue1_Name_Null ()
2278 string subKeyName = Guid.NewGuid ().ToString ();
2280 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2282 createdKey.SetValue (null, "value1");
2283 string [] names = createdKey.GetValueNames ();
2284 Assert.IsNotNull (names, "#A1");
2285 Assert.AreEqual (1, names.Length, "#A2");
2286 Assert.IsNotNull (names [0], "#A3");
2287 Assert.AreEqual (string.Empty, names [0], "#A4");
2288 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2289 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2290 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2291 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2293 createdKey.SetValue (string.Empty, "value2");
2294 names = createdKey.GetValueNames ();
2295 Assert.IsNotNull (names, "#B1");
2296 Assert.AreEqual (1, names.Length, "#B2");
2297 Assert.IsNotNull (names [0], "#B3");
2298 Assert.AreEqual (string.Empty, names [0], "#B4");
2299 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2300 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2301 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2302 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2305 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2309 [Test] // SetValue (String, Object)
2310 public void SetValue1_Name_Empty ()
2312 string subKeyName = Guid.NewGuid ().ToString ();
2314 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2316 createdKey.SetValue (string.Empty, "value1");
2317 string [] names = createdKey.GetValueNames ();
2318 Assert.IsNotNull (names, "#A1");
2319 Assert.AreEqual (1, names.Length, "#A2");
2320 Assert.IsNotNull (names [0], "#A3");
2321 Assert.AreEqual (string.Empty, names [0], "#A4");
2322 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2323 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2324 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2325 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2327 createdKey.SetValue (null, "value2");
2328 names = createdKey.GetValueNames ();
2329 Assert.IsNotNull (names, "#B1");
2330 Assert.AreEqual (1, names.Length, "#B2");
2331 Assert.IsNotNull (names [0], "#B3");
2332 Assert.AreEqual (string.Empty, names [0], "#B4");
2333 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2334 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2335 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2336 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2339 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2343 [Test] // SetValue (String, Object)
2344 public void SetValue1_Name_MaxLength ()
2346 string subKeyName = Guid.NewGuid ().ToString ();
2349 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2350 string name = new string ('a', 254);
2352 createdKey.SetValue (name, "value1");
2353 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2354 createdKey.DeleteValue (name);
2355 Assert.IsNull (createdKey.GetValue (name), "#A2");
2357 name = new string ('a', 255);
2359 createdKey.SetValue (name, "value2");
2360 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2361 createdKey.DeleteValue (name);
2362 Assert.IsNull (createdKey.GetValue (name), "#B2");
2364 name = new string ('a', 256);
2367 createdKey.SetValue (name, "value2");
2368 Assert.Fail ("#C1");
2369 } catch (ArgumentException ex) {
2370 // 1.x: Registry subkeys should not be
2371 // greater than or equal to 255 characters
2373 // 2.x: Registry subkeys should not be
2374 // greater than 255 characters
2375 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2376 Assert.IsNull (ex.InnerException, "#C3");
2377 Assert.IsNotNull (ex.Message, "#C4");
2378 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2379 Assert.IsNull (ex.ParamName, "#C6");
2384 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2385 if (createdKey != null) {
2386 createdKey.Close ();
2387 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2394 [Test] // SetValue (String, Object)
2395 public void SetValue1_Value_Null ()
2397 string subKeyName = Guid.NewGuid ().ToString ();
2399 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2402 createdKey.SetValue ("Name", null);
2404 } catch (ArgumentNullException ex) {
2405 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2406 Assert.IsNull (ex.InnerException, "#3");
2407 Assert.IsNotNull (ex.Message, "#4");
2408 Assert.AreEqual ("value", ex.ParamName, "#5");
2412 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2416 [Test] // SetValue (String, Object)
2417 public void SetValue1_Boolean ()
2419 string subKeyName = Guid.NewGuid ().ToString ();
2422 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2423 // we created a new subkey, so value should not exist
2424 Assert.IsNull (createdKey.GetValue ("Installed"), "#A1");
2426 createdKey.SetValue ("Installed", true);
2428 object value = createdKey.GetValue ("Installed");
2429 // value should exist
2430 Assert.IsNotNull (value, "#A2");
2431 // type of value should be string
2432 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2433 // ensure value matches
2434 Assert.AreEqual (true.ToString (), value, "#A4");
2437 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2438 object value = openedKey.GetValue ("Installed");
2439 Assert.IsNotNull (value, "#B1");
2440 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2441 Assert.AreEqual (true.ToString (), value, "#B3");
2445 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2449 [Test] // SetValue (String, Object)
2450 public void SetValue1_Byte ()
2452 string subKeyName = Guid.NewGuid ().ToString ();
2455 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2456 // we created a new subkey, so value should not exist
2457 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2459 createdKey.SetValue ("Flags", (byte) 5);
2461 object value = createdKey.GetValue ("Flags");
2462 // value should exist
2463 Assert.IsNotNull (value, "#A2");
2464 // type of value should be string
2465 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2466 // ensure value matches
2467 Assert.AreEqual ("5", value, "#A4");
2470 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2471 object value = openedKey.GetValue ("Flags");
2472 Assert.IsNotNull (value, "#B1");
2473 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2474 Assert.AreEqual ("5", value, "#B3");
2478 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2482 [Test] // SetValue (String, Object)
2483 public void SetValue1_ByteArray ()
2485 string subKeyName = Guid.NewGuid ().ToString ();
2488 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2489 // we created a new subkey, so value should not exist
2490 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2492 createdKey.SetValue ("Flags", new byte [] { 1, 5 });
2494 object value = createdKey.GetValue ("Flags");
2495 // value should exist
2496 Assert.IsNotNull (value, "#A2");
2497 // type of value should be string
2498 Assert.AreEqual (typeof (byte []), value.GetType (), "#3");
2499 // ensure value matches
2500 Assert.AreEqual (new byte [] { 1, 5 }, value, "#4");
2503 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2504 object value = openedKey.GetValue ("Flags");
2505 Assert.IsNotNull (value, "#B1");
2506 Assert.AreEqual (typeof (byte []), value.GetType (), "#B2");
2507 Assert.AreEqual (new byte [] { 1, 5 }, value, "#B3");
2511 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2515 [Test] // SetValue (String, Object)
2516 public void SetValue1_DateTime ()
2518 string subKeyName = Guid.NewGuid ().ToString ();
2521 object rawValue = DateTime.Now;
2523 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2524 // we created a new subkey, so value should not exist
2525 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2527 createdKey.SetValue ("Path", rawValue);
2529 object value = createdKey.GetValue ("Path");
2530 // value should exist
2531 Assert.IsNotNull (value, "#A2");
2532 // type of value should be string
2533 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2534 // ensure value matches
2535 Assert.AreEqual (rawValue.ToString (), value, "#A4");
2538 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2539 object value = openedKey.GetValue ("Path");
2540 Assert.IsNotNull (value, "#B1");
2541 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2542 Assert.AreEqual (rawValue.ToString (), value, "#B3");
2546 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2551 public void SetValue_Int32 ()
2553 string subKeyName = Guid.NewGuid ().ToString ();
2556 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2557 // we created a new subkey, so value should not exist
2558 Assert.IsNull (createdKey.GetValue ("RefCount"), "#A1");
2560 createdKey.SetValue ("RefCount", 5);
2562 object value = createdKey.GetValue ("RefCount");
2563 // value should exist
2564 Assert.IsNotNull (value, "#A2");
2565 // type of value should be int
2566 Assert.AreEqual (typeof (int), value.GetType (), "#A3");
2567 // ensure value matches
2568 Assert.AreEqual (5, value, "#A4");
2571 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2572 object value = openedKey.GetValue ("RefCount");
2573 Assert.IsNotNull (value, "#B1");
2574 Assert.AreEqual (typeof (int), value.GetType (), "#B2");
2575 Assert.AreEqual (5, value, "#B3");
2579 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2583 [Test] // SetValue (String, Object)
2584 public void SetValue1_Int64 ()
2586 string subKeyName = Guid.NewGuid ().ToString ();
2589 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2590 // we created a new subkey, so value should not exist
2591 Assert.IsNull (createdKey.GetValue ("Ticks"), "#A1");
2593 createdKey.SetValue ("Ticks", 500L);
2595 object value = createdKey.GetValue ("Ticks");
2596 // value should exist
2597 Assert.IsNotNull (value, "#A2");
2598 // type of value should be string
2599 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2600 // ensure value matches
2601 Assert.AreEqual ("500", value, "#A4");
2604 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2605 object value = openedKey.GetValue ("Ticks");
2606 Assert.IsNotNull (value, "#B1");
2607 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2608 Assert.AreEqual ("500", value, "#B3");
2612 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2616 [Test] // SetValue (String, Object)
2617 public void SetValue1_String ()
2619 string subKeyName = Guid.NewGuid ().ToString ();
2622 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2623 // we created a new subkey, so value should not exist
2624 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2626 createdKey.SetValue ("Path", "/usr/lib/whatever");
2628 object path = createdKey.GetValue ("Path");
2629 // value should exist
2630 Assert.IsNotNull (path, "#A2");
2631 // type of value should be string
2632 Assert.AreEqual (typeof (string), path.GetType (), "#A3");
2633 // ensure value matches
2634 Assert.AreEqual ("/usr/lib/whatever", path, "#A4");
2637 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2638 object path = openedKey.GetValue ("Path");
2639 Assert.IsNotNull (path, "#B1");
2640 Assert.AreEqual (typeof (string), path.GetType (), "#B2");
2641 Assert.AreEqual ("/usr/lib/whatever", path, "#B3");
2645 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2649 [Test] // SetValue (String, Object)
2650 public void SetValue1_StringArray ()
2652 string subKeyName = Guid.NewGuid ().ToString ();
2655 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2656 // we created a new subkey, so value should not exist
2657 Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#A1");
2659 createdKey.SetValue ("DependsOnGroup", new string [] { "A", "B" });
2661 object value = createdKey.GetValue ("DependsOnGroup");
2662 // value should exist
2663 Assert.IsNotNull (value, "#A2");
2664 // type of value should be string
2665 Assert.AreEqual (typeof (string []), value.GetType (), "#A3");
2666 // ensure value matches
2667 Assert.AreEqual (new string [] { "A", "B" }, value, "#A4");
2670 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2671 object value = openedKey.GetValue ("DependsOnGroup");
2672 Assert.IsNotNull (value, "#B1");
2673 Assert.AreEqual (typeof (string []), value.GetType (), "#B2");
2674 Assert.AreEqual (new string [] { "A", "B" }, value, "#B3");
2678 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2682 [Test] // SetValue (String, Object)
2683 public void SetValue1_Key_ReadOnly ()
2685 string subKeyName = Guid.NewGuid ().ToString ();
2687 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2689 softwareKey.SetValue ("name1", "value1");
2691 } catch (UnauthorizedAccessException ex) {
2692 // Cannot write to the registry key
2693 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2694 Assert.IsNotNull (ex.Message, "#3");
2695 Assert.IsNull (ex.InnerException, "#4");
2699 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2701 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2704 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2706 createdKey.SetValue ("name1", "value1");
2708 } catch (UnauthorizedAccessException ex) {
2709 // Cannot write to the registry key
2710 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2711 Assert.IsNotNull (ex.Message, "#3");
2712 Assert.IsNull (ex.InnerException, "#4");
2717 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2718 if (createdKey != null) {
2719 createdKey.Close ();
2720 softwareKey.DeleteSubKeyTree (subKeyName);
2728 [Test] // SetValue (String, Object)
2729 public void SetValue1_Key_Removed ()
2731 string subKeyName = Guid.NewGuid ().ToString ();
2733 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2735 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2736 softwareKey.DeleteSubKeyTree (subKeyName);
2737 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2739 createdKey.SetValue ("name1", "value1");
2741 } catch (IOException ex) {
2742 // Illegal operation attempted on a registry key that
2743 // has been marked for deletion
2744 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2745 Assert.IsNotNull (ex.Message, "#4");
2746 Assert.IsNull (ex.InnerException, "#5");
2751 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2752 if (createdKey != null) {
2753 createdKey.Close ();
2754 softwareKey.DeleteSubKeyTree (subKeyName);
2762 [Test] // SetValue (String, Object, RegistryValueKind)
2763 public void SetValue2_Key_ReadOnly ()
2765 string subKeyName = Guid.NewGuid ().ToString ();
2767 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2769 softwareKey.SetValue ("name1", "value1",
2770 RegistryValueKind.String);
2772 } catch (UnauthorizedAccessException ex) {
2773 // Cannot write to the registry key
2774 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2775 Assert.IsNotNull (ex.Message, "#3");
2776 Assert.IsNull (ex.InnerException, "#4");
2780 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2782 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2785 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2787 createdKey.SetValue ("name1", "value1",
2788 RegistryValueKind.String);
2790 } catch (UnauthorizedAccessException ex) {
2791 // Cannot write to the registry key
2792 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2793 Assert.IsNotNull (ex.Message, "#3");
2794 Assert.IsNull (ex.InnerException, "#4");
2799 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2800 if (createdKey != null) {
2801 createdKey.Close ();
2802 softwareKey.DeleteSubKeyTree (subKeyName);
2810 [Test] // SetValue (String, Object, RegistryValueKind)
2811 public void SetValue2_Key_Removed ()
2813 string subKeyName = Guid.NewGuid ().ToString ();
2815 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2817 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2818 softwareKey.DeleteSubKeyTree (subKeyName);
2819 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2821 createdKey.SetValue ("name1", "value1",
2822 RegistryValueKind.String);
2824 } catch (IOException ex) {
2825 // Illegal operation attempted on a registry key that
2826 // has been marked for deletion
2827 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2828 Assert.IsNotNull (ex.Message, "#4");
2829 Assert.IsNull (ex.InnerException, "#5");
2834 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2835 if (createdKey != null) {
2836 createdKey.Close ();
2837 softwareKey.DeleteSubKeyTree (subKeyName);
2845 [Test] // SetValue (String, Object, RegistryValueKind)
2846 public void SetValue2_Name_Empty ()
2848 string subKeyName = Guid.NewGuid ().ToString ();
2850 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2852 createdKey.SetValue (string.Empty, "value1",
2853 RegistryValueKind.String);
2854 string [] names = createdKey.GetValueNames ();
2855 Assert.IsNotNull (names, "#A1");
2856 Assert.AreEqual (1, names.Length, "#A2");
2857 Assert.IsNotNull (names [0], "#A3");
2858 Assert.AreEqual (string.Empty, names [0], "#A4");
2859 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2860 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2861 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2862 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2864 createdKey.SetValue (null, "value2",
2865 RegistryValueKind.String);
2866 names = createdKey.GetValueNames ();
2867 Assert.IsNotNull (names, "#B1");
2868 Assert.AreEqual (1, names.Length, "#B2");
2869 Assert.IsNotNull (names [0], "#B3");
2870 Assert.AreEqual (string.Empty, names [0], "#B4");
2871 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2872 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2873 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2874 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2877 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2881 [Test] // SetValue (String, Object, RegistryValueKind)
2882 public void SetValue2_Name_MaxLength ()
2884 string subKeyName = Guid.NewGuid ().ToString ();
2887 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2888 string name = new string ('a', 254);
2890 createdKey.SetValue (name, "value1",
2891 RegistryValueKind.String);
2892 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2893 createdKey.DeleteValue (name);
2894 Assert.IsNull (createdKey.GetValue (name), "#A2");
2896 name = new string ('a', 255);
2898 createdKey.SetValue (name, "value2",
2899 RegistryValueKind.String);
2900 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2901 createdKey.DeleteValue (name);
2902 Assert.IsNull (createdKey.GetValue (name), "#B2");
2904 name = new string ('a', 256);
2907 createdKey.SetValue (name, "value2",
2908 RegistryValueKind.String);
2909 Assert.Fail ("#C1");
2910 } catch (ArgumentException ex) {
2911 // Registry subkeys should not be
2912 // greater than 255 characters
2913 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2914 Assert.IsNull (ex.InnerException, "#C3");
2915 Assert.IsNotNull (ex.Message, "#C4");
2916 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2917 Assert.IsNull (ex.ParamName, "#C6");
2922 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2923 if (createdKey != null) {
2924 createdKey.Close ();
2925 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2932 [Test] // SetValue (String, Object, RegistryValueKind)
2933 public void SetValue2_Name_Null ()
2935 string subKeyName = Guid.NewGuid ().ToString ();
2937 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2939 createdKey.SetValue (null, "value1",
2940 RegistryValueKind.String);
2941 string [] names = createdKey.GetValueNames ();
2942 Assert.IsNotNull (names, "#A1");
2943 Assert.AreEqual (1, names.Length, "#A2");
2944 Assert.IsNotNull (names [0], "#A3");
2945 Assert.AreEqual (string.Empty, names [0], "#A4");
2946 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2947 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2948 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2949 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2951 createdKey.SetValue (string.Empty, "value2",
2952 RegistryValueKind.String);
2953 names = createdKey.GetValueNames ();
2954 Assert.IsNotNull (names, "#B1");
2955 Assert.AreEqual (1, names.Length, "#B2");
2956 Assert.IsNotNull (names [0], "#B3");
2957 Assert.AreEqual (string.Empty, names [0], "#B4");
2958 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2959 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2960 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2961 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2964 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2968 [Test] // SetValue (String, Object, RegistryValueKind)
2969 public void SetValue2_Value_Null ()
2971 string subKeyName = Guid.NewGuid ().ToString ();
2973 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2976 createdKey.SetValue ("Name", null,
2977 RegistryValueKind.String);
2979 } catch (ArgumentNullException ex) {
2980 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2981 Assert.IsNull (ex.InnerException, "#3");
2982 Assert.IsNotNull (ex.Message, "#4");
2983 Assert.AreEqual ("value", ex.ParamName, "#5");
2987 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2992 public void SubKeyCount ()
2994 string subKeyName = Guid.NewGuid ().ToString ();
2997 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2998 // check if key was successfully created
2999 Assert.IsNotNull (createdKey, "#A1");
3000 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3003 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3004 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3007 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3009 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3010 Assert.IsNotNull (createdKey, "#B1");
3011 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3013 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3014 Assert.IsNotNull (createdKey2, "#B3");
3015 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
3016 createdKey2.DeleteSubKey ("monotemp1");
3017 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
3019 Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
3023 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3024 if (createdKey != null) {
3025 createdKey.Close ();
3026 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3034 public void SubKeyCount_Key_Removed ()
3036 string subKeyName = Guid.NewGuid ().ToString ();
3039 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3040 // check if key was successfully created
3041 Assert.IsNotNull (createdKey, "#A1");
3042 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3045 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3046 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3049 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3051 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3052 Assert.IsNotNull (createdKey, "#B1");
3053 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3055 // remove created key
3056 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3059 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
3060 } catch (IOException ex) {
3061 // Illegal operation attempted on a registry key that
3062 // has been marked for deletion
3063 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3064 Assert.IsNotNull (ex.Message, "#15");
3065 Assert.IsNull (ex.InnerException, "#16");
3070 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3071 if (createdKey != null) {
3072 createdKey.Close ();
3073 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3081 public void ValueCount ()
3083 string subKeyName = Guid.NewGuid ().ToString ();
3086 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3087 // check if key was successfully created
3088 Assert.IsNotNull (createdKey, "#A1");
3089 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3090 createdKey.SetValue ("name1", "value1");
3091 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3092 createdKey.SetValue ("name2", "value2");
3093 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3094 createdKey.SetValue ("name2", "value2b");
3095 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3096 createdKey.SetValue ("name3", "value3");
3097 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3099 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3100 Assert.IsNotNull (createdKey, "#B1");
3101 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3103 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3104 Assert.IsNotNull (createdKey2, "#B3");
3105 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
3106 createdKey2.DeleteValue ("name2");
3107 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
3109 Assert.AreEqual (2, createdKey.ValueCount, "#B6");
3113 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3114 if (createdKey != null) {
3115 createdKey.Close ();
3116 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3124 public void ValueCount_Key_Removed ()
3126 string subKeyName = Guid.NewGuid ().ToString ();
3129 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3130 // check if key was successfully created
3131 Assert.IsNotNull (createdKey, "#A1");
3132 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3133 createdKey.SetValue ("name1", "value1");
3134 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3135 createdKey.SetValue ("name2", "value2");
3136 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3137 createdKey.SetValue ("name2", "value2b");
3138 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3139 createdKey.SetValue ("name3", "value3");
3140 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3142 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3143 Assert.IsNotNull (createdKey, "#B1");
3144 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3146 // remove created key
3147 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3150 Assert.Fail ("#C1: " + createdKey.ValueCount);
3151 } catch (IOException ex) {
3152 // Illegal operation attempted on a registry key that
3153 // has been marked for deletion
3154 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3155 Assert.IsNotNull (ex.Message, "#15");
3156 Assert.IsNull (ex.InnerException, "#16");
3161 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3162 if (createdKey != null) {
3163 createdKey.Close ();
3164 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3172 public void bug79051 ()
3174 string subKeyName = Guid.NewGuid ().ToString ();
3176 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3178 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3179 createdKey.SetValue ("test", "whatever");
3180 createdKey.Close ();
3181 softwareKey.DeleteSubKeyTree (subKeyName);
3185 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3186 if (createdKey != null) {
3187 createdKey.Close ();
3188 softwareKey.DeleteSubKeyTree (subKeyName);
3198 public void TypeCastTests ()
3200 string subKeyName = Guid.NewGuid ().ToString ();
3201 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3203 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3204 createdKey.SetValue ("test-int", (int) 1, RegistryValueKind.DWord);
3205 createdKey.SetValue ("test-uint", (uint) 1, RegistryValueKind.DWord);
3206 createdKey.SetValue ("test-byte", (byte) 1, RegistryValueKind.DWord);
3207 createdKey.SetValue ("test-sbyte", (sbyte) 1, RegistryValueKind.DWord);
3208 createdKey.SetValue ("test-short", (short) 1, RegistryValueKind.DWord);
3209 createdKey.SetValue ("test-ushort", (ushort) 1, RegistryValueKind.DWord);
3210 createdKey.SetValue ("test-long", (long) 1, RegistryValueKind.DWord);
3211 createdKey.SetValue ("test-ulong", (ulong) 1, RegistryValueKind.DWord);
3212 createdKey.SetValue ("test-decimal", (decimal) 1, RegistryValueKind.DWord);
3213 createdKey.SetValue ("test-float", (float) 1, RegistryValueKind.DWord);
3214 createdKey.SetValue ("test-bool", true, RegistryValueKind.DWord);
3216 createdKey.SetValue ("dtest-int", (int) 1, RegistryValueKind.QWord);
3217 createdKey.SetValue ("dtest-uint", (uint) 1, RegistryValueKind.QWord);
3218 createdKey.SetValue ("dtest-byte", (byte) 1, RegistryValueKind.QWord);
3219 createdKey.SetValue ("dtest-sbyte", (sbyte) 1, RegistryValueKind.QWord);
3220 createdKey.SetValue ("dtest-short", (short) 1, RegistryValueKind.QWord);
3221 createdKey.SetValue ("dtest-ushort", (ushort) 1, RegistryValueKind.QWord);
3222 createdKey.SetValue ("dtest-long", (long) 1, RegistryValueKind.QWord);
3223 createdKey.SetValue ("dtest-ulong", (ulong) 1, RegistryValueKind.QWord);
3224 createdKey.SetValue ("dtest-decimal", (decimal) 1, RegistryValueKind.QWord);
3225 createdKey.SetValue ("dtest-float", (float) 1, RegistryValueKind.QWord);
3226 createdKey.SetValue ("dtest-bool", true, RegistryValueKind.QWord);
3228 object r = createdKey.GetValue ("test-int");
3229 Assert.AreEqual (r is int, true);
3230 Assert.AreEqual ((int) r, 1);
3232 r = createdKey.GetValue ("test-uint");
3233 Assert.AreEqual (r is int, true);
3234 Assert.AreEqual ((int) r, 1);
3235 r = createdKey.GetValue ("test-byte");
3236 Assert.AreEqual (r is int, true);
3237 Assert.AreEqual ((int) r, 1);
3238 r = createdKey.GetValue ("test-sbyte");
3239 Assert.AreEqual (r is int, true);
3240 Assert.AreEqual ((int) r, 1);
3241 r = createdKey.GetValue ("test-short");
3242 Assert.AreEqual (r is int, true);
3243 Assert.AreEqual ((int) r, 1);
3244 r = createdKey.GetValue ("test-ushort");
3245 Assert.AreEqual (r is int, true);
3246 Assert.AreEqual ((int) r, 1);
3247 r = createdKey.GetValue ("test-long");
3248 Assert.AreEqual (r is int, true);
3249 Assert.AreEqual ((int) r, 1);
3250 r = createdKey.GetValue ("test-ulong");
3251 Assert.AreEqual (r is int, true);
3252 Assert.AreEqual ((int) r, 1);
3254 r = createdKey.GetValue ("dtest-int");
3255 Assert.AreEqual (r is long, true);
3256 Assert.AreEqual ((long) r, 1);
3257 r = createdKey.GetValue ("dtest-uint");
3258 Assert.AreEqual (r is long, true);
3259 Assert.AreEqual ((long) r, 1);
3260 r = createdKey.GetValue ("dtest-byte");
3261 Assert.AreEqual (r is long, true);
3262 Assert.AreEqual ((long) r, 1);
3263 r = createdKey.GetValue ("dtest-sbyte");
3264 Assert.AreEqual (r is long, true);
3265 Assert.AreEqual ((long) r, 1);
3266 r = createdKey.GetValue ("dtest-short");
3267 Assert.AreEqual (r is long, true);
3268 Assert.AreEqual ((long) r, 1);
3269 r = createdKey.GetValue ("dtest-ushort");
3270 Assert.AreEqual (r is long, true);
3271 Assert.AreEqual ((long) r, 1);
3272 r = createdKey.GetValue ("dtest-long");
3273 Assert.AreEqual (r is long, true);
3274 Assert.AreEqual ((long) r, 1);
3275 r = createdKey.GetValue ("dtest-ulong");
3276 Assert.AreEqual (r is long, true);
3277 Assert.AreEqual ((long) r, 1);
3278 r = createdKey.GetValue ("dtest-decimal");
3279 Assert.IsTrue (r is long);
3280 Assert.AreEqual ((long) r, 1);
3281 r = createdKey.GetValue ("dtest-float");
3282 Assert.IsTrue (r is long);
3283 Assert.AreEqual ((long) r, 1);
3284 r = createdKey.GetValue ("dtest-bool");
3285 Assert.AreEqual (typeof (long), r.GetType ());
3288 createdKey.SetValue ("test-int", uint.MaxValue, RegistryValueKind.DWord);
3289 Assert.Fail ("#100");
3291 createdKey.SetValue ("test-int", ulong.MaxValue, RegistryValueKind.QWord);
3292 Assert.Fail ("#101");
3293 } catch (ArgumentException) {
3296 createdKey.Close ();
3297 softwareKey.DeleteSubKeyTree (subKeyName);
3301 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3302 if (createdKey != null) {
3303 createdKey.Close ();
3304 softwareKey.DeleteSubKeyTree (subKeyName);
3313 public void bug79059 ()
3315 string subKeyName = Guid.NewGuid ().ToString ();
3317 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3319 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3320 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
3322 createdKey.Close ();
3323 softwareKey.DeleteSubKeyTree (subKeyName);
3327 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3328 if (createdKey != null) {
3329 createdKey.Close ();
3330 softwareKey.DeleteSubKeyTree (subKeyName);
3339 public void bugnew1 ()
3341 string subKeyName = Guid.NewGuid ().ToString ();
3343 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3345 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3346 createdKey.SetValue ("name1", "value1");
3348 RegistryKey testKey = null;
3350 testKey = createdKey.OpenSubKey ("test", true);
3351 if (testKey == null)
3352 testKey = createdKey.CreateSubKey ("test");
3353 testKey.SetValue ("another", "one");
3355 if (testKey != null)
3359 createdKey.SetValue ("name2", "value2");
3360 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
3361 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
3362 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
3363 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
3365 string [] names = createdKey.GetValueNames ();
3366 Assert.IsNotNull (names, "#6");
3367 Assert.AreEqual (2, names.Length, "#7");
3368 Assert.AreEqual ("name1", names [0], "#8");
3369 Assert.AreEqual ("name2", names [1], "#9");
3371 softwareKey.DeleteSubKeyTree (subKeyName);
3373 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
3374 Assert.IsNull (openedKey, "#10");
3377 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
3378 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
3381 createdKey.GetValueNames ();
3382 Assert.Fail ("#13");
3383 } catch (IOException ex) {
3384 // Illegal operation attempted on a registry key that
3385 // has been marked for deletion
3386 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3387 Assert.IsNotNull (ex.Message, "#15");
3388 Assert.IsNull (ex.InnerException, "#16");
3392 createdKey.SetValue ("name1", "value1");
3393 Assert.Fail ("#17");
3394 } catch (IOException ex) {
3395 // Illegal operation attempted on a registry key that
3396 // has been marked for deletion
3397 Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
3398 Assert.IsNotNull (ex.Message, "#19");
3399 Assert.IsNull (ex.InnerException, "#20");
3403 createdKey.SetValue ("newname", "value1");
3404 Assert.Fail ("#21");
3405 } catch (IOException ex) {
3406 // Illegal operation attempted on a registry key that
3407 // has been marked for deletion
3408 Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
3409 Assert.IsNotNull (ex.Message, "#23");
3410 Assert.IsNull (ex.InnerException, "#24");
3413 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
3414 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
3415 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
3416 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
3419 createdKey.CreateSubKey ("new");
3420 Assert.Fail ("#29");
3421 } catch (IOException ex) {
3422 // Illegal operation attempted on a registry key that
3423 // has been marked for deletion
3424 Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
3425 Assert.IsNotNull (ex.Message, "#31");
3426 Assert.IsNull (ex.InnerException, "#32");
3431 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3432 if (createdKey != null) {
3433 createdKey.Close ();
3434 softwareKey.DeleteSubKeyTree (subKeyName);
3443 public void bugnew2 () // values cannot be written on registry root (hive)
3445 string [] names = Registry.CurrentUser.GetValueNames ();
3446 Assert.IsNotNull (names, "#1");
3447 Registry.CurrentUser.SetValue ("name1", "value1");
3448 Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
3449 Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
3450 string [] newNames = Registry.CurrentUser.GetValueNames ();
3451 Assert.IsNotNull (newNames, "#4");
3452 Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
3453 Registry.CurrentUser.DeleteValue ("name1");
3457 public void bugnew3 () // on Windows, key cannot be closed twice
3459 string subKeyName = Guid.NewGuid ().ToString ();
3462 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3463 createdKey.Close ();
3466 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3471 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3472 if (createdKey != null) {
3473 createdKey.Close ();
3474 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3482 public void bugnew4 () // Key cannot be flushed once it has been closed
3484 string subKeyName = Guid.NewGuid ().ToString ();
3487 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3488 createdKey.Close ();
3491 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3496 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3497 if (createdKey != null) {
3498 createdKey.Close ();
3499 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3506 private bool RunningOnUnix {
3508 int p = (int) Environment.OSVersion.Platform;
3509 return ((p == 4) || (p == 128) || (p == 6));