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)
16 using Microsoft.Win32;
18 using NUnit.Framework;
20 namespace MonoTests.Microsoft.Win32
23 public class RegistryKeyTest
25 private const string mimeroot = @"MIME\Database\Content Type";
28 [Category ("NotWorking")] // this will not work on Linux ever
29 public void TestGetValue ()
31 RegistryKey root = Registry.ClassesRoot;
34 key = root.OpenSubKey (mimeroot + @"\audio/wav");
35 Assert.AreEqual (".wav", key.GetValue ("Extension"), "GetValue #1");
36 key = root.OpenSubKey (mimeroot + @"\text/x-scriptlet");
37 Assert.AreEqual (null, key.GetValue ("Extension"), "GetValue #2");
41 public void TestHandle ()
43 // this test is for Windows only
47 // this regpath always exists under windows
48 RegistryKey k = Registry.CurrentUser
49 .OpenSubKey ("Software", false)
50 .OpenSubKey ("Microsoft", false)
51 .OpenSubKey ("Windows", false);
53 Assert.IsNotNull (k, "#01");
57 public void OpenSubKey ()
59 RegistryKey key = Registry.LocalMachine;
61 // HKEY_LOCAL_MACHINE\software should always exist on Windows
62 // and is automatically created on Linux
63 Assert.IsNotNull (key.OpenSubKey ("Software"), "#A1");
64 Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#A2");
66 key = Registry.CurrentUser;
68 // HKEY_CURRENT_USER\software should always exist on Windows
69 // and is automatically created on Linux
70 Assert.IsNotNull (key.OpenSubKey ("Software"), "#B1");
71 Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#B2");
75 // HKEY_USERS\software should not exist on Windows, and should not
76 // be created automatically on Linux
77 Assert.IsNull (key.OpenSubKey ("Software"), "#C1");
78 Assert.IsNull (key.OpenSubKey ("soFtware"), "#C2");
82 public void OpenSubKey_Key_DoesNotExist ()
84 string subKeyName = Guid.NewGuid ().ToString ();
85 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#1"); // read-only
86 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#2"); // writable
90 public void OpenSubKey_Key_Removed ()
92 string subKeyName = Guid.NewGuid ().ToString ();
95 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
96 // check if key was successfully created
97 Assert.IsNotNull (createdKey, "#1");
98 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
101 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
102 Assert.IsNotNull (createdKey, "#2");
103 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
104 Assert.IsNotNull (createdKey, "#3");
106 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
109 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
110 Assert.IsNull (createdKey.OpenSubKey ("monotemp"), "#5"); // read-only
112 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#6");
113 Assert.IsNull (createdKey.OpenSubKey ("monotemp", true), "#7");
117 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
118 if (createdKey != null) {
120 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
128 [Category ("NotWorking")] // MS should not allow this
129 public void OpenSubKey_Name_Empty ()
132 using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty)) {
133 Assert.IsNotNull (emptyKey, "#1");
136 using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty, true)) {
137 Assert.IsNotNull (emptyKey, "#1");
142 public void OpenSubKey_Name_MaxLength ()
144 string name = new string ('a', 254);
146 Assert.IsNull (Registry.CurrentUser.OpenSubKey (name), "#A1");
148 name = new string ('a', 255);
150 Assert.IsNull (Registry.CurrentUser.OpenSubKey (name), "#B1");
152 name = new string ('a', 256);
155 Registry.CurrentUser.OpenSubKey (name);
157 } catch (ArgumentException ex) {
158 // 1.x: Registry subkeys should not be
159 // greater than or equal to 255 characters
161 // 2.x: Registry subkeys should not be
162 // greater than 255 characters
163 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
164 Assert.IsNull (ex.InnerException, "#C3");
165 Assert.IsNotNull (ex.Message, "#c4");
166 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
167 Assert.IsNull (ex.ParamName, "#C6");
172 public void OpenSubKey_Name_Null ()
175 Registry.CurrentUser.OpenSubKey (null);
177 } catch (ArgumentNullException ex) {
178 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
179 Assert.IsNull (ex.InnerException, "#A3");
180 Assert.IsNotNull (ex.Message, "#A4");
181 Assert.AreEqual ("name", ex.ParamName, "#A5");
185 Registry.CurrentUser.OpenSubKey (null, true);
187 } catch (ArgumentNullException ex) {
188 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
189 Assert.IsNull (ex.InnerException, "#B3");
190 Assert.IsNotNull (ex.Message, "#B4");
191 Assert.AreEqual ("name", ex.ParamName, "#B5");
196 public void Close_Local_Hive ()
198 RegistryKey hive = Registry.CurrentUser;
201 Assert.IsNotNull (hive.GetSubKeyNames (), "#1");
202 Assert.IsNull (hive.GetValue ("doesnotexist"), "#2");
203 Assert.IsNotNull (hive.GetValueNames (), "#3");
204 Assert.IsNull (hive.OpenSubKey ("doesnotexist"), "#4");
205 Assert.IsNotNull (hive.SubKeyCount, "#5");
206 Assert.IsNotNull (hive.ToString (), "#6");
208 // closing key again does not have any effect
213 public void Close_Local_Key ()
215 RegistryKey key = Registry.CurrentUser.OpenSubKey ("SOFTWARE");
218 // closing a key twice does not have any effect
222 key.CreateSubKey ("a");
224 } catch (ObjectDisposedException) {
228 key.DeleteSubKey ("doesnotexist");
230 } catch (ObjectDisposedException) {
234 key.DeleteSubKeyTree ("doesnotexist");
236 } catch (ObjectDisposedException) {
240 key.DeleteValue ("doesnotexist");
242 } catch (ObjectDisposedException) {
245 // flushing a closed key does not have any effect
249 key.GetSubKeyNames ();
251 } catch (ObjectDisposedException) {
255 key.GetValue ("doesnotexist");
257 } catch (ObjectDisposedException) {
261 key.GetValueNames ();
263 } catch (ObjectDisposedException) {
267 key.OpenSubKey ("doesnotexist");
269 } catch (ObjectDisposedException) {
273 key.SetValue ("doesnotexist", "something");
275 } catch (ObjectDisposedException) {
279 int x = key.SubKeyCount;
280 Assert.Fail ("#10:" + x);
281 } catch (ObjectDisposedException) {
287 } catch (ObjectDisposedException) {
291 int x = key.ValueCount;
292 Assert.Fail ("#12:" + x);
293 } catch (ObjectDisposedException) {
298 public void Close_Remote_Hive ()
300 // access to registry of remote machines is not implemented on unix
304 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
305 RegistryHive.CurrentUser, Environment.MachineName);
308 // closing a remote hive twice does not have any effect
312 hive.CreateSubKey ("a");
314 } catch (ObjectDisposedException) {
318 hive.DeleteSubKey ("doesnotexist");
320 } catch (ObjectDisposedException) {
324 hive.DeleteSubKeyTree ("doesnotexist");
326 } catch (ObjectDisposedException) {
330 hive.DeleteValue ("doesnotexist");
332 } catch (ObjectDisposedException) {
335 // flushing a closed hive does not have any effect
339 hive.GetSubKeyNames ();
341 } catch (ObjectDisposedException) {
345 hive.GetValue ("doesnotexist");
347 } catch (ObjectDisposedException) {
351 hive.GetValueNames ();
353 } catch (ObjectDisposedException) {
357 hive.OpenSubKey ("doesnotexist");
359 } catch (ObjectDisposedException) {
363 hive.SetValue ("doesnotexist", "something");
365 } catch (ObjectDisposedException) {
369 int x = hive.SubKeyCount;
370 Assert.Fail ("#10:" + x);
371 } catch (ObjectDisposedException) {
377 } catch (ObjectDisposedException) {
381 int x = hive.ValueCount;
382 Assert.Fail ("#12:" + x);
383 } catch (ObjectDisposedException) {
388 public void Close_Remote_Key ()
390 // access to registry of remote machines is not implemented on unix
394 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
395 RegistryHive.CurrentUser, Environment.MachineName);
396 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
399 // closing a remote key twice does not have any effect
403 key.CreateSubKey ("a");
405 } catch (ObjectDisposedException) {
409 key.DeleteSubKey ("doesnotexist");
411 } catch (ObjectDisposedException) {
415 key.DeleteSubKeyTree ("doesnotexist");
417 } catch (ObjectDisposedException) {
421 key.DeleteValue ("doesnotexist");
423 } catch (ObjectDisposedException) {
426 // flushing a closed key does not have any effect
430 key.GetSubKeyNames ();
432 } catch (ObjectDisposedException) {
436 key.GetValue ("doesnotexist");
438 } catch (ObjectDisposedException) {
442 key.GetValueNames ();
444 } catch (ObjectDisposedException) {
448 key.OpenSubKey ("doesnotexist");
450 } catch (ObjectDisposedException) {
454 key.SetValue ("doesnotexist", "something");
456 } catch (ObjectDisposedException) {
460 int x = key.SubKeyCount;
461 Assert.Fail ("#10:" + x);
462 } catch (ObjectDisposedException) {
468 } catch (ObjectDisposedException) {
472 int x = key.ValueCount;
473 Assert.Fail ("#12:" + x);
474 } catch (ObjectDisposedException) {
481 public void CreateSubKey ()
483 string subKeyName = Guid.NewGuid ().ToString ();
486 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
487 // check if key was successfully created
488 Assert.IsNotNull (createdKey, "#A1");
489 // software subkey should not be created automatically
490 Assert.IsNull (createdKey.OpenSubKey ("software"), "#A2");
493 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
494 // check if key was successfully created
495 Assert.IsNotNull (createdKey, "#B1");
496 // software subkey should not be created automatically
497 Assert.IsNull (createdKey.OpenSubKey ("software"), "#B2");
501 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
504 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
506 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
507 // check if key was successfully created
508 Assert.IsNotNull (createdKey, "#C1");
509 // software subkey should not be created automatically
510 Assert.IsNull (softwareKey.OpenSubKey ("software"), "#C2");
513 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
514 // check if key was successfully created
515 Assert.IsNotNull (createdKey, "#D1");
516 // software subkey should not be created automatically
517 Assert.IsNull (softwareKey.OpenSubKey ("software"), "#D2");
521 softwareKey.DeleteSubKeyTree (subKeyName);
527 public void CreateSubKey_Key_ReadOnly ()
529 string subKeyName = Guid.NewGuid ().ToString ();
531 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
532 RegistryKey createdKey = null;
535 createdKey = softwareKey.CreateSubKey (subKeyName);
537 } catch (UnauthorizedAccessException ex) {
538 // Cannot write to the registry key
539 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
540 Assert.IsNotNull (ex.Message, "#3");
541 Assert.IsNull (ex.InnerException, "#4");
544 if (createdKey != null)
551 public void CreateSubKey_Key_Removed ()
553 string subKeyName = Guid.NewGuid ().ToString ();
555 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
557 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
558 softwareKey.DeleteSubKeyTree (subKeyName);
559 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
561 createdKey.CreateSubKey ("test");
563 } catch (IOException ex) {
564 // Illegal operation attempted on a registry key that
565 // has been marked for deletion
566 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
567 Assert.IsNotNull (ex.Message, "#4");
568 Assert.IsNull (ex.InnerException, "#5");
573 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
574 if (createdKey != null) {
576 softwareKey.DeleteSubKeyTree (subKeyName);
585 [Category ("NotWorking")] // MS should not allow this
586 public void CreateSubKey_Name_Empty ()
588 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
589 using (RegistryKey emptyKey = softwareKey.CreateSubKey (string.Empty)) {
590 Assert.IsNotNull (emptyKey, "#1");
591 emptyKey.SetValue ("name1", "value1");
597 public void CreateSubKey_Name_MaxLength ()
599 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
600 string subKeyName = new string ('a', 254);
603 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
604 Assert.IsNotNull (createdKey, "#A1");
605 Assert.IsNotNull (softwareKey.OpenSubKey (subKeyName), "#A2");
608 softwareKey.DeleteSubKeyTree (subKeyName);
611 subKeyName = new string ('a', 255);
614 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
615 Assert.IsNotNull (createdKey, "#B1");
616 Assert.IsNotNull (softwareKey.OpenSubKey (subKeyName), "#B2");
619 softwareKey.DeleteSubKey (subKeyName);
622 subKeyName = new string ('a', 256);
625 softwareKey.CreateSubKey (subKeyName);
627 } catch (ArgumentException ex) {
628 // 1.x: Registry subkeys should not be
629 // greater than or equal to 255 characters
631 // 2.x: Registry subkeys should not be
632 // greater than 255 characters
633 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
634 Assert.IsNull (ex.InnerException, "#C3");
635 Assert.IsNotNull (ex.Message, "#C4");
636 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
637 Assert.IsNull (ex.ParamName, "#C6");
643 public void CreateSubKey_Name_Null ()
645 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
647 softwareKey.CreateSubKey (null);
649 } catch (ArgumentNullException ex) {
650 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
651 Assert.IsNull (ex.InnerException, "#3");
652 Assert.IsNotNull (ex.Message, "#4");
653 Assert.AreEqual ("name", ex.ParamName, "#5");
659 // Unfortunately we can't test that the scenario where a volatile
660 // key is not alive after a reboot, but we can test other bits.
662 public void CreateSubKey_Volatile ()
664 RegistryKey key = null;
665 RegistryKey subkey = null;
666 string subKeyName = "VolatileKey";
669 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
670 subkey = key.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
673 key = Registry.CurrentUser.OpenSubKey (subKeyName);
674 subkey = key.OpenSubKey ("Child");
675 Assert.AreEqual (true, subkey != null, "#A1");
685 public void CreateSubKey_Volatile_Child ()
687 RegistryKey key = null;
688 RegistryKey subkey = null;
689 string subKeyName = "VolatileKey";
692 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
693 subkey = key.CreateSubKey ("Child"); // Non volatile child
694 Assert.Fail ("#Exc");
695 } catch (IOException) {
705 public void CreateSubKey_Volatile_Conflict ()
707 RegistryKey key = null;
708 RegistryKey key2 = null;
709 RegistryKey subkey = null;
710 string subKeyName = "VolatileKey";
714 // Create a volatile key and try to open it as a normal one
716 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
717 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
718 Assert.AreEqual (key.Name, key2.Name, "A0");
720 subkey = key2.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
721 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#A1");
722 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#A2");
729 // Create a non-volatile key and try to open it as a volatile one
731 subKeyName = "NonVolatileKey";
732 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
733 key2.SetValue ("Name", "Mono");
734 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
735 Assert.AreEqual (key.Name, key2.Name, "B0");
736 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#B1");
737 Assert.AreEqual ("Mono", key2.GetValue ("Name"), "#B2");
739 key.CreateSubKey ("Child");
740 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#B3");
741 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#B4");
744 // Close the non-volatile key and try to re-open it as a volatile one
748 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
749 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#C0");
750 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#C1");
762 public void DeleteSubKey_Volatile ()
764 RegistryKey key = null;
765 RegistryKey subkey = null;
766 string subKeyName = "VolatileKey";
769 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
770 key.CreateSubKey ("VolatileKeyChild", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
771 key.SetValue ("Name", "Mono");
774 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
776 key = Registry.CurrentUser.OpenSubKey (subKeyName);
777 Assert.AreEqual (null, key, "#A0");
786 // Define a normal key, and create a normal and a volatile key under it, and retrieve their names.
788 public void GetSubKeyNames_Volatile ()
790 RegistryKey key = null;
791 RegistryKey subkey = null;
792 string subKeyName = Guid.NewGuid ().ToString ();
793 string volChildKeyName = "volatilechildkey";
794 string childKeyName = "childkey";
797 key = Registry.CurrentUser.CreateSubKey (subKeyName);
798 key.CreateSubKey (volChildKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
799 key.CreateSubKey (childKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
802 key = Registry.CurrentUser.OpenSubKey (subKeyName);
803 string [] keyNames = key.GetSubKeyNames ();
805 // we can guarantee the order of the child keys, so we sort the two of them
806 Array.Sort (keyNames);
808 Assert.AreEqual (2, keyNames.Length, "#A0");
809 Assert.AreEqual (childKeyName, keyNames [0], "#A1");
810 Assert.AreEqual (volChildKeyName, keyNames [1], "#A2");
822 public void DeleteSubKey ()
824 string subKeyName = Guid.NewGuid ().ToString ();
827 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
828 // check if key was successfully created
829 Assert.IsNotNull (createdKey, "#1");
831 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
832 Assert.IsNotNull (createdKey, "#2");
833 Registry.CurrentUser.DeleteSubKey (subKeyName);
835 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
836 Assert.IsNull (createdKey, "#3");
840 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
841 if (createdKey != null) {
843 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
851 public void DeleteSubKey_Key_HasChildKeys ()
853 string subKeyName = Guid.NewGuid ().ToString ();
856 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
857 // check if key was successfully created
858 Assert.IsNotNull (createdKey, "#1");
859 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
863 Registry.CurrentUser.DeleteSubKey (subKeyName);
865 } catch (InvalidOperationException ex) {
866 // Registry key has subkeys and recursive removes are not
867 // supported by this method
868 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
869 Assert.IsNotNull (ex.Message, "#4");
870 Assert.IsNull (ex.InnerException, "#5");
874 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
875 if (createdKey != null) {
877 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
885 public void DeleteSubKey_Key_ReadOnly ()
887 string subKeyName = Guid.NewGuid ().ToString ();
890 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
891 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
895 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
897 softwareKey.DeleteSubKey (subKeyName);
899 } catch (UnauthorizedAccessException ex) {
900 // Cannot write to the registry key
901 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
902 Assert.IsNotNull (ex.Message, "#3");
903 Assert.IsNull (ex.InnerException, "#4");
908 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
909 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
910 if (createdKey != null) {
912 softwareKey.DeleteSubKeyTree (subKeyName);
921 public void DeleteSubKey_Key_DoesNotExist ()
923 string subKeyName = Guid.NewGuid ().ToString ();
926 Registry.CurrentUser.DeleteSubKey (subKeyName);
928 } catch (ArgumentException ex) {
929 // Cannot delete a subkey tree because the subkey does not exist
930 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
931 Assert.IsNull (ex.InnerException, "#A3");
932 Assert.IsNotNull (ex.Message, "#A4");
933 Assert.IsNull (ex.ParamName, "#A5");
937 Registry.CurrentUser.DeleteSubKey (subKeyName, true);
939 } catch (ArgumentException ex) {
940 // Cannot delete a subkey tree because the subkey does not exist
941 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
942 Assert.IsNull (ex.InnerException, "#B3");
943 Assert.IsNotNull (ex.Message, "#B4");
944 Assert.IsNull (ex.ParamName, "#B5");
947 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
951 public void DeleteSubKey_Key_Removed ()
953 string subKeyName = Guid.NewGuid ().ToString ();
956 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
957 // check if key was successfully created
958 Assert.IsNotNull (createdKey, "#1");
959 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
962 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
963 Assert.IsNotNull (createdKey, "#2");
964 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
965 Assert.IsNotNull (createdKey, "#3");
967 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
968 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
970 createdKey.DeleteSubKey ("monotemp");
972 } catch (ArgumentException ex) {
973 // Cannot delete a subkey tree because the subkey does
975 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
976 Assert.IsNull (ex.InnerException, "#7");
977 Assert.IsNotNull (ex.Message, "#8");
978 Assert.IsNull (ex.ParamName, "#9");
983 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
984 if (createdKey != null) {
986 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
994 [Category ("NotWorking")] // MS should not allow this
995 public void DeleteSubKey_Name_Empty ()
997 string subKeyName = Guid.NewGuid ().ToString ();
999 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1001 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1002 createdKey.DeleteSubKey (string.Empty);
1003 createdKey.Close ();
1005 createdKey = softwareKey.OpenSubKey (subKeyName);
1006 Assert.IsNull (createdKey, "#1");
1009 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1010 if (createdKey != null)
1011 createdKey.Close ();
1012 softwareKey.DeleteSubKeyTree (subKeyName);
1020 public void DeleteSubKey_Name_MaxLength ()
1022 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1023 string subKeyName = new string ('a', 254);
1025 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1026 createdKey.Close ();
1028 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1029 Assert.IsNotNull (createdKey, "#A1");
1031 softwareKey.DeleteSubKey (subKeyName);
1032 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1033 Assert.IsNull (createdKey, "#A2");
1036 subKeyName = new string ('a', 256);
1039 softwareKey.DeleteSubKey (subKeyName);
1040 Assert.Fail ("#B1");
1041 } catch (ArgumentException ex) {
1042 // 1.x: Registry subkeys should not be
1043 // greater than or equal to 255 characters
1045 // 2.x: Registry subkeys should not be
1046 // greater than 255 characters
1047 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1048 Assert.IsNull (ex.InnerException, "#B3");
1049 Assert.IsNotNull (ex.Message, "#B4");
1050 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1051 Assert.IsNull (ex.ParamName, "#B6");
1057 public void DeleteSubKey_Name_Null ()
1059 string subKeyName = Guid.NewGuid ().ToString ();
1061 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1063 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1065 createdKey.DeleteSubKey (null);
1067 } catch (ArgumentNullException ex) {
1068 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1069 Assert.IsNull (ex.InnerException, "#3");
1070 Assert.IsNotNull (ex.Message, "#4");
1071 Assert.AreEqual ("name", ex.ParamName, "#5");
1075 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1076 if (createdKey != null)
1077 createdKey.Close ();
1078 softwareKey.DeleteSubKeyTree (subKeyName);
1086 public void DeleteSubKeyTree ()
1089 // - remove key with subkeys
1090 // - remove key of which some subkeys are marked for deletion
1091 // - remove key with values
1095 public void DeleteSubKeyTree_Key_DoesNotExist ()
1097 // Cannot delete a subkey tree because the subkey does not exist
1098 string subKeyName = Guid.NewGuid ().ToString ();
1100 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1102 } catch (ArgumentException ex) {
1103 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1104 Assert.IsNull (ex.InnerException, "#3");
1105 Assert.IsNotNull (ex.Message, "#4");
1106 Assert.IsNull (ex.ParamName, "#5");
1112 public void DeleteSubKeyTree_Key_DoesNotExist_Overload ()
1114 // Cannot delete a subkey tree because the subkey does not exist
1115 string subKeyName = Guid.NewGuid ().ToString ();
1117 Registry.CurrentUser.DeleteSubKeyTree (subKeyName, true);
1119 } catch (ArgumentException ex) {
1120 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1121 Assert.IsNull (ex.InnerException, "#3");
1122 Assert.IsNotNull (ex.Message, "#4");
1123 Assert.IsNull (ex.ParamName, "#5");
1126 // It's enough to know this line is not throwing an exception.
1127 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
1132 public void DeleteSubKeyTree_Key_ReadOnly ()
1134 string subKeyName = Guid.NewGuid ().ToString ();
1137 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1138 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1139 createdKey.Close ();
1142 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
1144 softwareKey.DeleteSubKeyTree (subKeyName);
1146 } catch (UnauthorizedAccessException ex) {
1147 // Cannot write to the registry key
1148 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1149 Assert.IsNotNull (ex.Message, "#3");
1150 Assert.IsNull (ex.InnerException, "#4");
1155 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1156 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1157 if (createdKey != null)
1158 createdKey.Close ();
1159 softwareKey.DeleteSubKeyTree (subKeyName);
1167 public void DeleteSubKeyTree_Key_Removed ()
1169 string subKeyName = Guid.NewGuid ().ToString ();
1172 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1173 // check if key was successfully created
1174 Assert.IsNotNull (createdKey, "#1");
1175 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
1178 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1179 Assert.IsNotNull (createdKey, "#2");
1180 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
1181 Assert.IsNotNull (createdKey, "#3");
1183 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1184 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
1186 createdKey.DeleteSubKeyTree ("monotemp");
1188 } catch (ArgumentException ex) {
1189 // Cannot delete a subkey tree because the subkey does
1191 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
1192 Assert.IsNull (ex.InnerException, "#7");
1193 Assert.IsNotNull (ex.Message, "#8");
1194 Assert.IsNull (ex.ParamName, "#9");
1199 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1200 if (createdKey != null) {
1201 createdKey.Close ();
1202 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1210 [Category ("NotWorking")] // MS should not allow this
1211 public void DeleteSubKeyTree_Name_Empty ()
1213 string subKeyName = Guid.NewGuid ().ToString ();
1215 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1217 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1218 createdKey.DeleteSubKeyTree (string.Empty);
1219 createdKey.Close ();
1221 createdKey = softwareKey.OpenSubKey (subKeyName);
1222 Assert.IsNull (createdKey, "#1");
1225 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1226 if (createdKey != null)
1227 createdKey.Close ();
1228 softwareKey.DeleteSubKeyTree (subKeyName);
1236 public void DeleteSubKeyTree_Name_MaxLength ()
1238 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1239 string subKeyName = new string ('a', 254);
1241 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1242 createdKey.Close ();
1244 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1245 Assert.IsNotNull (createdKey, "#A1");
1247 softwareKey.DeleteSubKeyTree (subKeyName);
1248 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1249 Assert.IsNull (createdKey, "#A2");
1253 subKeyName = new string ('a', 255);
1255 subKeyName = new string ('a', 256);
1259 softwareKey.DeleteSubKeyTree (subKeyName);
1260 Assert.Fail ("#B1");
1261 } catch (ArgumentException ex) {
1262 // 1.x: Registry subkeys should not be
1263 // greater than or equal to 255 characters
1265 // 2.x: Registry subkeys should not be
1266 // greater than 255 characters
1267 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1268 Assert.IsNull (ex.InnerException, "#B3");
1269 Assert.IsNotNull (ex.Message, "#B4");
1270 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1271 Assert.IsNull (ex.ParamName, "#B6");
1277 public void DeleteSubKeyTree_Name_Null ()
1279 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1281 softwareKey.DeleteSubKeyTree (null);
1283 } catch (ArgumentNullException ex) {
1284 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1285 Assert.IsNull (ex.InnerException, "#3");
1286 Assert.IsNotNull (ex.Message, "#4");
1287 Assert.AreEqual ("name", ex.ParamName, "#5");
1293 public void DeleteValue ()
1295 string subKeyName = Guid.NewGuid ().ToString ();
1298 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1299 // check if key was successfully created
1300 Assert.IsNotNull (createdKey, "#A1");
1301 createdKey.SetValue ("name1", "value1");
1302 createdKey.SetValue ("name2", "value2");
1303 string [] names = createdKey.GetValueNames ();
1304 Assert.IsNotNull (names, "#A2");
1305 Assert.AreEqual (2, names.Length, "#A3");
1306 Assert.IsNotNull (names [0], "#A4");
1307 Assert.AreEqual ("name1", names [0], "#A5");
1308 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A6");
1309 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A7");
1310 Assert.AreEqual ("name2", names [1], "#A8");
1311 Assert.IsNotNull (createdKey.GetValue ("name2"), "#A9");
1312 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#A10");
1314 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1315 Assert.IsNotNull (createdKey, "#B1");
1316 createdKey.DeleteValue ("name1");
1317 string [] names = createdKey.GetValueNames ();
1318 Assert.IsNotNull (names, "#B2");
1319 Assert.AreEqual (1, names.Length, "#B3");
1320 Assert.IsNotNull (names [0], "#B4");
1321 Assert.AreEqual ("name2", names [0], "#B5");
1322 Assert.IsNotNull (createdKey.GetValue ("name2"), "#B6");
1323 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#B7");
1324 createdKey.DeleteValue (new string ('a', 400), false);
1326 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1327 string [] names = createdKey.GetValueNames ();
1328 Assert.IsNotNull (names, "#C1");
1329 Assert.AreEqual (1, names.Length, "#C2");
1330 Assert.IsNotNull (names [0], "#C3");
1331 Assert.AreEqual ("name2", names [0], "#C4");
1332 Assert.IsNotNull (createdKey.GetValue ("name2"), "#C5");
1333 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#C6");
1337 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1338 if (createdKey != null) {
1339 createdKey.Close ();
1340 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1348 public void DeleteValue_Key_ReadOnly ()
1350 string subKeyName = Guid.NewGuid ().ToString ();
1353 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1354 createdKey.SetValue ("name1", "value1");
1357 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1359 // deleting value that exists
1360 createdKey.DeleteValue ("name1");
1361 Assert.Fail ("#A1");
1362 } catch (UnauthorizedAccessException ex) {
1363 // Cannot write to the registry key
1364 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#A2");
1365 Assert.IsNotNull (ex.Message, "#A3");
1366 Assert.IsNull (ex.InnerException, "#A4");
1370 // deleting value that exists
1371 createdKey.DeleteValue ("name1", true);
1372 Assert.Fail ("#B1");
1373 } catch (UnauthorizedAccessException ex) {
1374 // Cannot write to the registry key
1375 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#B2");
1376 Assert.IsNotNull (ex.Message, "#B3");
1377 Assert.IsNull (ex.InnerException, "#B4");
1381 // deleting value that exists
1382 createdKey.DeleteValue ("name1", false);
1383 Assert.Fail ("#C1");
1384 } catch (UnauthorizedAccessException ex) {
1385 // Cannot write to the registry key
1386 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#C2");
1387 Assert.IsNotNull (ex.Message, "#C3");
1388 Assert.IsNull (ex.InnerException, "#C4");
1392 // deleting value that does not exist
1393 createdKey.DeleteValue ("name2");
1394 Assert.Fail ("#D1");
1395 } catch (UnauthorizedAccessException ex) {
1396 // Cannot write to the registry key
1397 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#D2");
1398 Assert.IsNotNull (ex.Message, "#D3");
1399 Assert.IsNull (ex.InnerException, "#D4");
1403 // deleting value that does not exist
1404 createdKey.DeleteValue ("name2", true);
1405 Assert.Fail ("#E1");
1406 } catch (UnauthorizedAccessException ex) {
1407 // Cannot write to the registry key
1408 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#E2");
1409 Assert.IsNotNull (ex.Message, "#E3");
1410 Assert.IsNull (ex.InnerException, "#E4");
1414 // deleting value that does not exist
1415 createdKey.DeleteValue ("name2", false);
1416 Assert.Fail ("#F1");
1417 } catch (UnauthorizedAccessException ex) {
1418 // Cannot write to the registry key
1419 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#F2");
1420 Assert.IsNotNull (ex.Message, "#F3");
1421 Assert.IsNull (ex.InnerException, "#F4");
1426 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1427 if (createdKey != null) {
1428 createdKey.Close ();
1429 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1437 public void DeleteValue_Key_Removed ()
1439 string subKeyName = Guid.NewGuid ().ToString ();
1442 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1443 // check if key was successfully created
1444 Assert.IsNotNull (createdKey, "#1");
1445 createdKey.SetValue ("name1", "value1");
1447 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1448 Assert.IsNotNull (createdKey, "#2");
1449 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1450 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#3");
1452 createdKey.DeleteValue ("name1");
1453 createdKey.DeleteValue ("name1", true);
1454 createdKey.DeleteValue ("name1", false);
1458 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1459 if (createdKey != null) {
1460 createdKey.Close ();
1461 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1469 public void DeleteValue_Value_DoesNotExist ()
1471 string subKeyName = Guid.NewGuid ().ToString ();
1474 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1475 // check if key was successfully created
1476 Assert.IsNotNull (createdKey, "#A1");
1477 createdKey.SetValue ("name1", "value1");
1480 createdKey.DeleteValue ("name2");
1481 Assert.Fail ("#B1");
1482 } catch (ArgumentException ex) {
1483 // No value exists with that name
1484 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1485 Assert.IsNull (ex.InnerException, "#B3");
1486 Assert.IsNotNull (ex.Message, "#B4");
1487 Assert.IsNull (ex.ParamName, "#B5");
1491 createdKey.DeleteValue ("name2", true);
1492 Assert.Fail ("#C1");
1493 } catch (ArgumentException ex) {
1494 // No value exists with that name
1495 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1496 Assert.IsNull (ex.InnerException, "#C3");
1497 Assert.IsNotNull (ex.Message, "#C4");
1498 Assert.IsNull (ex.ParamName, "#C5");
1501 createdKey.DeleteValue ("name2", false);
1505 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1506 if (createdKey != null) {
1507 createdKey.Close ();
1508 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1516 public void DeleteValue_Name_Empty ()
1518 string subKeyName = Guid.NewGuid ().ToString ();
1521 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1522 createdKey.SetValue ("name1", "value1");
1523 createdKey.SetValue (string.Empty, "value2");
1525 string [] names = createdKey.GetValueNames ();
1526 Assert.IsNotNull (names, "#A1");
1527 Assert.AreEqual (2, names.Length, "#A2");
1528 Assert.IsNotNull (names [0], "#A3");
1530 Assert.AreEqual ("name1", names [0], "#A4");
1532 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1533 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1534 Assert.IsNotNull (names [1], "#A7");
1536 Assert.AreEqual (string.Empty, names [1], "#A8");
1538 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A9");
1539 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#A10");
1541 createdKey.DeleteValue (string.Empty);
1543 names = createdKey.GetValueNames ();
1544 Assert.IsNotNull (names, "#B1");
1545 Assert.AreEqual (1, names.Length, "#B2");
1546 Assert.IsNotNull (names [0], "#B3");
1547 Assert.AreEqual ("name1", names [0], "#B4");
1548 Assert.IsNotNull (createdKey.GetValue ("name1"), "#B5");
1549 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#B6");
1552 createdKey.DeleteValue (string.Empty);
1553 Assert.Fail ("#C1");
1554 } catch (ArgumentException ex) {
1555 // No value exists with that name
1556 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1557 Assert.IsNull (ex.InnerException, "#C3");
1558 Assert.IsNotNull (ex.Message, "#C4");
1559 Assert.IsNull (ex.ParamName, "#C5");
1563 createdKey.DeleteValue (string.Empty, true);
1564 Assert.Fail ("#D1");
1565 } catch (ArgumentException ex) {
1566 // No value exists with that name
1567 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
1568 Assert.IsNull (ex.InnerException, "#D3");
1569 Assert.IsNotNull (ex.Message, "#D4");
1570 Assert.IsNull (ex.ParamName, "#D5");
1573 createdKey.DeleteValue (string.Empty, false);
1575 names = createdKey.GetValueNames ();
1576 Assert.IsNotNull (names, "#E1");
1577 Assert.AreEqual (1, names.Length, "#E2");
1578 Assert.IsNotNull (names [0], "#E3");
1579 Assert.AreEqual ("name1", names [0], "#E4");
1580 Assert.IsNotNull (createdKey.GetValue ("name1"), "#E5");
1581 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#E6");
1585 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1586 if (createdKey != null) {
1587 createdKey.Close ();
1588 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1596 public void DeleteValue_Name_Null ()
1598 string subKeyName = Guid.NewGuid ().ToString ();
1601 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1602 createdKey.SetValue ("name1", "value1");
1603 createdKey.SetValue (null, "value2");
1605 string [] names = createdKey.GetValueNames ();
1606 Assert.IsNotNull (names, "#A1");
1607 Assert.AreEqual (2, names.Length, "#A2");
1608 Assert.IsNotNull (names [0], "#A3");
1610 Assert.AreEqual ("name1", names [0], "#A4");
1612 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1613 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1614 Assert.IsNotNull (names [1], "#A7");
1616 Assert.AreEqual (string.Empty, names [1], "#A8");
1618 Assert.IsNotNull (createdKey.GetValue (null), "#A9");
1619 Assert.AreEqual ("value2", createdKey.GetValue (null), "#A10");
1622 createdKey.DeleteValue (null);
1623 Assert.Fail ("#B1");
1624 } catch (ArgumentNullException ex) {
1625 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1626 Assert.IsNull (ex.InnerException, "#B3");
1627 Assert.IsNotNull (ex.Message, "#B4");
1628 Assert.AreEqual ("name", ex.ParamName, "#B5");
1632 createdKey.DeleteValue (null, true);
1633 Assert.Fail ("#C1");
1634 } catch (ArgumentNullException ex) {
1635 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
1636 Assert.IsNull (ex.InnerException, "#C3");
1637 Assert.IsNotNull (ex.Message, "#C4");
1638 Assert.AreEqual ("name", ex.ParamName, "#C5");
1642 createdKey.DeleteValue (null, false);
1643 Assert.Fail ("#D1");
1644 } catch (ArgumentNullException ex) {
1645 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
1646 Assert.IsNull (ex.InnerException, "#D3");
1647 Assert.IsNotNull (ex.Message, "#D4");
1648 Assert.AreEqual ("name", ex.ParamName, "#D5");
1653 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1654 if (createdKey != null) {
1655 createdKey.Close ();
1656 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1664 public void GetValue ()
1666 string subKeyName = Guid.NewGuid ().ToString ();
1669 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1670 createdKey.SetValue ("name1", "value1");
1671 createdKey.SetValue ("name2", "value2");
1674 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1675 Assert.IsNotNull (createdKey.GetValue ("name1"), "#1");
1676 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#2");
1677 Assert.IsNotNull (createdKey.GetValue ("name2"), "#3");
1678 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#4");
1679 Assert.IsNull (createdKey.GetValue ("name3"), "#5");
1680 Assert.AreEqual ("value3", createdKey.GetValue ("name3", "value3"), "#6");
1681 Assert.IsNull (createdKey.GetValue ("name3", null), "#7");
1682 Assert.IsNull (createdKey.GetValue (new string ('a', 400)), "#8");
1686 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1687 if (createdKey != null) {
1688 createdKey.Close ();
1689 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1697 public void GetValue_Key_Removed ()
1699 string subKeyName = Guid.NewGuid ().ToString ();
1702 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1703 createdKey.SetValue ("name1", "value1");
1704 createdKey.SetValue ("name2", "value2");
1707 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1708 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1710 Assert.IsNull (createdKey.GetValue ("name1"), "#1");
1711 Assert.IsNotNull (createdKey.GetValue ("name1", "default"), "#2");
1712 Assert.AreEqual ("default", createdKey.GetValue ("name1", "default"), "#3");
1713 Assert.IsNull (createdKey.GetValue ("name3"), "#3");
1714 Assert.IsNotNull (createdKey.GetValue ("name3", "default"), "#4");
1715 Assert.AreEqual ("default", createdKey.GetValue ("name3", "default"), "#5");
1719 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1720 if (createdKey != null) {
1721 createdKey.Close ();
1722 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1730 public void GetValue_Name_Empty ()
1732 string subKeyName = Guid.NewGuid ().ToString ();
1735 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1736 createdKey.SetValue ("name1", "value1");
1737 createdKey.SetValue ("name2", "value2");
1739 Assert.IsNull (createdKey.GetValue (string.Empty), "#A1");
1740 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#A2");
1741 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#A3");
1742 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#A4");
1745 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1746 Assert.IsNull (createdKey.GetValue (string.Empty), "#B1");
1747 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#B2");
1748 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#B3");
1749 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#B4");
1752 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1753 createdKey.SetValue (string.Empty, "value1");
1754 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#C1");
1755 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#C2");
1756 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#C3");
1757 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#C4");
1760 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1761 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#D1");
1762 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#D2");
1763 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#D3");
1764 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#D4");
1768 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1769 if (createdKey != null) {
1770 createdKey.Close ();
1771 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1779 public void GetValue_Name_Null ()
1781 string subKeyName = Guid.NewGuid ().ToString ();
1784 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1785 createdKey.SetValue ("name1", "value1");
1786 createdKey.SetValue ("name2", "value2");
1788 Assert.IsNull (createdKey.GetValue (null), "#A1");
1789 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#A2");
1790 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#A3");
1791 Assert.IsNull (createdKey.GetValue (null, null), "#A4");
1794 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1795 Assert.IsNull (createdKey.GetValue (null), "#B1");
1796 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#B2");
1797 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#B3");
1798 Assert.IsNull (createdKey.GetValue (null, null), "#B4");
1801 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1802 createdKey.SetValue (string.Empty, "value1");
1803 Assert.IsNotNull (createdKey.GetValue (null), "#C1");
1804 Assert.AreEqual ("value1", createdKey.GetValue (null), "#C2");
1805 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#C3");
1806 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#C4");
1809 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1810 Assert.IsNotNull (createdKey.GetValue (null), "#D1");
1811 Assert.AreEqual ("value1", createdKey.GetValue (null), "#D2");
1812 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#D3");
1813 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#D4");
1817 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1818 if (createdKey != null) {
1819 createdKey.Close ();
1820 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1828 public void GetValue_Expand ()
1830 string subKeyName = Guid.NewGuid ().ToString ();
1833 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1834 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1835 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1837 createdKey.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1838 RegistryValueKind.ExpandString);
1839 createdKey.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1840 createdKey.SetValue ("name3", "just some text",
1841 RegistryValueKind.ExpandString);
1843 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#A1");
1844 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#A2");
1845 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#A3");
1846 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1847 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A4");
1848 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1849 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A5");
1850 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1851 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A6");
1852 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1853 null, RegistryValueOptions.None), "#A7");
1854 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1855 null, RegistryValueOptions.None), "#A8");
1856 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1857 null, RegistryValueOptions.None), "#A9");
1859 Environment.SetEnvironmentVariable ("MONO_TEST1", "789");
1860 Environment.SetEnvironmentVariable ("MONO_TEST2", "666");
1862 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#B1");
1863 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#B2");
1864 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#B3");
1865 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1866 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B4");
1867 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1868 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B5");
1869 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1870 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B6");
1871 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1872 null, RegistryValueOptions.None), "#B7");
1873 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1874 null, RegistryValueOptions.None), "#B8");
1875 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1876 null, RegistryValueOptions.None), "#B9");
1878 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1879 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#C1");
1880 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#C2");
1881 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#C3");
1882 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1883 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C4");
1884 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1885 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C5");
1886 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1887 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C6");
1888 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1889 null, RegistryValueOptions.None), "#C7");
1890 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1891 null, RegistryValueOptions.None), "#C8");
1892 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1893 null, RegistryValueOptions.None), "#C9");
1895 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1896 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1898 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#D1");
1899 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#D2");
1900 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#D3");
1901 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1902 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D4");
1903 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1904 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D5");
1905 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1906 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D6");
1907 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1908 null, RegistryValueOptions.None), "#D7");
1909 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1910 null, RegistryValueOptions.None), "#D8");
1911 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1912 null, RegistryValueOptions.None), "#D9");
1916 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1917 if (createdKey != null) {
1918 createdKey.Close ();
1919 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1927 public void GetValueNames ()
1929 string subKeyName = Guid.NewGuid ().ToString ();
1932 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1933 string [] names = createdKey.GetValueNames ();
1934 Assert.IsNotNull (names, "#A1");
1935 Assert.AreEqual (0, names.Length, "#A2");
1937 createdKey.SetValue ("name1", "value1");
1938 createdKey.SetValue ("name2", "value2");
1939 createdKey.SetValue ("namelong", "value3");
1940 createdKey.SetValue ("name3", "value4");
1942 Assert.AreEqual (4, createdKey.ValueCount, "#B1");
1943 names = createdKey.GetValueNames ();
1944 Assert.IsNotNull (names, "#B2");
1945 Assert.AreEqual (4, names.Length, "#B3");
1948 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1949 string [] names = createdKey.GetValueNames ();
1950 Assert.IsNotNull (names, "#C1");
1951 Assert.AreEqual (4, names.Length, "#C2");
1953 // Mono's Unix registry API uses a hashtable to store the
1954 // values (and their names), so names are not returned in
1957 // to test whether the names returned by GetValueNames
1958 // match what we expect, we use these names to remove the
1959 // the values from the created keys and such we should end
1960 // up with zero values
1961 for (int i = 0; i < names.Length; i++) {
1962 string valueName = names [i];
1963 createdKey.DeleteValue (valueName);
1966 // all values should be removed now
1967 Assert.AreEqual (0, createdKey.ValueCount, "#C3");
1971 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1972 if (createdKey != null) {
1973 createdKey.Close ();
1974 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1982 public void GetValueNames_Key_Removed ()
1984 string subKeyName = Guid.NewGuid ().ToString ();
1987 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1988 createdKey.SetValue ("name1", "value1");
1989 createdKey.SetValue ("name2", "value2");
1991 string [] names = createdKey.GetValueNames ();
1992 Assert.IsNotNull (names, "#A1");
1993 Assert.AreEqual (2, names.Length, "#A2");
1996 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1997 string [] names = createdKey.GetValueNames ();
1998 Assert.IsNotNull (names, "#B1");
1999 Assert.AreEqual (2, names.Length, "#B2");
2001 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2004 createdKey.GetValueNames ();
2005 Assert.Fail ("#C1");
2006 } catch (IOException ex) {
2007 // Illegal operation attempted on a registry key that
2008 // has been marked for deletion
2009 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
2010 Assert.IsNotNull (ex.Message, "#C3");
2011 Assert.IsNull (ex.InnerException, "#C4");
2016 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2017 if (createdKey != null) {
2018 createdKey.Close ();
2019 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2026 [Test] // bug #78519
2027 public void GetSubKeyNamesTest ()
2029 string subKeyName = Guid.NewGuid ().ToString ();
2031 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2033 // check if key was successfully created
2034 Assert.IsNotNull (createdKey, "#A");
2036 RegistryKey subKey = createdKey.CreateSubKey ("foo");
2037 Assert.IsNotNull (subKey, "#B1");
2038 Assert.AreEqual (1, createdKey.SubKeyCount, "#B2");
2039 string[] subKeyNames = createdKey.GetSubKeyNames ();
2040 Assert.IsNotNull (subKeyNames, "#B3");
2041 Assert.AreEqual (1, subKeyNames.Length, "#B4");
2042 Assert.AreEqual ("foo", subKeyNames[0], "#B5");
2044 subKey = createdKey.CreateSubKey ("longfoo");
2045 Assert.IsNotNull (subKey, "#C1");
2046 Assert.AreEqual (2, createdKey.SubKeyCount, "#C2");
2047 subKeyNames = createdKey.GetSubKeyNames ();
2048 Assert.IsNotNull (subKeyNames, "#C3");
2049 Assert.AreEqual (2, subKeyNames.Length, "#C4");
2050 Assert.AreEqual ("foo", subKeyNames [0], "#C5");
2051 Assert.AreEqual ("longfoo", subKeyNames [1], "#C6");
2053 subKey = createdKey.CreateSubKey ("sfoo");
2054 Assert.IsNotNull (subKey, "#D1");
2055 Assert.AreEqual (3, createdKey.SubKeyCount, "#D2");
2056 subKeyNames = createdKey.GetSubKeyNames ();
2057 Assert.IsNotNull (subKeyNames, "#D3");
2058 Assert.AreEqual (3, subKeyNames.Length, "#D4");
2059 Assert.AreEqual ("foo", subKeyNames [0], "#D5");
2060 Assert.AreEqual ("longfoo", subKeyNames [1], "#D6");
2061 Assert.AreEqual ("sfoo", subKeyNames [2], "#D7");
2063 foreach (string name in subKeyNames) {
2064 createdKey.DeleteSubKeyTree (name);
2066 Assert.AreEqual (0, createdKey.SubKeyCount, "#E");
2069 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2074 public void OpenRemoteBaseKey ()
2076 // access to registry of remote machines is not implemented on unix
2080 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
2081 RegistryHive.CurrentUser, Environment.MachineName);
2082 Assert.IsNotNull (hive, "#1");
2084 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
2085 Assert.IsNotNull (key, "#2");
2092 public void OpenRemoteBaseKey_MachineName_Null ()
2095 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser, null);
2097 } catch (ArgumentNullException ex) {
2098 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2099 Assert.IsNull (ex.InnerException, "#3");
2100 Assert.IsNotNull (ex.Message, "#4");
2101 Assert.AreEqual ("machineName", ex.ParamName, "#5");
2106 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
2108 // access to registry of remote machines is not implemented on unix
2113 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
2116 } catch (IOException ex) {
2117 // The network path was not found
2118 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2119 Assert.IsNotNull (ex.Message, "#3");
2120 Assert.IsNull (ex.InnerException, "#4");
2124 [Test] // bug #322839
2125 public void SetValue1_EntityReferences ()
2127 string subKeyName = Guid.NewGuid ().ToString ();
2130 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2131 // we created a new subkey, so value should not exist
2132 Assert.IsNull (createdKey.GetValue ("FirstName&\"<LastName>\""), "#A1");
2134 createdKey.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
2136 object name = createdKey.GetValue ("FirstName&\"<LastName>\"");
2137 // value should exist
2138 Assert.IsNotNull (name, "#A2");
2139 // type of value should be string
2140 Assert.AreEqual (typeof (string), name.GetType (), "#A3");
2141 // ensure value matches
2142 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#A4");
2144 // we created a new subkey, so value should not exist
2145 Assert.IsNull (createdKey.GetValue ("Info"), "#B1");
2147 createdKey.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" });
2149 object info = createdKey.GetValue ("Info");
2150 // value should exist
2151 Assert.IsNotNull (info, "#B2");
2152 // type of value should be string
2153 Assert.AreEqual (typeof (string []), info.GetType (), "#B3");
2154 // ensure value matches
2155 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#B4");
2158 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2159 object name = openedKey.GetValue ("FirstName&\"<LastName>\"");
2160 Assert.IsNotNull (name, "#C1");
2161 Assert.AreEqual (typeof (string), name.GetType (), "#C2");
2162 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#C3");
2164 object info = openedKey.GetValue ("Info");
2165 Assert.IsNotNull (info, "#D1");
2166 Assert.AreEqual (typeof (string []), info.GetType (), "#D2");
2167 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#D3");
2171 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2175 [Test] // SetValue (String, Object)
2176 public void SetValue1_Name_Null ()
2178 string subKeyName = Guid.NewGuid ().ToString ();
2180 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2182 createdKey.SetValue (null, "value1");
2183 string [] names = createdKey.GetValueNames ();
2184 Assert.IsNotNull (names, "#A1");
2185 Assert.AreEqual (1, names.Length, "#A2");
2186 Assert.IsNotNull (names [0], "#A3");
2187 Assert.AreEqual (string.Empty, names [0], "#A4");
2188 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2189 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2190 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2191 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2193 createdKey.SetValue (string.Empty, "value2");
2194 names = createdKey.GetValueNames ();
2195 Assert.IsNotNull (names, "#B1");
2196 Assert.AreEqual (1, names.Length, "#B2");
2197 Assert.IsNotNull (names [0], "#B3");
2198 Assert.AreEqual (string.Empty, names [0], "#B4");
2199 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2200 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2201 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2202 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2205 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2209 [Test] // SetValue (String, Object)
2210 public void SetValue1_Name_Empty ()
2212 string subKeyName = Guid.NewGuid ().ToString ();
2214 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2216 createdKey.SetValue (string.Empty, "value1");
2217 string [] names = createdKey.GetValueNames ();
2218 Assert.IsNotNull (names, "#A1");
2219 Assert.AreEqual (1, names.Length, "#A2");
2220 Assert.IsNotNull (names [0], "#A3");
2221 Assert.AreEqual (string.Empty, names [0], "#A4");
2222 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2223 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2224 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2225 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2227 createdKey.SetValue (null, "value2");
2228 names = createdKey.GetValueNames ();
2229 Assert.IsNotNull (names, "#B1");
2230 Assert.AreEqual (1, names.Length, "#B2");
2231 Assert.IsNotNull (names [0], "#B3");
2232 Assert.AreEqual (string.Empty, names [0], "#B4");
2233 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2234 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2235 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2236 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2239 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2243 [Test] // SetValue (String, Object)
2244 public void SetValue1_Name_MaxLength ()
2246 string subKeyName = Guid.NewGuid ().ToString ();
2249 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2250 string name = new string ('a', 254);
2252 createdKey.SetValue (name, "value1");
2253 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2254 createdKey.DeleteValue (name);
2255 Assert.IsNull (createdKey.GetValue (name), "#A2");
2257 name = new string ('a', 255);
2259 createdKey.SetValue (name, "value2");
2260 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2261 createdKey.DeleteValue (name);
2262 Assert.IsNull (createdKey.GetValue (name), "#B2");
2264 name = new string ('a', 256);
2267 createdKey.SetValue (name, "value2");
2268 Assert.Fail ("#C1");
2269 } catch (ArgumentException ex) {
2270 // 1.x: Registry subkeys should not be
2271 // greater than or equal to 255 characters
2273 // 2.x: Registry subkeys should not be
2274 // greater than 255 characters
2275 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2276 Assert.IsNull (ex.InnerException, "#C3");
2277 Assert.IsNotNull (ex.Message, "#C4");
2278 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2279 Assert.IsNull (ex.ParamName, "#C6");
2284 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2285 if (createdKey != null) {
2286 createdKey.Close ();
2287 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2294 [Test] // SetValue (String, Object)
2295 public void SetValue1_Value_Null ()
2297 string subKeyName = Guid.NewGuid ().ToString ();
2299 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2302 createdKey.SetValue ("Name", null);
2304 } catch (ArgumentNullException ex) {
2305 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2306 Assert.IsNull (ex.InnerException, "#3");
2307 Assert.IsNotNull (ex.Message, "#4");
2308 Assert.AreEqual ("value", ex.ParamName, "#5");
2312 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2316 [Test] // SetValue (String, Object)
2317 public void SetValue1_Boolean ()
2319 string subKeyName = Guid.NewGuid ().ToString ();
2322 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2323 // we created a new subkey, so value should not exist
2324 Assert.IsNull (createdKey.GetValue ("Installed"), "#A1");
2326 createdKey.SetValue ("Installed", true);
2328 object value = createdKey.GetValue ("Installed");
2329 // value should exist
2330 Assert.IsNotNull (value, "#A2");
2331 // type of value should be string
2332 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2333 // ensure value matches
2334 Assert.AreEqual (true.ToString (), value, "#A4");
2337 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2338 object value = openedKey.GetValue ("Installed");
2339 Assert.IsNotNull (value, "#B1");
2340 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2341 Assert.AreEqual (true.ToString (), value, "#B3");
2345 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2349 [Test] // SetValue (String, Object)
2350 public void SetValue1_Byte ()
2352 string subKeyName = Guid.NewGuid ().ToString ();
2355 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2356 // we created a new subkey, so value should not exist
2357 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2359 createdKey.SetValue ("Flags", (byte) 5);
2361 object value = createdKey.GetValue ("Flags");
2362 // value should exist
2363 Assert.IsNotNull (value, "#A2");
2364 // type of value should be string
2365 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2366 // ensure value matches
2367 Assert.AreEqual ("5", value, "#A4");
2370 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2371 object value = openedKey.GetValue ("Flags");
2372 Assert.IsNotNull (value, "#B1");
2373 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2374 Assert.AreEqual ("5", value, "#B3");
2378 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2382 [Test] // SetValue (String, Object)
2383 public void SetValue1_ByteArray ()
2385 string subKeyName = Guid.NewGuid ().ToString ();
2388 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2389 // we created a new subkey, so value should not exist
2390 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2392 createdKey.SetValue ("Flags", new byte [] { 1, 5 });
2394 object value = createdKey.GetValue ("Flags");
2395 // value should exist
2396 Assert.IsNotNull (value, "#A2");
2397 // type of value should be string
2398 Assert.AreEqual (typeof (byte []), value.GetType (), "#3");
2399 // ensure value matches
2400 Assert.AreEqual (new byte [] { 1, 5 }, value, "#4");
2403 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2404 object value = openedKey.GetValue ("Flags");
2405 Assert.IsNotNull (value, "#B1");
2406 Assert.AreEqual (typeof (byte []), value.GetType (), "#B2");
2407 Assert.AreEqual (new byte [] { 1, 5 }, value, "#B3");
2411 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2415 [Test] // SetValue (String, Object)
2416 public void SetValue1_DateTime ()
2418 string subKeyName = Guid.NewGuid ().ToString ();
2421 object rawValue = DateTime.Now;
2423 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2424 // we created a new subkey, so value should not exist
2425 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2427 createdKey.SetValue ("Path", rawValue);
2429 object value = createdKey.GetValue ("Path");
2430 // value should exist
2431 Assert.IsNotNull (value, "#A2");
2432 // type of value should be string
2433 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2434 // ensure value matches
2435 Assert.AreEqual (rawValue.ToString (), value, "#A4");
2438 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2439 object value = openedKey.GetValue ("Path");
2440 Assert.IsNotNull (value, "#B1");
2441 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2442 Assert.AreEqual (rawValue.ToString (), value, "#B3");
2446 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2451 public void SetValue_Int32 ()
2453 string subKeyName = Guid.NewGuid ().ToString ();
2456 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2457 // we created a new subkey, so value should not exist
2458 Assert.IsNull (createdKey.GetValue ("RefCount"), "#A1");
2460 createdKey.SetValue ("RefCount", 5);
2462 object value = createdKey.GetValue ("RefCount");
2463 // value should exist
2464 Assert.IsNotNull (value, "#A2");
2465 // type of value should be int
2466 Assert.AreEqual (typeof (int), value.GetType (), "#A3");
2467 // ensure value matches
2468 Assert.AreEqual (5, value, "#A4");
2471 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2472 object value = openedKey.GetValue ("RefCount");
2473 Assert.IsNotNull (value, "#B1");
2474 Assert.AreEqual (typeof (int), value.GetType (), "#B2");
2475 Assert.AreEqual (5, value, "#B3");
2479 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2483 [Test] // SetValue (String, Object)
2484 public void SetValue1_Int64 ()
2486 string subKeyName = Guid.NewGuid ().ToString ();
2489 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2490 // we created a new subkey, so value should not exist
2491 Assert.IsNull (createdKey.GetValue ("Ticks"), "#A1");
2493 createdKey.SetValue ("Ticks", 500L);
2495 object value = createdKey.GetValue ("Ticks");
2496 // value should exist
2497 Assert.IsNotNull (value, "#A2");
2498 // type of value should be string
2499 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2500 // ensure value matches
2501 Assert.AreEqual ("500", value, "#A4");
2504 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2505 object value = openedKey.GetValue ("Ticks");
2506 Assert.IsNotNull (value, "#B1");
2507 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2508 Assert.AreEqual ("500", value, "#B3");
2512 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2516 [Test] // SetValue (String, Object)
2517 public void SetValue1_String ()
2519 string subKeyName = Guid.NewGuid ().ToString ();
2522 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2523 // we created a new subkey, so value should not exist
2524 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2526 createdKey.SetValue ("Path", "/usr/lib/whatever");
2528 object path = createdKey.GetValue ("Path");
2529 // value should exist
2530 Assert.IsNotNull (path, "#A2");
2531 // type of value should be string
2532 Assert.AreEqual (typeof (string), path.GetType (), "#A3");
2533 // ensure value matches
2534 Assert.AreEqual ("/usr/lib/whatever", path, "#A4");
2537 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2538 object path = openedKey.GetValue ("Path");
2539 Assert.IsNotNull (path, "#B1");
2540 Assert.AreEqual (typeof (string), path.GetType (), "#B2");
2541 Assert.AreEqual ("/usr/lib/whatever", path, "#B3");
2545 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2549 [Test] // SetValue (String, Object)
2550 public void SetValue1_StringArray ()
2552 string subKeyName = Guid.NewGuid ().ToString ();
2555 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2556 // we created a new subkey, so value should not exist
2557 Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#A1");
2559 createdKey.SetValue ("DependsOnGroup", new string [] { "A", "B" });
2561 object value = createdKey.GetValue ("DependsOnGroup");
2562 // value should exist
2563 Assert.IsNotNull (value, "#A2");
2564 // type of value should be string
2565 Assert.AreEqual (typeof (string []), value.GetType (), "#A3");
2566 // ensure value matches
2567 Assert.AreEqual (new string [] { "A", "B" }, value, "#A4");
2570 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2571 object value = openedKey.GetValue ("DependsOnGroup");
2572 Assert.IsNotNull (value, "#B1");
2573 Assert.AreEqual (typeof (string []), value.GetType (), "#B2");
2574 Assert.AreEqual (new string [] { "A", "B" }, value, "#B3");
2578 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2582 [Test] // SetValue (String, Object)
2583 public void SetValue1_Key_ReadOnly ()
2585 string subKeyName = Guid.NewGuid ().ToString ();
2587 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2589 softwareKey.SetValue ("name1", "value1");
2591 } catch (UnauthorizedAccessException ex) {
2592 // Cannot write to the registry key
2593 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2594 Assert.IsNotNull (ex.Message, "#3");
2595 Assert.IsNull (ex.InnerException, "#4");
2599 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2601 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2604 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2606 createdKey.SetValue ("name1", "value1");
2608 } catch (UnauthorizedAccessException ex) {
2609 // Cannot write to the registry key
2610 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2611 Assert.IsNotNull (ex.Message, "#3");
2612 Assert.IsNull (ex.InnerException, "#4");
2617 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2618 if (createdKey != null) {
2619 createdKey.Close ();
2620 softwareKey.DeleteSubKeyTree (subKeyName);
2628 [Test] // SetValue (String, Object)
2629 public void SetValue1_Key_Removed ()
2631 string subKeyName = Guid.NewGuid ().ToString ();
2633 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2635 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2636 softwareKey.DeleteSubKeyTree (subKeyName);
2637 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2639 createdKey.SetValue ("name1", "value1");
2641 } catch (IOException ex) {
2642 // Illegal operation attempted on a registry key that
2643 // has been marked for deletion
2644 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2645 Assert.IsNotNull (ex.Message, "#4");
2646 Assert.IsNull (ex.InnerException, "#5");
2651 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2652 if (createdKey != null) {
2653 createdKey.Close ();
2654 softwareKey.DeleteSubKeyTree (subKeyName);
2662 [Test] // SetValue (String, Object, RegistryValueKind)
2663 public void SetValue2_Key_ReadOnly ()
2665 string subKeyName = Guid.NewGuid ().ToString ();
2667 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2669 softwareKey.SetValue ("name1", "value1",
2670 RegistryValueKind.String);
2672 } catch (UnauthorizedAccessException ex) {
2673 // Cannot write to the registry key
2674 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2675 Assert.IsNotNull (ex.Message, "#3");
2676 Assert.IsNull (ex.InnerException, "#4");
2680 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2682 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2685 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2687 createdKey.SetValue ("name1", "value1",
2688 RegistryValueKind.String);
2690 } catch (UnauthorizedAccessException ex) {
2691 // Cannot write to the registry key
2692 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2693 Assert.IsNotNull (ex.Message, "#3");
2694 Assert.IsNull (ex.InnerException, "#4");
2699 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2700 if (createdKey != null) {
2701 createdKey.Close ();
2702 softwareKey.DeleteSubKeyTree (subKeyName);
2710 [Test] // SetValue (String, Object, RegistryValueKind)
2711 public void SetValue2_Key_Removed ()
2713 string subKeyName = Guid.NewGuid ().ToString ();
2715 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2717 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2718 softwareKey.DeleteSubKeyTree (subKeyName);
2719 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2721 createdKey.SetValue ("name1", "value1",
2722 RegistryValueKind.String);
2724 } catch (IOException ex) {
2725 // Illegal operation attempted on a registry key that
2726 // has been marked for deletion
2727 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2728 Assert.IsNotNull (ex.Message, "#4");
2729 Assert.IsNull (ex.InnerException, "#5");
2734 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2735 if (createdKey != null) {
2736 createdKey.Close ();
2737 softwareKey.DeleteSubKeyTree (subKeyName);
2745 [Test] // SetValue (String, Object, RegistryValueKind)
2746 public void SetValue2_Name_Empty ()
2748 string subKeyName = Guid.NewGuid ().ToString ();
2750 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2752 createdKey.SetValue (string.Empty, "value1",
2753 RegistryValueKind.String);
2754 string [] names = createdKey.GetValueNames ();
2755 Assert.IsNotNull (names, "#A1");
2756 Assert.AreEqual (1, names.Length, "#A2");
2757 Assert.IsNotNull (names [0], "#A3");
2758 Assert.AreEqual (string.Empty, names [0], "#A4");
2759 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2760 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2761 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2762 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2764 createdKey.SetValue (null, "value2",
2765 RegistryValueKind.String);
2766 names = createdKey.GetValueNames ();
2767 Assert.IsNotNull (names, "#B1");
2768 Assert.AreEqual (1, names.Length, "#B2");
2769 Assert.IsNotNull (names [0], "#B3");
2770 Assert.AreEqual (string.Empty, names [0], "#B4");
2771 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2772 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2773 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2774 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2777 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2781 [Test] // SetValue (String, Object, RegistryValueKind)
2782 public void SetValue2_Name_MaxLength ()
2784 string subKeyName = Guid.NewGuid ().ToString ();
2787 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2788 string name = new string ('a', 254);
2790 createdKey.SetValue (name, "value1",
2791 RegistryValueKind.String);
2792 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2793 createdKey.DeleteValue (name);
2794 Assert.IsNull (createdKey.GetValue (name), "#A2");
2796 name = new string ('a', 255);
2798 createdKey.SetValue (name, "value2",
2799 RegistryValueKind.String);
2800 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2801 createdKey.DeleteValue (name);
2802 Assert.IsNull (createdKey.GetValue (name), "#B2");
2804 name = new string ('a', 256);
2807 createdKey.SetValue (name, "value2",
2808 RegistryValueKind.String);
2809 Assert.Fail ("#C1");
2810 } catch (ArgumentException ex) {
2811 // Registry subkeys should not be
2812 // greater than 255 characters
2813 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2814 Assert.IsNull (ex.InnerException, "#C3");
2815 Assert.IsNotNull (ex.Message, "#C4");
2816 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2817 Assert.IsNull (ex.ParamName, "#C6");
2822 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2823 if (createdKey != null) {
2824 createdKey.Close ();
2825 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2832 [Test] // SetValue (String, Object, RegistryValueKind)
2833 public void SetValue2_Name_Null ()
2835 string subKeyName = Guid.NewGuid ().ToString ();
2837 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2839 createdKey.SetValue (null, "value1",
2840 RegistryValueKind.String);
2841 string [] names = createdKey.GetValueNames ();
2842 Assert.IsNotNull (names, "#A1");
2843 Assert.AreEqual (1, names.Length, "#A2");
2844 Assert.IsNotNull (names [0], "#A3");
2845 Assert.AreEqual (string.Empty, names [0], "#A4");
2846 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2847 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2848 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2849 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2851 createdKey.SetValue (string.Empty, "value2",
2852 RegistryValueKind.String);
2853 names = createdKey.GetValueNames ();
2854 Assert.IsNotNull (names, "#B1");
2855 Assert.AreEqual (1, names.Length, "#B2");
2856 Assert.IsNotNull (names [0], "#B3");
2857 Assert.AreEqual (string.Empty, names [0], "#B4");
2858 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2859 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2860 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2861 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2864 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2868 [Test] // SetValue (String, Object, RegistryValueKind)
2869 public void SetValue2_Value_Null ()
2871 string subKeyName = Guid.NewGuid ().ToString ();
2873 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2876 createdKey.SetValue ("Name", null,
2877 RegistryValueKind.String);
2879 } catch (ArgumentNullException ex) {
2880 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2881 Assert.IsNull (ex.InnerException, "#3");
2882 Assert.IsNotNull (ex.Message, "#4");
2883 Assert.AreEqual ("value", ex.ParamName, "#5");
2887 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2892 public void SubKeyCount ()
2894 string subKeyName = Guid.NewGuid ().ToString ();
2897 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2898 // check if key was successfully created
2899 Assert.IsNotNull (createdKey, "#A1");
2900 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2903 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2904 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2907 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2909 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2910 Assert.IsNotNull (createdKey, "#B1");
2911 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
2913 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2914 Assert.IsNotNull (createdKey2, "#B3");
2915 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
2916 createdKey2.DeleteSubKey ("monotemp1");
2917 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
2919 Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
2923 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2924 if (createdKey != null) {
2925 createdKey.Close ();
2926 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2934 public void SubKeyCount_Key_Removed ()
2936 string subKeyName = Guid.NewGuid ().ToString ();
2939 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2940 // check if key was successfully created
2941 Assert.IsNotNull (createdKey, "#A1");
2942 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2945 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2946 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2949 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2951 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2952 Assert.IsNotNull (createdKey, "#B1");
2953 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
2955 // remove created key
2956 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2959 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
2960 } catch (IOException ex) {
2961 // Illegal operation attempted on a registry key that
2962 // has been marked for deletion
2963 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
2964 Assert.IsNotNull (ex.Message, "#15");
2965 Assert.IsNull (ex.InnerException, "#16");
2970 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2971 if (createdKey != null) {
2972 createdKey.Close ();
2973 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2981 public void ValueCount ()
2983 string subKeyName = Guid.NewGuid ().ToString ();
2986 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2987 // check if key was successfully created
2988 Assert.IsNotNull (createdKey, "#A1");
2989 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
2990 createdKey.SetValue ("name1", "value1");
2991 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
2992 createdKey.SetValue ("name2", "value2");
2993 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
2994 createdKey.SetValue ("name2", "value2b");
2995 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
2996 createdKey.SetValue ("name3", "value3");
2997 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
2999 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3000 Assert.IsNotNull (createdKey, "#B1");
3001 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3003 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3004 Assert.IsNotNull (createdKey2, "#B3");
3005 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
3006 createdKey2.DeleteValue ("name2");
3007 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
3009 Assert.AreEqual (2, createdKey.ValueCount, "#B6");
3013 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3014 if (createdKey != null) {
3015 createdKey.Close ();
3016 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3024 public void ValueCount_Key_Removed ()
3026 string subKeyName = Guid.NewGuid ().ToString ();
3029 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3030 // check if key was successfully created
3031 Assert.IsNotNull (createdKey, "#A1");
3032 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3033 createdKey.SetValue ("name1", "value1");
3034 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3035 createdKey.SetValue ("name2", "value2");
3036 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3037 createdKey.SetValue ("name2", "value2b");
3038 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3039 createdKey.SetValue ("name3", "value3");
3040 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3042 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3043 Assert.IsNotNull (createdKey, "#B1");
3044 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3046 // remove created key
3047 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3050 Assert.Fail ("#C1: " + createdKey.ValueCount);
3051 } catch (IOException ex) {
3052 // Illegal operation attempted on a registry key that
3053 // has been marked for deletion
3054 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3055 Assert.IsNotNull (ex.Message, "#15");
3056 Assert.IsNull (ex.InnerException, "#16");
3061 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3062 if (createdKey != null) {
3063 createdKey.Close ();
3064 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3072 public void bug79051 ()
3074 string subKeyName = Guid.NewGuid ().ToString ();
3076 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3078 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3079 createdKey.SetValue ("test", "whatever");
3080 createdKey.Close ();
3081 softwareKey.DeleteSubKeyTree (subKeyName);
3085 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3086 if (createdKey != null) {
3087 createdKey.Close ();
3088 softwareKey.DeleteSubKeyTree (subKeyName);
3097 public void bug79059 ()
3099 string subKeyName = Guid.NewGuid ().ToString ();
3101 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3103 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3104 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
3106 createdKey.Close ();
3107 softwareKey.DeleteSubKeyTree (subKeyName);
3111 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3112 if (createdKey != null) {
3113 createdKey.Close ();
3114 softwareKey.DeleteSubKeyTree (subKeyName);
3123 public void bugnew1 ()
3125 string subKeyName = Guid.NewGuid ().ToString ();
3127 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3129 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3130 createdKey.SetValue ("name1", "value1");
3132 RegistryKey testKey = null;
3134 testKey = createdKey.OpenSubKey ("test", true);
3135 if (testKey == null)
3136 testKey = createdKey.CreateSubKey ("test");
3137 testKey.SetValue ("another", "one");
3139 if (testKey != null)
3143 createdKey.SetValue ("name2", "value2");
3144 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
3145 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
3146 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
3147 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
3149 string [] names = createdKey.GetValueNames ();
3150 Assert.IsNotNull (names, "#6");
3151 Assert.AreEqual (2, names.Length, "#7");
3152 Assert.AreEqual ("name1", names [0], "#8");
3153 Assert.AreEqual ("name2", names [1], "#9");
3155 softwareKey.DeleteSubKeyTree (subKeyName);
3157 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
3158 Assert.IsNull (openedKey, "#10");
3161 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
3162 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
3165 createdKey.GetValueNames ();
3166 Assert.Fail ("#13");
3167 } catch (IOException ex) {
3168 // Illegal operation attempted on a registry key that
3169 // has been marked for deletion
3170 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3171 Assert.IsNotNull (ex.Message, "#15");
3172 Assert.IsNull (ex.InnerException, "#16");
3176 createdKey.SetValue ("name1", "value1");
3177 Assert.Fail ("#17");
3178 } catch (IOException ex) {
3179 // Illegal operation attempted on a registry key that
3180 // has been marked for deletion
3181 Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
3182 Assert.IsNotNull (ex.Message, "#19");
3183 Assert.IsNull (ex.InnerException, "#20");
3187 createdKey.SetValue ("newname", "value1");
3188 Assert.Fail ("#21");
3189 } catch (IOException ex) {
3190 // Illegal operation attempted on a registry key that
3191 // has been marked for deletion
3192 Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
3193 Assert.IsNotNull (ex.Message, "#23");
3194 Assert.IsNull (ex.InnerException, "#24");
3197 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
3198 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
3199 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
3200 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
3203 createdKey.CreateSubKey ("new");
3204 Assert.Fail ("#29");
3205 } catch (IOException ex) {
3206 // Illegal operation attempted on a registry key that
3207 // has been marked for deletion
3208 Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
3209 Assert.IsNotNull (ex.Message, "#31");
3210 Assert.IsNull (ex.InnerException, "#32");
3215 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3216 if (createdKey != null) {
3217 createdKey.Close ();
3218 softwareKey.DeleteSubKeyTree (subKeyName);
3227 public void bugnew2 () // values cannot be written on registry root (hive)
3229 string [] names = Registry.CurrentUser.GetValueNames ();
3230 Assert.IsNotNull (names, "#1");
3231 Registry.CurrentUser.SetValue ("name1", "value1");
3232 Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
3233 Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
3234 string [] newNames = Registry.CurrentUser.GetValueNames ();
3235 Assert.IsNotNull (newNames, "#4");
3236 Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
3237 Registry.CurrentUser.DeleteValue ("name1");
3241 public void bugnew3 () // on Windows, key cannot be closed twice
3243 string subKeyName = Guid.NewGuid ().ToString ();
3246 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3247 createdKey.Close ();
3250 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3255 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3256 if (createdKey != null) {
3257 createdKey.Close ();
3258 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3266 public void bugnew4 () // Key cannot be flushed once it has been closed
3268 string subKeyName = Guid.NewGuid ().ToString ();
3271 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3272 createdKey.Close ();
3275 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3280 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3281 if (createdKey != null) {
3282 createdKey.Close ();
3283 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3290 private bool RunningOnUnix {
3292 int p = (int) Environment.OSVersion.Platform;
3293 return ((p == 4) || (p == 128) || (p == 6));