2 // RegistryKeyTest.cs - NUnit Test Cases for Microsoft.Win32.RegistryKey
5 // mei (mei@work.email.ne.jp)
6 // Robert Jordan (robertj@gmx.net)
7 // Gert Driesen (drieseng@users.sourceforge.net)
10 // (C) 2004, 2005 Novell (http://www.novell.com)
15 using System.Runtime.InteropServices;
17 using Microsoft.Win32;
18 using Microsoft.Win32.SafeHandles;
20 using NUnit.Framework;
22 namespace MonoTests.Microsoft.Win32
25 public class RegistryKeyTest
27 private const string mimeroot = @"MIME\Database\Content Type";
30 [Category ("NotWorking")] // this will not work on Linux ever
31 public void TestGetValue ()
33 RegistryKey root = Registry.ClassesRoot;
36 key = root.OpenSubKey (mimeroot + @"\audio/wav");
37 Assert.AreEqual (".wav", key.GetValue ("Extension"), "GetValue #1");
38 key = root.OpenSubKey (mimeroot + @"\text/x-scriptlet");
39 Assert.AreEqual (null, key.GetValue ("Extension"), "GetValue #2");
43 public void TestHandle ()
45 // this test is for Windows only
47 Assert.Ignore ("Running on Unix.");
49 // this regpath always exists under windows
50 RegistryKey k = Registry.CurrentUser
51 .OpenSubKey ("Software", false)
52 .OpenSubKey ("Microsoft", false)
53 .OpenSubKey ("Windows", false);
55 Assert.IsNotNull (k, "#01");
59 public void OpenSubKey ()
61 RegistryKey key = Registry.LocalMachine;
63 // HKEY_LOCAL_MACHINE\software should always exist on Windows
64 // and is automatically created on Linux
65 Assert.IsNotNull (key.OpenSubKey ("Software"), "#A1");
66 Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#A2");
68 key = Registry.CurrentUser;
70 // HKEY_CURRENT_USER\software should always exist on Windows
71 // and is automatically created on Linux
72 Assert.IsNotNull (key.OpenSubKey ("Software"), "#B1");
73 Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#B2");
77 // HKEY_USERS\software should not exist on Windows, and should not
78 // be created automatically on Linux
79 Assert.IsNull (key.OpenSubKey ("Software"), "#C1");
80 Assert.IsNull (key.OpenSubKey ("soFtware"), "#C2");
84 public void OpenSubKey_Key_DoesNotExist ()
86 string subKeyName = Guid.NewGuid ().ToString ();
87 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#1"); // read-only
88 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#2"); // writable
92 public void OpenSubKey_Key_Removed ()
94 string subKeyName = Guid.NewGuid ().ToString ();
97 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
98 // check if key was successfully created
99 Assert.IsNotNull (createdKey, "#1");
100 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
103 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
104 Assert.IsNotNull (createdKey, "#2");
105 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
106 Assert.IsNotNull (createdKey, "#3");
108 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
111 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
112 Assert.IsNull (createdKey.OpenSubKey ("monotemp"), "#5"); // read-only
114 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#6");
115 Assert.IsNull (createdKey.OpenSubKey ("monotemp", true), "#7");
119 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
120 if (createdKey != null) {
122 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
130 [Category ("NotWorking")] // MS should not allow this
131 public void OpenSubKey_Name_Empty ()
134 using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty)) {
135 Assert.IsNotNull (emptyKey, "#1");
138 using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty, true)) {
139 Assert.IsNotNull (emptyKey, "#1");
144 public void OpenSubKey_Name_MaxLength ()
146 string name = new string ('a', 254);
148 Assert.IsNull (Registry.CurrentUser.OpenSubKey (name), "#A1");
150 name = new string ('a', 255);
152 Assert.IsNull (Registry.CurrentUser.OpenSubKey (name), "#B1");
154 name = new string ('a', 256);
157 Registry.CurrentUser.OpenSubKey (name);
159 } catch (ArgumentException ex) {
160 // 1.x: Registry subkeys should not be
161 // greater than or equal to 255 characters
163 // 2.x: Registry subkeys should not be
164 // greater than 255 characters
165 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
166 Assert.IsNull (ex.InnerException, "#C3");
167 Assert.IsNotNull (ex.Message, "#c4");
168 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
169 Assert.IsNull (ex.ParamName, "#C6");
174 public void OpenSubKey_Name_Null ()
177 Registry.CurrentUser.OpenSubKey (null);
179 } catch (ArgumentNullException ex) {
180 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
181 Assert.IsNull (ex.InnerException, "#A3");
182 Assert.IsNotNull (ex.Message, "#A4");
183 Assert.AreEqual ("name", ex.ParamName, "#A5");
187 Registry.CurrentUser.OpenSubKey (null, true);
189 } catch (ArgumentNullException ex) {
190 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
191 Assert.IsNull (ex.InnerException, "#B3");
192 Assert.IsNotNull (ex.Message, "#B4");
193 Assert.AreEqual ("name", ex.ParamName, "#B5");
198 public void Close_Local_Hive ()
200 RegistryKey hive = Registry.CurrentUser;
203 Assert.IsNotNull (hive.GetSubKeyNames (), "#1");
204 Assert.IsNull (hive.GetValue ("doesnotexist"), "#2");
205 Assert.IsNotNull (hive.GetValueNames (), "#3");
206 Assert.IsNull (hive.OpenSubKey ("doesnotexist"), "#4");
207 Assert.IsNotNull (hive.SubKeyCount, "#5");
208 Assert.IsNotNull (hive.ToString (), "#6");
210 // closing key again does not have any effect
215 public void Close_Local_Key ()
217 RegistryKey key = Registry.CurrentUser.OpenSubKey ("SOFTWARE");
220 // closing a key twice does not have any effect
224 key.CreateSubKey ("a");
226 } catch (ObjectDisposedException) {
230 key.DeleteSubKey ("doesnotexist");
232 } catch (ObjectDisposedException) {
236 key.DeleteSubKeyTree ("doesnotexist");
238 } catch (ObjectDisposedException) {
242 key.DeleteValue ("doesnotexist");
244 } catch (ObjectDisposedException) {
247 // flushing a closed key does not have any effect
251 key.GetSubKeyNames ();
253 } catch (ObjectDisposedException) {
257 key.GetValue ("doesnotexist");
259 } catch (ObjectDisposedException) {
263 key.GetValueNames ();
265 } catch (ObjectDisposedException) {
269 key.OpenSubKey ("doesnotexist");
271 } catch (ObjectDisposedException) {
275 key.SetValue ("doesnotexist", "something");
277 } catch (ObjectDisposedException) {
281 int x = key.SubKeyCount;
282 Assert.Fail ("#10:" + x);
283 } catch (ObjectDisposedException) {
289 } catch (ObjectDisposedException) {
293 int x = key.ValueCount;
294 Assert.Fail ("#12:" + x);
295 } catch (ObjectDisposedException) {
300 public void Close_Remote_Hive ()
302 // access to registry of remote machines is not implemented on unix
304 Assert.Ignore ("Running on Unix.");
306 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
307 RegistryHive.CurrentUser, Environment.MachineName);
310 // closing a remote hive twice does not have any effect
314 hive.CreateSubKey ("a");
316 } catch (ObjectDisposedException) {
320 hive.DeleteSubKey ("doesnotexist");
322 } catch (ObjectDisposedException) {
326 hive.DeleteSubKeyTree ("doesnotexist");
328 } catch (ObjectDisposedException) {
332 hive.DeleteValue ("doesnotexist");
334 } catch (ObjectDisposedException) {
337 // flushing a closed hive does not have any effect
341 hive.GetSubKeyNames ();
343 } catch (ObjectDisposedException) {
347 hive.GetValue ("doesnotexist");
349 } catch (ObjectDisposedException) {
353 hive.GetValueNames ();
355 } catch (ObjectDisposedException) {
359 hive.OpenSubKey ("doesnotexist");
361 } catch (ObjectDisposedException) {
365 hive.SetValue ("doesnotexist", "something");
367 } catch (ObjectDisposedException) {
371 int x = hive.SubKeyCount;
372 Assert.Fail ("#10:" + x);
373 } catch (ObjectDisposedException) {
379 } catch (ObjectDisposedException) {
383 int x = hive.ValueCount;
384 Assert.Fail ("#12:" + x);
385 } catch (ObjectDisposedException) {
390 public void Close_Remote_Key ()
392 // access to registry of remote machines is not implemented on unix
394 Assert.Ignore ("Running on Unix.");
396 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
397 RegistryHive.CurrentUser, Environment.MachineName);
398 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
401 // closing a remote key twice does not have any effect
405 key.CreateSubKey ("a");
407 } catch (ObjectDisposedException) {
411 key.DeleteSubKey ("doesnotexist");
413 } catch (ObjectDisposedException) {
417 key.DeleteSubKeyTree ("doesnotexist");
419 } catch (ObjectDisposedException) {
423 key.DeleteValue ("doesnotexist");
425 } catch (ObjectDisposedException) {
428 // flushing a closed key does not have any effect
432 key.GetSubKeyNames ();
434 } catch (ObjectDisposedException) {
438 key.GetValue ("doesnotexist");
440 } catch (ObjectDisposedException) {
444 key.GetValueNames ();
446 } catch (ObjectDisposedException) {
450 key.OpenSubKey ("doesnotexist");
452 } catch (ObjectDisposedException) {
456 key.SetValue ("doesnotexist", "something");
458 } catch (ObjectDisposedException) {
462 int x = key.SubKeyCount;
463 Assert.Fail ("#10:" + x);
464 } catch (ObjectDisposedException) {
470 } catch (ObjectDisposedException) {
474 int x = key.ValueCount;
475 Assert.Fail ("#12:" + x);
476 } catch (ObjectDisposedException) {
483 public void CreateSubKey ()
485 string subKeyName = Guid.NewGuid ().ToString ();
488 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
489 // check if key was successfully created
490 Assert.IsNotNull (createdKey, "#A1");
491 // software subkey should not be created automatically
492 Assert.IsNull (createdKey.OpenSubKey ("software"), "#A2");
495 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
496 // check if key was successfully created
497 Assert.IsNotNull (createdKey, "#B1");
498 // software subkey should not be created automatically
499 Assert.IsNull (createdKey.OpenSubKey ("software"), "#B2");
503 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
506 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
508 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
509 // check if key was successfully created
510 Assert.IsNotNull (createdKey, "#C1");
511 // software subkey should not be created automatically
512 Assert.IsNull (softwareKey.OpenSubKey ("software"), "#C2");
515 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
516 // check if key was successfully created
517 Assert.IsNotNull (createdKey, "#D1");
518 // software subkey should not be created automatically
519 Assert.IsNull (softwareKey.OpenSubKey ("software"), "#D2");
523 softwareKey.DeleteSubKeyTree (subKeyName);
529 public void CreateSubKey_Key_ReadOnly ()
531 string subKeyName = Guid.NewGuid ().ToString ();
533 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
534 RegistryKey createdKey = null;
537 createdKey = softwareKey.CreateSubKey (subKeyName);
539 } catch (UnauthorizedAccessException ex) {
540 // Cannot write to the registry key
541 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
542 Assert.IsNotNull (ex.Message, "#3");
543 Assert.IsNull (ex.InnerException, "#4");
546 if (createdKey != null)
553 public void CreateSubKey_Key_Removed ()
555 string subKeyName = Guid.NewGuid ().ToString ();
557 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
559 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
560 softwareKey.DeleteSubKeyTree (subKeyName);
561 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
563 createdKey.CreateSubKey ("test");
565 } catch (IOException ex) {
566 // Illegal operation attempted on a registry key that
567 // has been marked for deletion
568 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
569 Assert.IsNotNull (ex.Message, "#4");
570 Assert.IsNull (ex.InnerException, "#5");
575 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
576 if (createdKey != null) {
578 softwareKey.DeleteSubKeyTree (subKeyName);
587 [Category ("NotWorking")] // MS should not allow this
588 public void CreateSubKey_Name_Empty ()
590 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
591 using (RegistryKey emptyKey = softwareKey.CreateSubKey (string.Empty)) {
592 Assert.IsNotNull (emptyKey, "#1");
593 emptyKey.SetValue ("name1", "value1");
599 public void CreateSubKey_Name_MaxLength ()
601 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
602 string subKeyName = new string ('a', 254);
605 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
606 Assert.IsNotNull (createdKey, "#A1");
607 Assert.IsNotNull (softwareKey.OpenSubKey (subKeyName), "#A2");
610 softwareKey.DeleteSubKeyTree (subKeyName);
613 subKeyName = new string ('a', 255);
616 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
617 Assert.IsNotNull (createdKey, "#B1");
618 Assert.IsNotNull (softwareKey.OpenSubKey (subKeyName), "#B2");
621 softwareKey.DeleteSubKey (subKeyName);
624 subKeyName = new string ('a', 256);
627 softwareKey.CreateSubKey (subKeyName);
629 } catch (ArgumentException ex) {
630 // 1.x: Registry subkeys should not be
631 // greater than or equal to 255 characters
633 // 2.x: Registry subkeys should not be
634 // greater than 255 characters
635 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
636 Assert.IsNull (ex.InnerException, "#C3");
637 Assert.IsNotNull (ex.Message, "#C4");
638 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
639 Assert.IsNull (ex.ParamName, "#C6");
645 public void CreateSubKey_Name_Null ()
647 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
649 softwareKey.CreateSubKey (null);
651 } catch (ArgumentNullException ex) {
652 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
653 Assert.IsNull (ex.InnerException, "#3");
654 Assert.IsNotNull (ex.Message, "#4");
655 Assert.AreEqual ("name", ex.ParamName, "#5");
661 // Unfortunately we can't test that the scenario where a volatile
662 // key is not alive after a reboot, but we can test other bits.
664 public void CreateSubKey_Volatile ()
666 RegistryKey key = null;
667 RegistryKey subkey = null;
668 string subKeyName = "VolatileKey";
671 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
672 subkey = key.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
675 key = Registry.CurrentUser.OpenSubKey (subKeyName);
676 subkey = key.OpenSubKey ("Child");
677 Assert.AreEqual (true, subkey != null, "#A1");
687 public void CreateSubKey_Volatile_Child ()
689 RegistryKey key = null;
690 RegistryKey subkey = null;
691 string subKeyName = "VolatileKey";
694 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
695 subkey = key.CreateSubKey ("Child"); // Non volatile child
696 Assert.Fail ("#Exc");
697 } catch (IOException) {
707 public void CreateSubKey_Volatile_Conflict ()
709 RegistryKey key = null;
710 RegistryKey key2 = null;
711 RegistryKey subkey = null;
712 string subKeyName = "VolatileKey";
716 // Create a volatile key and try to open it as a normal one
718 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
719 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
720 Assert.AreEqual (key.Name, key2.Name, "A0");
722 subkey = key2.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
723 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#A1");
724 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#A2");
731 // Create a non-volatile key and try to open it as a volatile one
733 subKeyName = "NonVolatileKey";
734 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
735 key2.SetValue ("Name", "Mono");
736 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
737 Assert.AreEqual (key.Name, key2.Name, "B0");
738 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#B1");
739 Assert.AreEqual ("Mono", key2.GetValue ("Name"), "#B2");
741 key.CreateSubKey ("Child");
742 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#B3");
743 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#B4");
746 // Close the non-volatile key and try to re-open it as a volatile one
750 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
751 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#C0");
752 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#C1");
764 public void DeleteSubKey_Volatile ()
766 RegistryKey key = null;
767 RegistryKey subkey = null;
768 string subKeyName = "VolatileKey";
771 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
772 key.CreateSubKey ("VolatileKeyChild", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
773 key.SetValue ("Name", "Mono");
776 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
778 key = Registry.CurrentUser.OpenSubKey (subKeyName);
779 Assert.AreEqual (null, key, "#A0");
788 // Define a normal key, and create a normal and a volatile key under it, and retrieve their names.
790 public void GetSubKeyNames_Volatile ()
792 RegistryKey key = null;
793 RegistryKey subkey = null;
794 string subKeyName = Guid.NewGuid ().ToString ();
795 string volChildKeyName = "volatilechildkey";
796 string childKeyName = "childkey";
799 key = Registry.CurrentUser.CreateSubKey (subKeyName);
800 key.CreateSubKey (volChildKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
801 key.CreateSubKey (childKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
804 key = Registry.CurrentUser.OpenSubKey (subKeyName);
805 string [] keyNames = key.GetSubKeyNames ();
807 // we can guarantee the order of the child keys, so we sort the two of them
808 Array.Sort (keyNames);
810 Assert.AreEqual (2, keyNames.Length, "#A0");
811 Assert.AreEqual (childKeyName, keyNames [0], "#A1");
812 Assert.AreEqual (volChildKeyName, keyNames [1], "#A2");
824 public void DeleteSubKey ()
826 string subKeyName = Guid.NewGuid ().ToString ();
829 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
830 // check if key was successfully created
831 Assert.IsNotNull (createdKey, "#1");
833 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
834 Assert.IsNotNull (createdKey, "#2");
835 Registry.CurrentUser.DeleteSubKey (subKeyName);
837 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
838 Assert.IsNull (createdKey, "#3");
842 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
843 if (createdKey != null) {
845 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
853 public void DeleteSubKey_Key_HasChildKeys ()
855 string subKeyName = Guid.NewGuid ().ToString ();
858 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
859 // check if key was successfully created
860 Assert.IsNotNull (createdKey, "#1");
861 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
865 Registry.CurrentUser.DeleteSubKey (subKeyName);
867 } catch (InvalidOperationException ex) {
868 // Registry key has subkeys and recursive removes are not
869 // supported by this method
870 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
871 Assert.IsNotNull (ex.Message, "#4");
872 Assert.IsNull (ex.InnerException, "#5");
876 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
877 if (createdKey != null) {
879 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
887 public void DeleteSubKey_Key_ReadOnly ()
889 string subKeyName = Guid.NewGuid ().ToString ();
892 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
893 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
897 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
899 softwareKey.DeleteSubKey (subKeyName);
901 } catch (UnauthorizedAccessException ex) {
902 // Cannot write to the registry key
903 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
904 Assert.IsNotNull (ex.Message, "#3");
905 Assert.IsNull (ex.InnerException, "#4");
910 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
911 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
912 if (createdKey != null) {
914 softwareKey.DeleteSubKeyTree (subKeyName);
923 public void DeleteSubKey_Key_DoesNotExist ()
925 string subKeyName = Guid.NewGuid ().ToString ();
928 Registry.CurrentUser.DeleteSubKey (subKeyName);
930 } catch (ArgumentException ex) {
931 // Cannot delete a subkey tree because the subkey does not exist
932 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
933 Assert.IsNull (ex.InnerException, "#A3");
934 Assert.IsNotNull (ex.Message, "#A4");
935 Assert.IsNull (ex.ParamName, "#A5");
939 Registry.CurrentUser.DeleteSubKey (subKeyName, true);
941 } catch (ArgumentException ex) {
942 // Cannot delete a subkey tree because the subkey does not exist
943 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
944 Assert.IsNull (ex.InnerException, "#B3");
945 Assert.IsNotNull (ex.Message, "#B4");
946 Assert.IsNull (ex.ParamName, "#B5");
949 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
953 public void DeleteSubKey_Key_Removed ()
955 string subKeyName = Guid.NewGuid ().ToString ();
958 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
959 // check if key was successfully created
960 Assert.IsNotNull (createdKey, "#1");
961 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
964 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
965 Assert.IsNotNull (createdKey, "#2");
966 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
967 Assert.IsNotNull (createdKey, "#3");
969 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
970 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
972 createdKey.DeleteSubKey ("monotemp");
974 } catch (ArgumentException ex) {
975 // Cannot delete a subkey tree because the subkey does
977 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
978 Assert.IsNull (ex.InnerException, "#7");
979 Assert.IsNotNull (ex.Message, "#8");
980 Assert.IsNull (ex.ParamName, "#9");
985 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
986 if (createdKey != null) {
988 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
996 [Category ("NotWorking")] // MS should not allow this
997 public void DeleteSubKey_Name_Empty ()
999 string subKeyName = Guid.NewGuid ().ToString ();
1001 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1003 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1004 createdKey.DeleteSubKey (string.Empty);
1005 createdKey.Close ();
1007 createdKey = softwareKey.OpenSubKey (subKeyName);
1008 Assert.IsNull (createdKey, "#1");
1011 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1012 if (createdKey != null)
1013 createdKey.Close ();
1014 softwareKey.DeleteSubKeyTree (subKeyName);
1022 public void DeleteSubKey_Name_MaxLength ()
1024 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1025 string subKeyName = new string ('a', 254);
1027 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1028 createdKey.Close ();
1030 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1031 Assert.IsNotNull (createdKey, "#A1");
1033 softwareKey.DeleteSubKey (subKeyName);
1034 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1035 Assert.IsNull (createdKey, "#A2");
1038 subKeyName = new string ('a', 256);
1041 softwareKey.DeleteSubKey (subKeyName);
1042 Assert.Fail ("#B1");
1043 } catch (ArgumentException ex) {
1044 // 1.x: Registry subkeys should not be
1045 // greater than or equal to 255 characters
1047 // 2.x: Registry subkeys should not be
1048 // greater than 255 characters
1049 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1050 Assert.IsNull (ex.InnerException, "#B3");
1051 Assert.IsNotNull (ex.Message, "#B4");
1052 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1053 Assert.IsNull (ex.ParamName, "#B6");
1059 public void DeleteSubKey_Name_Null ()
1061 string subKeyName = Guid.NewGuid ().ToString ();
1063 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1065 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1067 createdKey.DeleteSubKey (null);
1069 } catch (ArgumentNullException ex) {
1070 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1071 Assert.IsNull (ex.InnerException, "#3");
1072 Assert.IsNotNull (ex.Message, "#4");
1073 Assert.AreEqual ("name", ex.ParamName, "#5");
1077 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1078 if (createdKey != null)
1079 createdKey.Close ();
1080 softwareKey.DeleteSubKeyTree (subKeyName);
1088 public void DeleteSubKeyTree ()
1091 // - remove key with subkeys
1092 // - remove key of which some subkeys are marked for deletion
1093 // - remove key with values
1097 public void DeleteSubKeyTree_Key_DoesNotExist ()
1099 // Cannot delete a subkey tree because the subkey does not exist
1100 string subKeyName = Guid.NewGuid ().ToString ();
1102 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1104 } catch (ArgumentException ex) {
1105 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1106 Assert.IsNull (ex.InnerException, "#3");
1107 Assert.IsNotNull (ex.Message, "#4");
1108 Assert.IsNull (ex.ParamName, "#5");
1114 public void DeleteSubKeyTree_Key_DoesNotExist_Overload ()
1116 // Cannot delete a subkey tree because the subkey does not exist
1117 string subKeyName = Guid.NewGuid ().ToString ();
1119 Registry.CurrentUser.DeleteSubKeyTree (subKeyName, true);
1121 } catch (ArgumentException ex) {
1122 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1123 Assert.IsNull (ex.InnerException, "#3");
1124 Assert.IsNotNull (ex.Message, "#4");
1125 Assert.IsNull (ex.ParamName, "#5");
1128 // It's enough to know this line is not throwing an exception.
1129 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
1134 public void DeleteSubKeyTree_Key_ReadOnly ()
1136 string subKeyName = Guid.NewGuid ().ToString ();
1139 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1140 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1141 createdKey.Close ();
1144 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
1146 softwareKey.DeleteSubKeyTree (subKeyName);
1148 } catch (UnauthorizedAccessException ex) {
1149 // Cannot write to the registry key
1150 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1151 Assert.IsNotNull (ex.Message, "#3");
1152 Assert.IsNull (ex.InnerException, "#4");
1157 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1158 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1159 if (createdKey != null)
1160 createdKey.Close ();
1161 softwareKey.DeleteSubKeyTree (subKeyName);
1169 public void DeleteSubKeyTree_Key_Removed ()
1171 string subKeyName = Guid.NewGuid ().ToString ();
1174 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1175 // check if key was successfully created
1176 Assert.IsNotNull (createdKey, "#1");
1177 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
1180 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1181 Assert.IsNotNull (createdKey, "#2");
1182 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
1183 Assert.IsNotNull (createdKey, "#3");
1185 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1186 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
1188 createdKey.DeleteSubKeyTree ("monotemp");
1190 } catch (ArgumentException ex) {
1191 // Cannot delete a subkey tree because the subkey does
1193 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
1194 Assert.IsNull (ex.InnerException, "#7");
1195 Assert.IsNotNull (ex.Message, "#8");
1196 Assert.IsNull (ex.ParamName, "#9");
1201 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1202 if (createdKey != null) {
1203 createdKey.Close ();
1204 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1212 [Category ("NotWorking")] // MS should not allow this
1213 public void DeleteSubKeyTree_Name_Empty ()
1215 string subKeyName = Guid.NewGuid ().ToString ();
1217 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1219 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1220 createdKey.DeleteSubKeyTree (string.Empty);
1221 createdKey.Close ();
1223 createdKey = softwareKey.OpenSubKey (subKeyName);
1224 Assert.IsNull (createdKey, "#1");
1227 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1228 if (createdKey != null)
1229 createdKey.Close ();
1230 softwareKey.DeleteSubKeyTree (subKeyName);
1238 public void DeleteSubKeyTree_Name_MaxLength ()
1240 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1241 string subKeyName = new string ('a', 254);
1243 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1244 createdKey.Close ();
1246 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1247 Assert.IsNotNull (createdKey, "#A1");
1249 softwareKey.DeleteSubKeyTree (subKeyName);
1250 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1251 Assert.IsNull (createdKey, "#A2");
1254 subKeyName = new string ('a', 256);
1257 softwareKey.DeleteSubKeyTree (subKeyName);
1258 Assert.Fail ("#B1");
1259 } catch (ArgumentException ex) {
1260 // 1.x: Registry subkeys should not be
1261 // greater than or equal to 255 characters
1263 // 2.x: Registry subkeys should not be
1264 // greater than 255 characters
1265 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1266 Assert.IsNull (ex.InnerException, "#B3");
1267 Assert.IsNotNull (ex.Message, "#B4");
1268 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1269 Assert.IsNull (ex.ParamName, "#B6");
1275 public void DeleteSubKeyTree_Name_Null ()
1277 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1279 softwareKey.DeleteSubKeyTree (null);
1281 } catch (ArgumentNullException ex) {
1282 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1283 Assert.IsNull (ex.InnerException, "#3");
1284 Assert.IsNotNull (ex.Message, "#4");
1285 Assert.AreEqual ("name", ex.ParamName, "#5");
1291 public void DeleteValue ()
1293 string subKeyName = Guid.NewGuid ().ToString ();
1296 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1297 // check if key was successfully created
1298 Assert.IsNotNull (createdKey, "#A1");
1299 createdKey.SetValue ("name1", "value1");
1300 createdKey.SetValue ("name2", "value2");
1301 string [] names = createdKey.GetValueNames ();
1302 Assert.IsNotNull (names, "#A2");
1303 Assert.AreEqual (2, names.Length, "#A3");
1304 Assert.IsNotNull (names [0], "#A4");
1305 Assert.AreEqual ("name1", names [0], "#A5");
1306 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A6");
1307 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A7");
1308 Assert.AreEqual ("name2", names [1], "#A8");
1309 Assert.IsNotNull (createdKey.GetValue ("name2"), "#A9");
1310 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#A10");
1312 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1313 Assert.IsNotNull (createdKey, "#B1");
1314 createdKey.DeleteValue ("name1");
1315 string [] names = createdKey.GetValueNames ();
1316 Assert.IsNotNull (names, "#B2");
1317 Assert.AreEqual (1, names.Length, "#B3");
1318 Assert.IsNotNull (names [0], "#B4");
1319 Assert.AreEqual ("name2", names [0], "#B5");
1320 Assert.IsNotNull (createdKey.GetValue ("name2"), "#B6");
1321 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#B7");
1322 createdKey.DeleteValue (new string ('a', 400), false);
1324 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1325 string [] names = createdKey.GetValueNames ();
1326 Assert.IsNotNull (names, "#C1");
1327 Assert.AreEqual (1, names.Length, "#C2");
1328 Assert.IsNotNull (names [0], "#C3");
1329 Assert.AreEqual ("name2", names [0], "#C4");
1330 Assert.IsNotNull (createdKey.GetValue ("name2"), "#C5");
1331 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#C6");
1335 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1336 if (createdKey != null) {
1337 createdKey.Close ();
1338 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1346 public void DeleteValue_Key_ReadOnly ()
1348 string subKeyName = Guid.NewGuid ().ToString ();
1351 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1352 createdKey.SetValue ("name1", "value1");
1355 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1357 // deleting value that exists
1358 createdKey.DeleteValue ("name1");
1359 Assert.Fail ("#A1");
1360 } catch (UnauthorizedAccessException ex) {
1361 // Cannot write to the registry key
1362 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#A2");
1363 Assert.IsNotNull (ex.Message, "#A3");
1364 Assert.IsNull (ex.InnerException, "#A4");
1368 // deleting value that exists
1369 createdKey.DeleteValue ("name1", true);
1370 Assert.Fail ("#B1");
1371 } catch (UnauthorizedAccessException ex) {
1372 // Cannot write to the registry key
1373 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#B2");
1374 Assert.IsNotNull (ex.Message, "#B3");
1375 Assert.IsNull (ex.InnerException, "#B4");
1379 // deleting value that exists
1380 createdKey.DeleteValue ("name1", false);
1381 Assert.Fail ("#C1");
1382 } catch (UnauthorizedAccessException ex) {
1383 // Cannot write to the registry key
1384 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#C2");
1385 Assert.IsNotNull (ex.Message, "#C3");
1386 Assert.IsNull (ex.InnerException, "#C4");
1390 // deleting value that does not exist
1391 createdKey.DeleteValue ("name2");
1392 Assert.Fail ("#D1");
1393 } catch (UnauthorizedAccessException ex) {
1394 // Cannot write to the registry key
1395 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#D2");
1396 Assert.IsNotNull (ex.Message, "#D3");
1397 Assert.IsNull (ex.InnerException, "#D4");
1401 // deleting value that does not exist
1402 createdKey.DeleteValue ("name2", true);
1403 Assert.Fail ("#E1");
1404 } catch (UnauthorizedAccessException ex) {
1405 // Cannot write to the registry key
1406 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#E2");
1407 Assert.IsNotNull (ex.Message, "#E3");
1408 Assert.IsNull (ex.InnerException, "#E4");
1412 // deleting value that does not exist
1413 createdKey.DeleteValue ("name2", false);
1414 Assert.Fail ("#F1");
1415 } catch (UnauthorizedAccessException ex) {
1416 // Cannot write to the registry key
1417 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#F2");
1418 Assert.IsNotNull (ex.Message, "#F3");
1419 Assert.IsNull (ex.InnerException, "#F4");
1424 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1425 if (createdKey != null) {
1426 createdKey.Close ();
1427 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1435 public void DeleteValue_Key_Removed ()
1437 string subKeyName = Guid.NewGuid ().ToString ();
1440 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1441 // check if key was successfully created
1442 Assert.IsNotNull (createdKey, "#1");
1443 createdKey.SetValue ("name1", "value1");
1445 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1446 Assert.IsNotNull (createdKey, "#2");
1447 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1448 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#3");
1450 createdKey.DeleteValue ("name1");
1451 createdKey.DeleteValue ("name1", true);
1452 createdKey.DeleteValue ("name1", false);
1456 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1457 if (createdKey != null) {
1458 createdKey.Close ();
1459 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1467 public void DeleteValue_Value_DoesNotExist ()
1469 string subKeyName = Guid.NewGuid ().ToString ();
1472 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1473 // check if key was successfully created
1474 Assert.IsNotNull (createdKey, "#A1");
1475 createdKey.SetValue ("name1", "value1");
1478 createdKey.DeleteValue ("name2");
1479 Assert.Fail ("#B1");
1480 } catch (ArgumentException ex) {
1481 // No value exists with that name
1482 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1483 Assert.IsNull (ex.InnerException, "#B3");
1484 Assert.IsNotNull (ex.Message, "#B4");
1485 Assert.IsNull (ex.ParamName, "#B5");
1489 createdKey.DeleteValue ("name2", true);
1490 Assert.Fail ("#C1");
1491 } catch (ArgumentException ex) {
1492 // No value exists with that name
1493 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1494 Assert.IsNull (ex.InnerException, "#C3");
1495 Assert.IsNotNull (ex.Message, "#C4");
1496 Assert.IsNull (ex.ParamName, "#C5");
1499 createdKey.DeleteValue ("name2", false);
1503 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1504 if (createdKey != null) {
1505 createdKey.Close ();
1506 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1514 public void DeleteValue_Name_Empty ()
1516 string subKeyName = Guid.NewGuid ().ToString ();
1519 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1520 createdKey.SetValue ("name1", "value1");
1521 createdKey.SetValue (string.Empty, "value2");
1523 string [] names = createdKey.GetValueNames ();
1524 Assert.IsNotNull (names, "#A1");
1525 Assert.AreEqual (2, names.Length, "#A2");
1526 Assert.IsNotNull (names [0], "#A3");
1528 Assert.AreEqual ("name1", names [0], "#A4");
1530 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1531 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1532 Assert.IsNotNull (names [1], "#A7");
1534 Assert.AreEqual (string.Empty, names [1], "#A8");
1536 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A9");
1537 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#A10");
1539 createdKey.DeleteValue (string.Empty);
1541 names = createdKey.GetValueNames ();
1542 Assert.IsNotNull (names, "#B1");
1543 Assert.AreEqual (1, names.Length, "#B2");
1544 Assert.IsNotNull (names [0], "#B3");
1545 Assert.AreEqual ("name1", names [0], "#B4");
1546 Assert.IsNotNull (createdKey.GetValue ("name1"), "#B5");
1547 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#B6");
1550 createdKey.DeleteValue (string.Empty);
1551 Assert.Fail ("#C1");
1552 } catch (ArgumentException ex) {
1553 // No value exists with that name
1554 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1555 Assert.IsNull (ex.InnerException, "#C3");
1556 Assert.IsNotNull (ex.Message, "#C4");
1557 Assert.IsNull (ex.ParamName, "#C5");
1561 createdKey.DeleteValue (string.Empty, true);
1562 Assert.Fail ("#D1");
1563 } catch (ArgumentException ex) {
1564 // No value exists with that name
1565 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
1566 Assert.IsNull (ex.InnerException, "#D3");
1567 Assert.IsNotNull (ex.Message, "#D4");
1568 Assert.IsNull (ex.ParamName, "#D5");
1571 createdKey.DeleteValue (string.Empty, false);
1573 names = createdKey.GetValueNames ();
1574 Assert.IsNotNull (names, "#E1");
1575 Assert.AreEqual (1, names.Length, "#E2");
1576 Assert.IsNotNull (names [0], "#E3");
1577 Assert.AreEqual ("name1", names [0], "#E4");
1578 Assert.IsNotNull (createdKey.GetValue ("name1"), "#E5");
1579 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#E6");
1583 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1584 if (createdKey != null) {
1585 createdKey.Close ();
1586 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1594 public void DeleteValue_Name_Null ()
1596 string subKeyName = Guid.NewGuid ().ToString ();
1599 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1600 createdKey.SetValue ("name1", "value1");
1601 createdKey.SetValue (null, "value2");
1603 string [] names = createdKey.GetValueNames ();
1604 Assert.IsNotNull (names, "#A1");
1605 Assert.AreEqual (2, names.Length, "#A2");
1606 Assert.IsNotNull (names [0], "#A3");
1608 Assert.AreEqual ("name1", names [0], "#A4");
1610 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1611 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1612 Assert.IsNotNull (names [1], "#A7");
1614 Assert.AreEqual (string.Empty, names [1], "#A8");
1616 Assert.IsNotNull (createdKey.GetValue (null), "#A9");
1617 Assert.AreEqual ("value2", createdKey.GetValue (null), "#A10");
1620 createdKey.DeleteValue (null);
1621 Assert.Fail ("#B1");
1622 } catch (ArgumentNullException ex) {
1623 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1624 Assert.IsNull (ex.InnerException, "#B3");
1625 Assert.IsNotNull (ex.Message, "#B4");
1626 Assert.AreEqual ("name", ex.ParamName, "#B5");
1630 createdKey.DeleteValue (null, true);
1631 Assert.Fail ("#C1");
1632 } catch (ArgumentNullException ex) {
1633 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
1634 Assert.IsNull (ex.InnerException, "#C3");
1635 Assert.IsNotNull (ex.Message, "#C4");
1636 Assert.AreEqual ("name", ex.ParamName, "#C5");
1640 createdKey.DeleteValue (null, false);
1641 Assert.Fail ("#D1");
1642 } catch (ArgumentNullException ex) {
1643 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
1644 Assert.IsNull (ex.InnerException, "#D3");
1645 Assert.IsNotNull (ex.Message, "#D4");
1646 Assert.AreEqual ("name", ex.ParamName, "#D5");
1651 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1652 if (createdKey != null) {
1653 createdKey.Close ();
1654 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1662 [DllImport ("advapi32.dll", CharSet = CharSet.Unicode)]
1663 static extern int RegOpenKeyEx (IntPtr keyBase, string keyName, IntPtr reserved, int access, out IntPtr keyHandle);
1665 const int RegCurrentUserHive = -2147483647;
1666 const int RegAccessRead = 0x00020019;
1668 // FromHandle is specially designed to retrieve a RegistryKey instance based on a IntPtr
1669 // retrieved using any unmanaged registry function, so we use directly RegOpenKeyEx to load
1670 // our handle and then pass it to the new 4.0 method.
1672 public void FromHandle ()
1674 // Not supported on Unix
1676 Assert.Ignore ("Running on Unix.");
1678 string subKeyName = Guid.NewGuid ().ToString ();
1680 using (RegistryKey key = Registry.CurrentUser.CreateSubKey (subKeyName))
1681 key.SetValue ("Name", "Mono001");
1684 int res = RegOpenKeyEx (new IntPtr (RegCurrentUserHive), subKeyName, IntPtr.Zero, RegAccessRead, out handle);
1686 using (RegistryKey key = RegistryKey.FromHandle (new SafeRegistryHandle (handle, true))) {
1687 Assert.AreEqual (String.Empty, key.Name, "#A0");
1688 Assert.AreEqual ("Mono001", key.GetValue ("Name"), "#A1");
1691 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1692 if (createdKey != null) {
1693 createdKey.Close ();
1694 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1700 public void FromHandle_InvalidHandle ()
1702 // Not supported on Unix
1704 Assert.Ignore ("Running on Unix.");
1706 string subKeyName = Guid.NewGuid ().ToString ();
1708 using (RegistryKey key = RegistryKey.FromHandle (new SafeRegistryHandle (IntPtr.Zero, true))) {
1709 Assert.AreEqual (String.Empty, key.Name, "#A0");
1711 // Any operation should throw a IOException, since even if we have a RegistryKey instance,
1712 // the handle is not valid.
1713 key.CreateSubKey ("ChildSubKey");
1714 Assert.Fail ("#Exc0");
1716 } catch (IOException) {
1718 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1719 if (createdKey != null) {
1720 createdKey.Close ();
1721 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1727 public void Handle ()
1730 Assert.Ignore ("Running on Unix.");
1732 string subKeyName = Guid.NewGuid ().ToString ();
1733 RegistryKey subkey = null;
1735 subkey = Registry.CurrentUser.CreateSubKey (subKeyName);
1736 Assert.AreEqual (true, subkey.Handle != null, "#A0");
1737 Assert.AreEqual (false, subkey.Handle.IsClosed, "#A1");
1738 Assert.AreEqual (false, subkey.Handle.IsInvalid, "#A2");
1742 if (subkey.Handle != null)
1743 Console.WriteLine (); // Avoids a warning at compile time
1744 Assert.Fail ("#Disposed");
1745 } catch (ObjectDisposedException) {
1749 if (subkey != null) {
1751 Registry.CurrentUser.DeleteSubKeyTree (subKeyName, false);
1758 public void GetValue ()
1760 string subKeyName = Guid.NewGuid ().ToString ();
1763 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1764 createdKey.SetValue ("name1", "value1");
1765 createdKey.SetValue ("name2", "value2");
1768 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1769 Assert.IsNotNull (createdKey.GetValue ("name1"), "#1");
1770 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#2");
1771 Assert.IsNotNull (createdKey.GetValue ("name2"), "#3");
1772 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#4");
1773 Assert.IsNull (createdKey.GetValue ("name3"), "#5");
1774 Assert.AreEqual ("value3", createdKey.GetValue ("name3", "value3"), "#6");
1775 Assert.IsNull (createdKey.GetValue ("name3", null), "#7");
1776 Assert.IsNull (createdKey.GetValue (new string ('a', 400)), "#8");
1780 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1781 if (createdKey != null) {
1782 createdKey.Close ();
1783 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1791 public void GetValue_Key_Removed ()
1793 string subKeyName = Guid.NewGuid ().ToString ();
1796 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1797 createdKey.SetValue ("name1", "value1");
1798 createdKey.SetValue ("name2", "value2");
1801 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1802 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1804 Assert.IsNull (createdKey.GetValue ("name1"), "#1");
1805 Assert.IsNotNull (createdKey.GetValue ("name1", "default"), "#2");
1806 Assert.AreEqual ("default", createdKey.GetValue ("name1", "default"), "#3");
1807 Assert.IsNull (createdKey.GetValue ("name3"), "#3");
1808 Assert.IsNotNull (createdKey.GetValue ("name3", "default"), "#4");
1809 Assert.AreEqual ("default", createdKey.GetValue ("name3", "default"), "#5");
1813 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1814 if (createdKey != null) {
1815 createdKey.Close ();
1816 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1824 public void GetValue_Name_Empty ()
1826 string subKeyName = Guid.NewGuid ().ToString ();
1829 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1830 createdKey.SetValue ("name1", "value1");
1831 createdKey.SetValue ("name2", "value2");
1833 Assert.IsNull (createdKey.GetValue (string.Empty), "#A1");
1834 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#A2");
1835 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#A3");
1836 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#A4");
1839 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1840 Assert.IsNull (createdKey.GetValue (string.Empty), "#B1");
1841 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#B2");
1842 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#B3");
1843 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#B4");
1846 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1847 createdKey.SetValue (string.Empty, "value1");
1848 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#C1");
1849 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#C2");
1850 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#C3");
1851 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#C4");
1854 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1855 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#D1");
1856 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#D2");
1857 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#D3");
1858 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#D4");
1862 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1863 if (createdKey != null) {
1864 createdKey.Close ();
1865 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1873 public void GetValue_Name_Null ()
1875 string subKeyName = Guid.NewGuid ().ToString ();
1878 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1879 createdKey.SetValue ("name1", "value1");
1880 createdKey.SetValue ("name2", "value2");
1882 Assert.IsNull (createdKey.GetValue (null), "#A1");
1883 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#A2");
1884 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#A3");
1885 Assert.IsNull (createdKey.GetValue (null, null), "#A4");
1888 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1889 Assert.IsNull (createdKey.GetValue (null), "#B1");
1890 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#B2");
1891 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#B3");
1892 Assert.IsNull (createdKey.GetValue (null, null), "#B4");
1895 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1896 createdKey.SetValue (string.Empty, "value1");
1897 Assert.IsNotNull (createdKey.GetValue (null), "#C1");
1898 Assert.AreEqual ("value1", createdKey.GetValue (null), "#C2");
1899 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#C3");
1900 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#C4");
1903 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1904 Assert.IsNotNull (createdKey.GetValue (null), "#D1");
1905 Assert.AreEqual ("value1", createdKey.GetValue (null), "#D2");
1906 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#D3");
1907 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#D4");
1911 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1912 if (createdKey != null) {
1913 createdKey.Close ();
1914 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1922 public void GetValue_Expand ()
1924 string subKeyName = Guid.NewGuid ().ToString ();
1927 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1928 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1929 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1931 createdKey.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1932 RegistryValueKind.ExpandString);
1933 createdKey.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1934 createdKey.SetValue ("name3", "just some text",
1935 RegistryValueKind.ExpandString);
1937 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#A1");
1938 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#A2");
1939 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#A3");
1940 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1941 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A4");
1942 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1943 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A5");
1944 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1945 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A6");
1946 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1947 null, RegistryValueOptions.None), "#A7");
1948 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1949 null, RegistryValueOptions.None), "#A8");
1950 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1951 null, RegistryValueOptions.None), "#A9");
1953 Environment.SetEnvironmentVariable ("MONO_TEST1", "789");
1954 Environment.SetEnvironmentVariable ("MONO_TEST2", "666");
1956 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#B1");
1957 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#B2");
1958 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#B3");
1959 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1960 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B4");
1961 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1962 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B5");
1963 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1964 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B6");
1965 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1966 null, RegistryValueOptions.None), "#B7");
1967 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1968 null, RegistryValueOptions.None), "#B8");
1969 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1970 null, RegistryValueOptions.None), "#B9");
1972 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1973 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#C1");
1974 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#C2");
1975 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#C3");
1976 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1977 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C4");
1978 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1979 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C5");
1980 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1981 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C6");
1982 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1983 null, RegistryValueOptions.None), "#C7");
1984 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1985 null, RegistryValueOptions.None), "#C8");
1986 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1987 null, RegistryValueOptions.None), "#C9");
1989 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1990 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1992 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#D1");
1993 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#D2");
1994 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#D3");
1995 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1996 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D4");
1997 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1998 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D5");
1999 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
2000 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D6");
2001 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
2002 null, RegistryValueOptions.None), "#D7");
2003 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
2004 null, RegistryValueOptions.None), "#D8");
2005 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
2006 null, RegistryValueOptions.None), "#D9");
2010 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2011 if (createdKey != null) {
2012 createdKey.Close ();
2013 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2021 public void GetValueNames ()
2023 string subKeyName = Guid.NewGuid ().ToString ();
2026 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2027 string [] names = createdKey.GetValueNames ();
2028 Assert.IsNotNull (names, "#A1");
2029 Assert.AreEqual (0, names.Length, "#A2");
2031 createdKey.SetValue ("name1", "value1");
2032 createdKey.SetValue ("name2", "value2");
2033 createdKey.SetValue ("namelong", "value3");
2034 createdKey.SetValue ("name3", "value4");
2036 Assert.AreEqual (4, createdKey.ValueCount, "#B1");
2037 names = createdKey.GetValueNames ();
2038 Assert.IsNotNull (names, "#B2");
2039 Assert.AreEqual (4, names.Length, "#B3");
2042 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2043 string [] names = createdKey.GetValueNames ();
2044 Assert.IsNotNull (names, "#C1");
2045 Assert.AreEqual (4, names.Length, "#C2");
2047 // Mono's Unix registry API uses a hashtable to store the
2048 // values (and their names), so names are not returned in
2051 // to test whether the names returned by GetValueNames
2052 // match what we expect, we use these names to remove the
2053 // the values from the created keys and such we should end
2054 // up with zero values
2055 for (int i = 0; i < names.Length; i++) {
2056 string valueName = names [i];
2057 createdKey.DeleteValue (valueName);
2060 // all values should be removed now
2061 Assert.AreEqual (0, createdKey.ValueCount, "#C3");
2065 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2066 if (createdKey != null) {
2067 createdKey.Close ();
2068 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2076 public void GetValueNames_Key_Removed ()
2078 string subKeyName = Guid.NewGuid ().ToString ();
2081 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2082 createdKey.SetValue ("name1", "value1");
2083 createdKey.SetValue ("name2", "value2");
2085 string [] names = createdKey.GetValueNames ();
2086 Assert.IsNotNull (names, "#A1");
2087 Assert.AreEqual (2, names.Length, "#A2");
2090 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2091 string [] names = createdKey.GetValueNames ();
2092 Assert.IsNotNull (names, "#B1");
2093 Assert.AreEqual (2, names.Length, "#B2");
2095 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2098 createdKey.GetValueNames ();
2099 Assert.Fail ("#C1");
2100 } catch (IOException ex) {
2101 // Illegal operation attempted on a registry key that
2102 // has been marked for deletion
2103 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
2104 Assert.IsNotNull (ex.Message, "#C3");
2105 Assert.IsNull (ex.InnerException, "#C4");
2110 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2111 if (createdKey != null) {
2112 createdKey.Close ();
2113 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2120 [Test] // bug #78519
2121 public void GetSubKeyNamesTest ()
2123 string subKeyName = Guid.NewGuid ().ToString ();
2125 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2127 // check if key was successfully created
2128 Assert.IsNotNull (createdKey, "#A");
2130 RegistryKey subKey = createdKey.CreateSubKey ("foo");
2131 Assert.IsNotNull (subKey, "#B1");
2132 Assert.AreEqual (1, createdKey.SubKeyCount, "#B2");
2133 string[] subKeyNames = createdKey.GetSubKeyNames ();
2134 Assert.IsNotNull (subKeyNames, "#B3");
2135 Assert.AreEqual (1, subKeyNames.Length, "#B4");
2136 Assert.AreEqual ("foo", subKeyNames[0], "#B5");
2138 subKey = createdKey.CreateSubKey ("longfoo");
2139 Assert.IsNotNull (subKey, "#C1");
2140 Assert.AreEqual (2, createdKey.SubKeyCount, "#C2");
2141 subKeyNames = createdKey.GetSubKeyNames ();
2142 Assert.IsNotNull (subKeyNames, "#C3");
2143 Assert.AreEqual (2, subKeyNames.Length, "#C4");
2144 Assert.AreEqual ("foo", subKeyNames [0], "#C5");
2145 Assert.AreEqual ("longfoo", subKeyNames [1], "#C6");
2147 subKey = createdKey.CreateSubKey ("sfoo");
2148 Assert.IsNotNull (subKey, "#D1");
2149 Assert.AreEqual (3, createdKey.SubKeyCount, "#D2");
2150 subKeyNames = createdKey.GetSubKeyNames ();
2151 Assert.IsNotNull (subKeyNames, "#D3");
2152 Assert.AreEqual (3, subKeyNames.Length, "#D4");
2153 Assert.AreEqual ("foo", subKeyNames [0], "#D5");
2154 Assert.AreEqual ("longfoo", subKeyNames [1], "#D6");
2155 Assert.AreEqual ("sfoo", subKeyNames [2], "#D7");
2157 foreach (string name in subKeyNames) {
2158 createdKey.DeleteSubKeyTree (name);
2160 Assert.AreEqual (0, createdKey.SubKeyCount, "#E");
2163 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2168 public void OpenRemoteBaseKey ()
2170 // access to registry of remote machines is not implemented on unix
2172 Assert.Ignore ("Running on Unix.");
2174 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
2175 RegistryHive.CurrentUser, Environment.MachineName);
2176 Assert.IsNotNull (hive, "#1");
2178 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
2179 Assert.IsNotNull (key, "#2");
2186 public void OpenRemoteBaseKey_MachineName_Null ()
2189 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser, null);
2191 } catch (ArgumentNullException ex) {
2192 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2193 Assert.IsNull (ex.InnerException, "#3");
2194 Assert.IsNotNull (ex.Message, "#4");
2195 Assert.AreEqual ("machineName", ex.ParamName, "#5");
2200 // This hangs on windows
2201 [Category ("NotWorking")]
2202 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
2204 // access to registry of remote machines is not implemented on unix
2206 Assert.Ignore ("Running on Unix.");
2209 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
2212 } catch (IOException ex) {
2213 // The network path was not found
2214 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2215 Assert.IsNotNull (ex.Message, "#3");
2216 Assert.IsNull (ex.InnerException, "#4");
2220 [Test] // bug #322839
2221 public void SetValue1_EntityReferences ()
2223 string subKeyName = Guid.NewGuid ().ToString ();
2226 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2227 // we created a new subkey, so value should not exist
2228 Assert.IsNull (createdKey.GetValue ("FirstName&\"<LastName>\""), "#A1");
2230 createdKey.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
2232 object name = createdKey.GetValue ("FirstName&\"<LastName>\"");
2233 // value should exist
2234 Assert.IsNotNull (name, "#A2");
2235 // type of value should be string
2236 Assert.AreEqual (typeof (string), name.GetType (), "#A3");
2237 // ensure value matches
2238 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#A4");
2240 // we created a new subkey, so value should not exist
2241 Assert.IsNull (createdKey.GetValue ("Info"), "#B1");
2243 createdKey.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" });
2245 object info = createdKey.GetValue ("Info");
2246 // value should exist
2247 Assert.IsNotNull (info, "#B2");
2248 // type of value should be string
2249 Assert.AreEqual (typeof (string []), info.GetType (), "#B3");
2250 // ensure value matches
2251 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#B4");
2254 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2255 object name = openedKey.GetValue ("FirstName&\"<LastName>\"");
2256 Assert.IsNotNull (name, "#C1");
2257 Assert.AreEqual (typeof (string), name.GetType (), "#C2");
2258 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#C3");
2260 object info = openedKey.GetValue ("Info");
2261 Assert.IsNotNull (info, "#D1");
2262 Assert.AreEqual (typeof (string []), info.GetType (), "#D2");
2263 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#D3");
2267 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2271 [Test] // SetValue (String, Object)
2272 public void SetValue1_Name_Null ()
2274 string subKeyName = Guid.NewGuid ().ToString ();
2276 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2278 createdKey.SetValue (null, "value1");
2279 string [] names = createdKey.GetValueNames ();
2280 Assert.IsNotNull (names, "#A1");
2281 Assert.AreEqual (1, names.Length, "#A2");
2282 Assert.IsNotNull (names [0], "#A3");
2283 Assert.AreEqual (string.Empty, names [0], "#A4");
2284 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2285 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2286 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2287 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2289 createdKey.SetValue (string.Empty, "value2");
2290 names = createdKey.GetValueNames ();
2291 Assert.IsNotNull (names, "#B1");
2292 Assert.AreEqual (1, names.Length, "#B2");
2293 Assert.IsNotNull (names [0], "#B3");
2294 Assert.AreEqual (string.Empty, names [0], "#B4");
2295 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2296 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2297 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2298 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2301 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2305 [Test] // SetValue (String, Object)
2306 public void SetValue1_Name_Empty ()
2308 string subKeyName = Guid.NewGuid ().ToString ();
2310 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2312 createdKey.SetValue (string.Empty, "value1");
2313 string [] names = createdKey.GetValueNames ();
2314 Assert.IsNotNull (names, "#A1");
2315 Assert.AreEqual (1, names.Length, "#A2");
2316 Assert.IsNotNull (names [0], "#A3");
2317 Assert.AreEqual (string.Empty, names [0], "#A4");
2318 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2319 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2320 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2321 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2323 createdKey.SetValue (null, "value2");
2324 names = createdKey.GetValueNames ();
2325 Assert.IsNotNull (names, "#B1");
2326 Assert.AreEqual (1, names.Length, "#B2");
2327 Assert.IsNotNull (names [0], "#B3");
2328 Assert.AreEqual (string.Empty, names [0], "#B4");
2329 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2330 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2331 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2332 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2335 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2339 [Test] // SetValue (String, Object)
2340 public void SetValue1_Name_MaxLength ()
2342 string subKeyName = Guid.NewGuid ().ToString ();
2345 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2346 string name = new string ('a', 254);
2348 createdKey.SetValue (name, "value1");
2349 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2350 createdKey.DeleteValue (name);
2351 Assert.IsNull (createdKey.GetValue (name), "#A2");
2353 name = new string ('a', 255);
2355 createdKey.SetValue (name, "value2");
2356 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2357 createdKey.DeleteValue (name);
2358 Assert.IsNull (createdKey.GetValue (name), "#B2");
2360 name = new string ('a', 256);
2363 createdKey.SetValue (name, "value2");
2364 Assert.Fail ("#C1");
2365 } catch (ArgumentException ex) {
2366 // 1.x: Registry subkeys should not be
2367 // greater than or equal to 255 characters
2369 // 2.x: Registry subkeys should not be
2370 // greater than 255 characters
2371 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2372 Assert.IsNull (ex.InnerException, "#C3");
2373 Assert.IsNotNull (ex.Message, "#C4");
2374 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2375 Assert.IsNull (ex.ParamName, "#C6");
2380 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2381 if (createdKey != null) {
2382 createdKey.Close ();
2383 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2390 [Test] // SetValue (String, Object)
2391 public void SetValue1_Value_Null ()
2393 string subKeyName = Guid.NewGuid ().ToString ();
2395 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2398 createdKey.SetValue ("Name", null);
2400 } catch (ArgumentNullException ex) {
2401 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2402 Assert.IsNull (ex.InnerException, "#3");
2403 Assert.IsNotNull (ex.Message, "#4");
2404 Assert.AreEqual ("value", ex.ParamName, "#5");
2408 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2412 [Test] // SetValue (String, Object)
2413 public void SetValue1_Boolean ()
2415 string subKeyName = Guid.NewGuid ().ToString ();
2418 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2419 // we created a new subkey, so value should not exist
2420 Assert.IsNull (createdKey.GetValue ("Installed"), "#A1");
2422 createdKey.SetValue ("Installed", true);
2424 object value = createdKey.GetValue ("Installed");
2425 // value should exist
2426 Assert.IsNotNull (value, "#A2");
2427 // type of value should be string
2428 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2429 // ensure value matches
2430 Assert.AreEqual (true.ToString (), value, "#A4");
2433 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2434 object value = openedKey.GetValue ("Installed");
2435 Assert.IsNotNull (value, "#B1");
2436 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2437 Assert.AreEqual (true.ToString (), value, "#B3");
2441 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2445 [Test] // SetValue (String, Object)
2446 public void SetValue1_Byte ()
2448 string subKeyName = Guid.NewGuid ().ToString ();
2451 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2452 // we created a new subkey, so value should not exist
2453 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2455 createdKey.SetValue ("Flags", (byte) 5);
2457 object value = createdKey.GetValue ("Flags");
2458 // value should exist
2459 Assert.IsNotNull (value, "#A2");
2460 // type of value should be string
2461 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2462 // ensure value matches
2463 Assert.AreEqual ("5", value, "#A4");
2466 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2467 object value = openedKey.GetValue ("Flags");
2468 Assert.IsNotNull (value, "#B1");
2469 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2470 Assert.AreEqual ("5", value, "#B3");
2474 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2478 [Test] // SetValue (String, Object)
2479 public void SetValue1_ByteArray ()
2481 string subKeyName = Guid.NewGuid ().ToString ();
2484 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2485 // we created a new subkey, so value should not exist
2486 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2488 createdKey.SetValue ("Flags", new byte [] { 1, 5 });
2490 object value = createdKey.GetValue ("Flags");
2491 // value should exist
2492 Assert.IsNotNull (value, "#A2");
2493 // type of value should be string
2494 Assert.AreEqual (typeof (byte []), value.GetType (), "#3");
2495 // ensure value matches
2496 Assert.AreEqual (new byte [] { 1, 5 }, value, "#4");
2499 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2500 object value = openedKey.GetValue ("Flags");
2501 Assert.IsNotNull (value, "#B1");
2502 Assert.AreEqual (typeof (byte []), value.GetType (), "#B2");
2503 Assert.AreEqual (new byte [] { 1, 5 }, value, "#B3");
2507 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2511 [Test] // SetValue (String, Object)
2512 public void SetValue1_DateTime ()
2514 string subKeyName = Guid.NewGuid ().ToString ();
2517 object rawValue = DateTime.Now;
2519 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2520 // we created a new subkey, so value should not exist
2521 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2523 createdKey.SetValue ("Path", rawValue);
2525 object value = createdKey.GetValue ("Path");
2526 // value should exist
2527 Assert.IsNotNull (value, "#A2");
2528 // type of value should be string
2529 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2530 // ensure value matches
2531 Assert.AreEqual (rawValue.ToString (), value, "#A4");
2534 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2535 object value = openedKey.GetValue ("Path");
2536 Assert.IsNotNull (value, "#B1");
2537 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2538 Assert.AreEqual (rawValue.ToString (), value, "#B3");
2542 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2547 public void SetValue_Int32 ()
2549 string subKeyName = Guid.NewGuid ().ToString ();
2552 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2553 // we created a new subkey, so value should not exist
2554 Assert.IsNull (createdKey.GetValue ("RefCount"), "#A1");
2556 createdKey.SetValue ("RefCount", 5);
2558 object value = createdKey.GetValue ("RefCount");
2559 // value should exist
2560 Assert.IsNotNull (value, "#A2");
2561 // type of value should be int
2562 Assert.AreEqual (typeof (int), value.GetType (), "#A3");
2563 // ensure value matches
2564 Assert.AreEqual (5, value, "#A4");
2567 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2568 object value = openedKey.GetValue ("RefCount");
2569 Assert.IsNotNull (value, "#B1");
2570 Assert.AreEqual (typeof (int), value.GetType (), "#B2");
2571 Assert.AreEqual (5, value, "#B3");
2575 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2579 [Test] // SetValue (String, Object)
2580 public void SetValue1_Int64 ()
2582 string subKeyName = Guid.NewGuid ().ToString ();
2585 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2586 // we created a new subkey, so value should not exist
2587 Assert.IsNull (createdKey.GetValue ("Ticks"), "#A1");
2589 createdKey.SetValue ("Ticks", 500L);
2591 object value = createdKey.GetValue ("Ticks");
2592 // value should exist
2593 Assert.IsNotNull (value, "#A2");
2594 // type of value should be string
2595 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2596 // ensure value matches
2597 Assert.AreEqual ("500", value, "#A4");
2600 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2601 object value = openedKey.GetValue ("Ticks");
2602 Assert.IsNotNull (value, "#B1");
2603 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2604 Assert.AreEqual ("500", value, "#B3");
2608 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2612 [Test] // SetValue (String, Object)
2613 public void SetValue1_String ()
2615 string subKeyName = Guid.NewGuid ().ToString ();
2618 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2619 // we created a new subkey, so value should not exist
2620 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2622 createdKey.SetValue ("Path", "/usr/lib/whatever");
2624 object path = createdKey.GetValue ("Path");
2625 // value should exist
2626 Assert.IsNotNull (path, "#A2");
2627 // type of value should be string
2628 Assert.AreEqual (typeof (string), path.GetType (), "#A3");
2629 // ensure value matches
2630 Assert.AreEqual ("/usr/lib/whatever", path, "#A4");
2633 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2634 object path = openedKey.GetValue ("Path");
2635 Assert.IsNotNull (path, "#B1");
2636 Assert.AreEqual (typeof (string), path.GetType (), "#B2");
2637 Assert.AreEqual ("/usr/lib/whatever", path, "#B3");
2641 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2645 [Test] // SetValue (String, Object)
2646 public void SetValue1_StringArray ()
2648 string subKeyName = Guid.NewGuid ().ToString ();
2651 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2652 // we created a new subkey, so value should not exist
2653 Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#A1");
2655 createdKey.SetValue ("DependsOnGroup", new string [] { "A", "B" });
2657 object value = createdKey.GetValue ("DependsOnGroup");
2658 // value should exist
2659 Assert.IsNotNull (value, "#A2");
2660 // type of value should be string
2661 Assert.AreEqual (typeof (string []), value.GetType (), "#A3");
2662 // ensure value matches
2663 Assert.AreEqual (new string [] { "A", "B" }, value, "#A4");
2666 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2667 object value = openedKey.GetValue ("DependsOnGroup");
2668 Assert.IsNotNull (value, "#B1");
2669 Assert.AreEqual (typeof (string []), value.GetType (), "#B2");
2670 Assert.AreEqual (new string [] { "A", "B" }, value, "#B3");
2674 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2678 [Test] // SetValue (String, Object)
2679 public void SetValue1_Key_ReadOnly ()
2681 string subKeyName = Guid.NewGuid ().ToString ();
2683 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2685 softwareKey.SetValue ("name1", "value1");
2687 } catch (UnauthorizedAccessException ex) {
2688 // Cannot write to the registry key
2689 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2690 Assert.IsNotNull (ex.Message, "#3");
2691 Assert.IsNull (ex.InnerException, "#4");
2695 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2697 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2700 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2702 createdKey.SetValue ("name1", "value1");
2704 } catch (UnauthorizedAccessException ex) {
2705 // Cannot write to the registry key
2706 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2707 Assert.IsNotNull (ex.Message, "#3");
2708 Assert.IsNull (ex.InnerException, "#4");
2713 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2714 if (createdKey != null) {
2715 createdKey.Close ();
2716 softwareKey.DeleteSubKeyTree (subKeyName);
2724 [Test] // SetValue (String, Object)
2725 public void SetValue1_Key_Removed ()
2727 string subKeyName = Guid.NewGuid ().ToString ();
2729 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2731 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2732 softwareKey.DeleteSubKeyTree (subKeyName);
2733 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2735 createdKey.SetValue ("name1", "value1");
2737 } catch (IOException ex) {
2738 // Illegal operation attempted on a registry key that
2739 // has been marked for deletion
2740 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2741 Assert.IsNotNull (ex.Message, "#4");
2742 Assert.IsNull (ex.InnerException, "#5");
2747 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2748 if (createdKey != null) {
2749 createdKey.Close ();
2750 softwareKey.DeleteSubKeyTree (subKeyName);
2758 [Test] // SetValue (String, Object, RegistryValueKind)
2759 public void SetValue2_Key_ReadOnly ()
2761 string subKeyName = Guid.NewGuid ().ToString ();
2763 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2765 softwareKey.SetValue ("name1", "value1",
2766 RegistryValueKind.String);
2768 } catch (UnauthorizedAccessException ex) {
2769 // Cannot write to the registry key
2770 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2771 Assert.IsNotNull (ex.Message, "#3");
2772 Assert.IsNull (ex.InnerException, "#4");
2776 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2778 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2781 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2783 createdKey.SetValue ("name1", "value1",
2784 RegistryValueKind.String);
2786 } catch (UnauthorizedAccessException ex) {
2787 // Cannot write to the registry key
2788 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2789 Assert.IsNotNull (ex.Message, "#3");
2790 Assert.IsNull (ex.InnerException, "#4");
2795 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2796 if (createdKey != null) {
2797 createdKey.Close ();
2798 softwareKey.DeleteSubKeyTree (subKeyName);
2806 [Test] // SetValue (String, Object, RegistryValueKind)
2807 public void SetValue2_Key_Removed ()
2809 string subKeyName = Guid.NewGuid ().ToString ();
2811 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2813 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2814 softwareKey.DeleteSubKeyTree (subKeyName);
2815 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2817 createdKey.SetValue ("name1", "value1",
2818 RegistryValueKind.String);
2820 } catch (IOException ex) {
2821 // Illegal operation attempted on a registry key that
2822 // has been marked for deletion
2823 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2824 Assert.IsNotNull (ex.Message, "#4");
2825 Assert.IsNull (ex.InnerException, "#5");
2830 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2831 if (createdKey != null) {
2832 createdKey.Close ();
2833 softwareKey.DeleteSubKeyTree (subKeyName);
2841 [Test] // SetValue (String, Object, RegistryValueKind)
2842 public void SetValue2_Name_Empty ()
2844 string subKeyName = Guid.NewGuid ().ToString ();
2846 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2848 createdKey.SetValue (string.Empty, "value1",
2849 RegistryValueKind.String);
2850 string [] names = createdKey.GetValueNames ();
2851 Assert.IsNotNull (names, "#A1");
2852 Assert.AreEqual (1, names.Length, "#A2");
2853 Assert.IsNotNull (names [0], "#A3");
2854 Assert.AreEqual (string.Empty, names [0], "#A4");
2855 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2856 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2857 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2858 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2860 createdKey.SetValue (null, "value2",
2861 RegistryValueKind.String);
2862 names = createdKey.GetValueNames ();
2863 Assert.IsNotNull (names, "#B1");
2864 Assert.AreEqual (1, names.Length, "#B2");
2865 Assert.IsNotNull (names [0], "#B3");
2866 Assert.AreEqual (string.Empty, names [0], "#B4");
2867 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2868 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2869 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2870 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2873 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2877 [Test] // SetValue (String, Object, RegistryValueKind)
2878 public void SetValue2_Name_MaxLength ()
2880 string subKeyName = Guid.NewGuid ().ToString ();
2883 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2884 string name = new string ('a', 254);
2886 createdKey.SetValue (name, "value1",
2887 RegistryValueKind.String);
2888 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2889 createdKey.DeleteValue (name);
2890 Assert.IsNull (createdKey.GetValue (name), "#A2");
2892 name = new string ('a', 255);
2894 createdKey.SetValue (name, "value2",
2895 RegistryValueKind.String);
2896 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2897 createdKey.DeleteValue (name);
2898 Assert.IsNull (createdKey.GetValue (name), "#B2");
2900 name = new string ('a', 256);
2903 createdKey.SetValue (name, "value2",
2904 RegistryValueKind.String);
2905 Assert.Fail ("#C1");
2906 } catch (ArgumentException ex) {
2907 // Registry subkeys should not be
2908 // greater than 255 characters
2909 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2910 Assert.IsNull (ex.InnerException, "#C3");
2911 Assert.IsNotNull (ex.Message, "#C4");
2912 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2913 Assert.IsNull (ex.ParamName, "#C6");
2918 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2919 if (createdKey != null) {
2920 createdKey.Close ();
2921 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2928 [Test] // SetValue (String, Object, RegistryValueKind)
2929 public void SetValue2_Name_Null ()
2931 string subKeyName = Guid.NewGuid ().ToString ();
2933 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2935 createdKey.SetValue (null, "value1",
2936 RegistryValueKind.String);
2937 string [] names = createdKey.GetValueNames ();
2938 Assert.IsNotNull (names, "#A1");
2939 Assert.AreEqual (1, names.Length, "#A2");
2940 Assert.IsNotNull (names [0], "#A3");
2941 Assert.AreEqual (string.Empty, names [0], "#A4");
2942 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2943 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2944 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2945 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2947 createdKey.SetValue (string.Empty, "value2",
2948 RegistryValueKind.String);
2949 names = createdKey.GetValueNames ();
2950 Assert.IsNotNull (names, "#B1");
2951 Assert.AreEqual (1, names.Length, "#B2");
2952 Assert.IsNotNull (names [0], "#B3");
2953 Assert.AreEqual (string.Empty, names [0], "#B4");
2954 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2955 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2956 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2957 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2960 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2964 [Test] // SetValue (String, Object, RegistryValueKind)
2965 public void SetValue2_Value_Null ()
2967 string subKeyName = Guid.NewGuid ().ToString ();
2969 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2972 createdKey.SetValue ("Name", null,
2973 RegistryValueKind.String);
2975 } catch (ArgumentNullException ex) {
2976 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2977 Assert.IsNull (ex.InnerException, "#3");
2978 Assert.IsNotNull (ex.Message, "#4");
2979 Assert.AreEqual ("value", ex.ParamName, "#5");
2983 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2988 public void SubKeyCount ()
2990 string subKeyName = Guid.NewGuid ().ToString ();
2993 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2994 // check if key was successfully created
2995 Assert.IsNotNull (createdKey, "#A1");
2996 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2999 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3000 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3003 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3005 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3006 Assert.IsNotNull (createdKey, "#B1");
3007 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3009 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3010 Assert.IsNotNull (createdKey2, "#B3");
3011 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
3012 createdKey2.DeleteSubKey ("monotemp1");
3013 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
3015 Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
3019 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3020 if (createdKey != null) {
3021 createdKey.Close ();
3022 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3030 public void SubKeyCount_Key_Removed ()
3032 string subKeyName = Guid.NewGuid ().ToString ();
3035 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3036 // check if key was successfully created
3037 Assert.IsNotNull (createdKey, "#A1");
3038 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3041 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3042 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3045 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3047 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3048 Assert.IsNotNull (createdKey, "#B1");
3049 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3051 // remove created key
3052 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3055 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
3056 } catch (IOException ex) {
3057 // Illegal operation attempted on a registry key that
3058 // has been marked for deletion
3059 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3060 Assert.IsNotNull (ex.Message, "#15");
3061 Assert.IsNull (ex.InnerException, "#16");
3066 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3067 if (createdKey != null) {
3068 createdKey.Close ();
3069 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3077 public void ValueCount ()
3079 string subKeyName = Guid.NewGuid ().ToString ();
3082 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3083 // check if key was successfully created
3084 Assert.IsNotNull (createdKey, "#A1");
3085 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3086 createdKey.SetValue ("name1", "value1");
3087 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3088 createdKey.SetValue ("name2", "value2");
3089 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3090 createdKey.SetValue ("name2", "value2b");
3091 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3092 createdKey.SetValue ("name3", "value3");
3093 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3095 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3096 Assert.IsNotNull (createdKey, "#B1");
3097 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3099 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3100 Assert.IsNotNull (createdKey2, "#B3");
3101 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
3102 createdKey2.DeleteValue ("name2");
3103 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
3105 Assert.AreEqual (2, createdKey.ValueCount, "#B6");
3109 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3110 if (createdKey != null) {
3111 createdKey.Close ();
3112 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3120 public void ValueCount_Key_Removed ()
3122 string subKeyName = Guid.NewGuid ().ToString ();
3125 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3126 // check if key was successfully created
3127 Assert.IsNotNull (createdKey, "#A1");
3128 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3129 createdKey.SetValue ("name1", "value1");
3130 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3131 createdKey.SetValue ("name2", "value2");
3132 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3133 createdKey.SetValue ("name2", "value2b");
3134 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3135 createdKey.SetValue ("name3", "value3");
3136 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3138 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3139 Assert.IsNotNull (createdKey, "#B1");
3140 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3142 // remove created key
3143 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3146 Assert.Fail ("#C1: " + createdKey.ValueCount);
3147 } catch (IOException ex) {
3148 // Illegal operation attempted on a registry key that
3149 // has been marked for deletion
3150 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3151 Assert.IsNotNull (ex.Message, "#15");
3152 Assert.IsNull (ex.InnerException, "#16");
3157 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3158 if (createdKey != null) {
3159 createdKey.Close ();
3160 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3168 public void bug79051 ()
3170 string subKeyName = Guid.NewGuid ().ToString ();
3172 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3174 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3175 createdKey.SetValue ("test", "whatever");
3176 createdKey.Close ();
3177 softwareKey.DeleteSubKeyTree (subKeyName);
3181 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3182 if (createdKey != null) {
3183 createdKey.Close ();
3184 softwareKey.DeleteSubKeyTree (subKeyName);
3194 public void TypeCastTests ()
3196 string subKeyName = Guid.NewGuid ().ToString ();
3197 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3199 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3200 createdKey.SetValue ("test-int", (int) 1, RegistryValueKind.DWord);
3201 createdKey.SetValue ("test-uint", (uint) 1, RegistryValueKind.DWord);
3202 createdKey.SetValue ("test-byte", (byte) 1, RegistryValueKind.DWord);
3203 createdKey.SetValue ("test-sbyte", (sbyte) 1, RegistryValueKind.DWord);
3204 createdKey.SetValue ("test-short", (short) 1, RegistryValueKind.DWord);
3205 createdKey.SetValue ("test-ushort", (ushort) 1, RegistryValueKind.DWord);
3206 createdKey.SetValue ("test-long", (long) 1, RegistryValueKind.DWord);
3207 createdKey.SetValue ("test-ulong", (ulong) 1, RegistryValueKind.DWord);
3208 createdKey.SetValue ("test-decimal", (decimal) 1, RegistryValueKind.DWord);
3209 createdKey.SetValue ("test-float", (float) 1, RegistryValueKind.DWord);
3210 createdKey.SetValue ("test-bool", true, RegistryValueKind.DWord);
3212 createdKey.SetValue ("dtest-int", (int) 1, RegistryValueKind.QWord);
3213 createdKey.SetValue ("dtest-uint", (uint) 1, RegistryValueKind.QWord);
3214 createdKey.SetValue ("dtest-byte", (byte) 1, RegistryValueKind.QWord);
3215 createdKey.SetValue ("dtest-sbyte", (sbyte) 1, RegistryValueKind.QWord);
3216 createdKey.SetValue ("dtest-short", (short) 1, RegistryValueKind.QWord);
3217 createdKey.SetValue ("dtest-ushort", (ushort) 1, RegistryValueKind.QWord);
3218 createdKey.SetValue ("dtest-long", (long) 1, RegistryValueKind.QWord);
3219 createdKey.SetValue ("dtest-ulong", (ulong) 1, RegistryValueKind.QWord);
3220 createdKey.SetValue ("dtest-decimal", (decimal) 1, RegistryValueKind.QWord);
3221 createdKey.SetValue ("dtest-float", (float) 1, RegistryValueKind.QWord);
3222 createdKey.SetValue ("dtest-bool", true, RegistryValueKind.QWord);
3224 object r = createdKey.GetValue ("test-int");
3225 Assert.AreEqual (r is int, true);
3226 Assert.AreEqual ((int) r, 1);
3228 r = createdKey.GetValue ("test-uint");
3229 Assert.AreEqual (r is int, true);
3230 Assert.AreEqual ((int) r, 1);
3231 r = createdKey.GetValue ("test-byte");
3232 Assert.AreEqual (r is int, true);
3233 Assert.AreEqual ((int) r, 1);
3234 r = createdKey.GetValue ("test-sbyte");
3235 Assert.AreEqual (r is int, true);
3236 Assert.AreEqual ((int) r, 1);
3237 r = createdKey.GetValue ("test-short");
3238 Assert.AreEqual (r is int, true);
3239 Assert.AreEqual ((int) r, 1);
3240 r = createdKey.GetValue ("test-ushort");
3241 Assert.AreEqual (r is int, true);
3242 Assert.AreEqual ((int) r, 1);
3243 r = createdKey.GetValue ("test-long");
3244 Assert.AreEqual (r is int, true);
3245 Assert.AreEqual ((int) r, 1);
3246 r = createdKey.GetValue ("test-ulong");
3247 Assert.AreEqual (r is int, true);
3248 Assert.AreEqual ((int) r, 1);
3250 r = createdKey.GetValue ("dtest-int");
3251 Assert.AreEqual (r is long, true);
3252 Assert.AreEqual ((long) r, 1);
3253 r = createdKey.GetValue ("dtest-uint");
3254 Assert.AreEqual (r is long, true);
3255 Assert.AreEqual ((long) r, 1);
3256 r = createdKey.GetValue ("dtest-byte");
3257 Assert.AreEqual (r is long, true);
3258 Assert.AreEqual ((long) r, 1);
3259 r = createdKey.GetValue ("dtest-sbyte");
3260 Assert.AreEqual (r is long, true);
3261 Assert.AreEqual ((long) r, 1);
3262 r = createdKey.GetValue ("dtest-short");
3263 Assert.AreEqual (r is long, true);
3264 Assert.AreEqual ((long) r, 1);
3265 r = createdKey.GetValue ("dtest-ushort");
3266 Assert.AreEqual (r is long, true);
3267 Assert.AreEqual ((long) r, 1);
3268 r = createdKey.GetValue ("dtest-long");
3269 Assert.AreEqual (r is long, true);
3270 Assert.AreEqual ((long) r, 1);
3271 r = createdKey.GetValue ("dtest-ulong");
3272 Assert.AreEqual (r is long, true);
3273 Assert.AreEqual ((long) r, 1);
3274 r = createdKey.GetValue ("dtest-decimal");
3275 Assert.IsTrue (r is long);
3276 Assert.AreEqual ((long) r, 1);
3277 r = createdKey.GetValue ("dtest-float");
3278 Assert.IsTrue (r is long);
3279 Assert.AreEqual ((long) r, 1);
3280 r = createdKey.GetValue ("dtest-bool");
3281 Assert.AreEqual (typeof (long), r.GetType ());
3284 createdKey.SetValue ("test-int", uint.MaxValue, RegistryValueKind.DWord);
3285 Assert.Fail ("#100");
3287 createdKey.SetValue ("test-int", ulong.MaxValue, RegistryValueKind.QWord);
3288 Assert.Fail ("#101");
3289 } catch (ArgumentException) {
3292 createdKey.Close ();
3293 softwareKey.DeleteSubKeyTree (subKeyName);
3297 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3298 if (createdKey != null) {
3299 createdKey.Close ();
3300 softwareKey.DeleteSubKeyTree (subKeyName);
3309 public void bug79059 ()
3311 string subKeyName = Guid.NewGuid ().ToString ();
3313 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3315 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3316 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
3318 createdKey.Close ();
3319 softwareKey.DeleteSubKeyTree (subKeyName);
3323 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3324 if (createdKey != null) {
3325 createdKey.Close ();
3326 softwareKey.DeleteSubKeyTree (subKeyName);
3335 public void bugnew1 ()
3337 string subKeyName = Guid.NewGuid ().ToString ();
3339 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3341 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3342 createdKey.SetValue ("name1", "value1");
3344 RegistryKey testKey = null;
3346 testKey = createdKey.OpenSubKey ("test", true);
3347 if (testKey == null)
3348 testKey = createdKey.CreateSubKey ("test");
3349 testKey.SetValue ("another", "one");
3351 if (testKey != null)
3355 createdKey.SetValue ("name2", "value2");
3356 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
3357 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
3358 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
3359 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
3361 string [] names = createdKey.GetValueNames ();
3362 Assert.IsNotNull (names, "#6");
3363 Assert.AreEqual (2, names.Length, "#7");
3364 Assert.AreEqual ("name1", names [0], "#8");
3365 Assert.AreEqual ("name2", names [1], "#9");
3367 softwareKey.DeleteSubKeyTree (subKeyName);
3369 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
3370 Assert.IsNull (openedKey, "#10");
3373 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
3374 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
3377 createdKey.GetValueNames ();
3378 Assert.Fail ("#13");
3379 } catch (IOException ex) {
3380 // Illegal operation attempted on a registry key that
3381 // has been marked for deletion
3382 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3383 Assert.IsNotNull (ex.Message, "#15");
3384 Assert.IsNull (ex.InnerException, "#16");
3388 createdKey.SetValue ("name1", "value1");
3389 Assert.Fail ("#17");
3390 } catch (IOException ex) {
3391 // Illegal operation attempted on a registry key that
3392 // has been marked for deletion
3393 Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
3394 Assert.IsNotNull (ex.Message, "#19");
3395 Assert.IsNull (ex.InnerException, "#20");
3399 createdKey.SetValue ("newname", "value1");
3400 Assert.Fail ("#21");
3401 } catch (IOException ex) {
3402 // Illegal operation attempted on a registry key that
3403 // has been marked for deletion
3404 Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
3405 Assert.IsNotNull (ex.Message, "#23");
3406 Assert.IsNull (ex.InnerException, "#24");
3409 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
3410 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
3411 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
3412 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
3415 createdKey.CreateSubKey ("new");
3416 Assert.Fail ("#29");
3417 } catch (IOException ex) {
3418 // Illegal operation attempted on a registry key that
3419 // has been marked for deletion
3420 Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
3421 Assert.IsNotNull (ex.Message, "#31");
3422 Assert.IsNull (ex.InnerException, "#32");
3427 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3428 if (createdKey != null) {
3429 createdKey.Close ();
3430 softwareKey.DeleteSubKeyTree (subKeyName);
3439 public void bugnew2 () // values cannot be written on registry root (hive)
3441 string [] names = Registry.CurrentUser.GetValueNames ();
3442 Assert.IsNotNull (names, "#1");
3443 Registry.CurrentUser.SetValue ("name1", "value1");
3444 Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
3445 Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
3446 string [] newNames = Registry.CurrentUser.GetValueNames ();
3447 Assert.IsNotNull (newNames, "#4");
3448 Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
3449 Registry.CurrentUser.DeleteValue ("name1");
3453 public void bugnew3 () // on Windows, key cannot be closed twice
3455 string subKeyName = Guid.NewGuid ().ToString ();
3458 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3459 createdKey.Close ();
3462 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3467 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3468 if (createdKey != null) {
3469 createdKey.Close ();
3470 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3478 public void bugnew4 () // Key cannot be flushed once it has been closed
3480 string subKeyName = Guid.NewGuid ().ToString ();
3483 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3484 createdKey.Close ();
3487 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3492 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3493 if (createdKey != null) {
3494 createdKey.Close ();
3495 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3502 private bool RunningOnUnix {
3504 int p = (int) Environment.OSVersion.Platform;
3505 return ((p == 4) || (p == 128) || (p == 6));