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");
660 // Unfortunately we can't test that the scenario where a volatile
661 // key is not alive after a reboot, but we can test other bits.
663 public void CreateSubKey_Volatile ()
665 RegistryKey key = null;
666 RegistryKey subkey = null;
667 string subKeyName = "VolatileKey";
670 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
671 subkey = key.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
674 key = Registry.CurrentUser.OpenSubKey (subKeyName);
675 subkey = key.OpenSubKey ("Child");
676 Assert.AreEqual (true, subkey != null, "#A1");
686 public void CreateSubKey_Volatile_Child ()
688 RegistryKey key = null;
689 RegistryKey subkey = null;
690 string subKeyName = "VolatileKey";
693 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
694 subkey = key.CreateSubKey ("Child"); // Non volatile child
695 Assert.Fail ("#Exc");
696 } catch (IOException) {
706 public void CreateSubKey_Volatile_Conflict ()
708 RegistryKey key = null;
709 RegistryKey key2 = null;
710 RegistryKey subkey = null;
711 string subKeyName = "VolatileKey";
715 // Create a volatile key and try to open it as a normal one
717 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
718 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
719 Assert.AreEqual (key.Name, key2.Name, "A0");
721 subkey = key2.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
722 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#A1");
723 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#A2");
730 // Create a non-volatile key and try to open it as a volatile one
732 subKeyName = "NonVolatileKey";
733 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
734 key2.SetValue ("Name", "Mono");
735 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
736 Assert.AreEqual (key.Name, key2.Name, "B0");
737 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#B1");
738 Assert.AreEqual ("Mono", key2.GetValue ("Name"), "#B2");
740 key.CreateSubKey ("Child");
741 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#B3");
742 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#B4");
745 // Close the non-volatile key and try to re-open it as a volatile one
749 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
750 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#C0");
751 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#C1");
763 public void DeleteSubKey_Volatile ()
765 RegistryKey key = null;
766 RegistryKey subkey = null;
767 string subKeyName = "VolatileKey";
770 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
771 key.CreateSubKey ("VolatileKeyChild", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
772 key.SetValue ("Name", "Mono");
775 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
777 key = Registry.CurrentUser.OpenSubKey (subKeyName);
778 Assert.AreEqual (null, key, "#A0");
787 // Define a normal key, and create a normal and a volatile key under it, and retrieve their names.
789 public void GetSubKeyNames_Volatile ()
791 RegistryKey key = null;
792 RegistryKey subkey = null;
793 string subKeyName = Guid.NewGuid ().ToString ();
794 string volChildKeyName = "volatilechildkey";
795 string childKeyName = "childkey";
798 key = Registry.CurrentUser.CreateSubKey (subKeyName);
799 key.CreateSubKey (volChildKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
800 key.CreateSubKey (childKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
803 key = Registry.CurrentUser.OpenSubKey (subKeyName);
804 string [] keyNames = key.GetSubKeyNames ();
806 // we can guarantee the order of the child keys, so we sort the two of them
807 Array.Sort (keyNames);
809 Assert.AreEqual (2, keyNames.Length, "#A0");
810 Assert.AreEqual (childKeyName, keyNames [0], "#A1");
811 Assert.AreEqual (volChildKeyName, keyNames [1], "#A2");
822 public void DeleteSubKey ()
824 string subKeyName = Guid.NewGuid ().ToString ();
827 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
828 // check if key was successfully created
829 Assert.IsNotNull (createdKey, "#1");
831 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
832 Assert.IsNotNull (createdKey, "#2");
833 Registry.CurrentUser.DeleteSubKey (subKeyName);
835 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
836 Assert.IsNull (createdKey, "#3");
840 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
841 if (createdKey != null) {
843 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
851 public void DeleteSubKey_Key_HasChildKeys ()
853 string subKeyName = Guid.NewGuid ().ToString ();
856 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
857 // check if key was successfully created
858 Assert.IsNotNull (createdKey, "#1");
859 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
863 Registry.CurrentUser.DeleteSubKey (subKeyName);
865 } catch (InvalidOperationException ex) {
866 // Registry key has subkeys and recursive removes are not
867 // supported by this method
868 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
869 Assert.IsNotNull (ex.Message, "#4");
870 Assert.IsNull (ex.InnerException, "#5");
874 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
875 if (createdKey != null) {
877 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
885 public void DeleteSubKey_Key_ReadOnly ()
887 string subKeyName = Guid.NewGuid ().ToString ();
890 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
891 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
895 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
897 softwareKey.DeleteSubKey (subKeyName);
899 } catch (UnauthorizedAccessException ex) {
900 // Cannot write to the registry key
901 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
902 Assert.IsNotNull (ex.Message, "#3");
903 Assert.IsNull (ex.InnerException, "#4");
908 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
909 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
910 if (createdKey != null) {
912 softwareKey.DeleteSubKeyTree (subKeyName);
921 public void DeleteSubKey_Key_DoesNotExist ()
923 string subKeyName = Guid.NewGuid ().ToString ();
926 Registry.CurrentUser.DeleteSubKey (subKeyName);
928 } catch (ArgumentException ex) {
929 // Cannot delete a subkey tree because the subkey does not exist
930 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
931 Assert.IsNull (ex.InnerException, "#A3");
932 Assert.IsNotNull (ex.Message, "#A4");
933 Assert.IsNull (ex.ParamName, "#A5");
937 Registry.CurrentUser.DeleteSubKey (subKeyName, true);
939 } catch (ArgumentException ex) {
940 // Cannot delete a subkey tree because the subkey does not exist
941 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
942 Assert.IsNull (ex.InnerException, "#B3");
943 Assert.IsNotNull (ex.Message, "#B4");
944 Assert.IsNull (ex.ParamName, "#B5");
947 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
951 public void DeleteSubKey_Key_Removed ()
953 string subKeyName = Guid.NewGuid ().ToString ();
956 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
957 // check if key was successfully created
958 Assert.IsNotNull (createdKey, "#1");
959 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
962 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
963 Assert.IsNotNull (createdKey, "#2");
964 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
965 Assert.IsNotNull (createdKey, "#3");
967 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
968 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
970 createdKey.DeleteSubKey ("monotemp");
972 } catch (ArgumentException ex) {
973 // Cannot delete a subkey tree because the subkey does
975 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
976 Assert.IsNull (ex.InnerException, "#7");
977 Assert.IsNotNull (ex.Message, "#8");
978 Assert.IsNull (ex.ParamName, "#9");
983 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
984 if (createdKey != null) {
986 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
994 [Category ("NotWorking")] // MS should not allow this
995 public void DeleteSubKey_Name_Empty ()
997 string subKeyName = Guid.NewGuid ().ToString ();
999 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1001 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1002 createdKey.DeleteSubKey (string.Empty);
1003 createdKey.Close ();
1005 createdKey = softwareKey.OpenSubKey (subKeyName);
1006 Assert.IsNull (createdKey, "#1");
1009 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1010 if (createdKey != null)
1011 createdKey.Close ();
1012 softwareKey.DeleteSubKeyTree (subKeyName);
1020 public void DeleteSubKey_Name_MaxLength ()
1022 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1023 string subKeyName = new string ('a', 254);
1025 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1026 createdKey.Close ();
1028 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1029 Assert.IsNotNull (createdKey, "#A1");
1031 softwareKey.DeleteSubKey (subKeyName);
1032 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1033 Assert.IsNull (createdKey, "#A2");
1036 subKeyName = new string ('a', 256);
1039 softwareKey.DeleteSubKey (subKeyName);
1040 Assert.Fail ("#B1");
1041 } catch (ArgumentException ex) {
1042 // 1.x: Registry subkeys should not be
1043 // greater than or equal to 255 characters
1045 // 2.x: Registry subkeys should not be
1046 // greater than 255 characters
1047 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1048 Assert.IsNull (ex.InnerException, "#B3");
1049 Assert.IsNotNull (ex.Message, "#B4");
1050 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1051 Assert.IsNull (ex.ParamName, "#B6");
1057 public void DeleteSubKey_Name_Null ()
1059 string subKeyName = Guid.NewGuid ().ToString ();
1061 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1063 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1065 createdKey.DeleteSubKey (null);
1067 } catch (ArgumentNullException ex) {
1068 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1069 Assert.IsNull (ex.InnerException, "#3");
1070 Assert.IsNotNull (ex.Message, "#4");
1071 Assert.AreEqual ("name", ex.ParamName, "#5");
1075 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1076 if (createdKey != null)
1077 createdKey.Close ();
1078 softwareKey.DeleteSubKeyTree (subKeyName);
1086 public void DeleteSubKeyTree ()
1089 // - remove key with subkeys
1090 // - remove key of which some subkeys are marked for deletion
1091 // - remove key with values
1095 public void DeleteSubKeyTree_Key_DoesNotExist ()
1097 // Cannot delete a subkey tree because the subkey does not exist
1098 string subKeyName = Guid.NewGuid ().ToString ();
1100 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1102 } catch (ArgumentException ex) {
1103 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1104 Assert.IsNull (ex.InnerException, "#3");
1105 Assert.IsNotNull (ex.Message, "#4");
1106 Assert.IsNull (ex.ParamName, "#5");
1111 public void DeleteSubKeyTree_Key_DoesNotExist_Overload ()
1113 // Cannot delete a subkey tree because the subkey does not exist
1114 string subKeyName = Guid.NewGuid ().ToString ();
1116 Registry.CurrentUser.DeleteSubKeyTree (subKeyName, true);
1118 } catch (ArgumentException ex) {
1119 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1120 Assert.IsNull (ex.InnerException, "#3");
1121 Assert.IsNotNull (ex.Message, "#4");
1122 Assert.IsNull (ex.ParamName, "#5");
1125 // It's enough to know this line is not throwing an exception.
1126 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
1130 public void DeleteSubKeyTree_Key_ReadOnly ()
1132 string subKeyName = Guid.NewGuid ().ToString ();
1135 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1136 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1137 createdKey.Close ();
1140 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
1142 softwareKey.DeleteSubKeyTree (subKeyName);
1144 } catch (UnauthorizedAccessException ex) {
1145 // Cannot write to the registry key
1146 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1147 Assert.IsNotNull (ex.Message, "#3");
1148 Assert.IsNull (ex.InnerException, "#4");
1153 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1154 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1155 if (createdKey != null)
1156 createdKey.Close ();
1157 softwareKey.DeleteSubKeyTree (subKeyName);
1165 public void DeleteSubKeyTree_Key_Removed ()
1167 string subKeyName = Guid.NewGuid ().ToString ();
1170 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1171 // check if key was successfully created
1172 Assert.IsNotNull (createdKey, "#1");
1173 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
1176 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1177 Assert.IsNotNull (createdKey, "#2");
1178 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
1179 Assert.IsNotNull (createdKey, "#3");
1181 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1182 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
1184 createdKey.DeleteSubKeyTree ("monotemp");
1186 } catch (ArgumentException ex) {
1187 // Cannot delete a subkey tree because the subkey does
1189 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
1190 Assert.IsNull (ex.InnerException, "#7");
1191 Assert.IsNotNull (ex.Message, "#8");
1192 Assert.IsNull (ex.ParamName, "#9");
1197 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1198 if (createdKey != null) {
1199 createdKey.Close ();
1200 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1208 [Category ("NotWorking")] // MS should not allow this
1209 public void DeleteSubKeyTree_Name_Empty ()
1211 string subKeyName = Guid.NewGuid ().ToString ();
1213 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1215 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1216 createdKey.DeleteSubKeyTree (string.Empty);
1217 createdKey.Close ();
1219 createdKey = softwareKey.OpenSubKey (subKeyName);
1220 Assert.IsNull (createdKey, "#1");
1223 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1224 if (createdKey != null)
1225 createdKey.Close ();
1226 softwareKey.DeleteSubKeyTree (subKeyName);
1234 public void DeleteSubKeyTree_Name_MaxLength ()
1236 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1237 string subKeyName = new string ('a', 254);
1239 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1240 createdKey.Close ();
1242 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1243 Assert.IsNotNull (createdKey, "#A1");
1245 softwareKey.DeleteSubKeyTree (subKeyName);
1246 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1247 Assert.IsNull (createdKey, "#A2");
1250 subKeyName = new string ('a', 256);
1253 softwareKey.DeleteSubKeyTree (subKeyName);
1254 Assert.Fail ("#B1");
1255 } catch (ArgumentException ex) {
1256 // 1.x: Registry subkeys should not be
1257 // greater than or equal to 255 characters
1259 // 2.x: Registry subkeys should not be
1260 // greater than 255 characters
1261 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1262 Assert.IsNull (ex.InnerException, "#B3");
1263 Assert.IsNotNull (ex.Message, "#B4");
1264 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1265 Assert.IsNull (ex.ParamName, "#B6");
1271 public void DeleteSubKeyTree_Name_Null ()
1273 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1275 softwareKey.DeleteSubKeyTree (null);
1277 } catch (ArgumentNullException ex) {
1278 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1279 Assert.IsNull (ex.InnerException, "#3");
1280 Assert.IsNotNull (ex.Message, "#4");
1281 Assert.AreEqual ("name", ex.ParamName, "#5");
1287 public void DeleteValue ()
1289 string subKeyName = Guid.NewGuid ().ToString ();
1292 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1293 // check if key was successfully created
1294 Assert.IsNotNull (createdKey, "#A1");
1295 createdKey.SetValue ("name1", "value1");
1296 createdKey.SetValue ("name2", "value2");
1297 string [] names = createdKey.GetValueNames ();
1298 Assert.IsNotNull (names, "#A2");
1299 Assert.AreEqual (2, names.Length, "#A3");
1300 Assert.IsNotNull (names [0], "#A4");
1301 Assert.AreEqual ("name1", names [0], "#A5");
1302 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A6");
1303 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A7");
1304 Assert.AreEqual ("name2", names [1], "#A8");
1305 Assert.IsNotNull (createdKey.GetValue ("name2"), "#A9");
1306 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#A10");
1308 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1309 Assert.IsNotNull (createdKey, "#B1");
1310 createdKey.DeleteValue ("name1");
1311 string [] names = createdKey.GetValueNames ();
1312 Assert.IsNotNull (names, "#B2");
1313 Assert.AreEqual (1, names.Length, "#B3");
1314 Assert.IsNotNull (names [0], "#B4");
1315 Assert.AreEqual ("name2", names [0], "#B5");
1316 Assert.IsNotNull (createdKey.GetValue ("name2"), "#B6");
1317 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#B7");
1318 createdKey.DeleteValue (new string ('a', 400), false);
1320 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1321 string [] names = createdKey.GetValueNames ();
1322 Assert.IsNotNull (names, "#C1");
1323 Assert.AreEqual (1, names.Length, "#C2");
1324 Assert.IsNotNull (names [0], "#C3");
1325 Assert.AreEqual ("name2", names [0], "#C4");
1326 Assert.IsNotNull (createdKey.GetValue ("name2"), "#C5");
1327 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#C6");
1331 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1332 if (createdKey != null) {
1333 createdKey.Close ();
1334 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1342 public void DeleteValue_Key_ReadOnly ()
1344 string subKeyName = Guid.NewGuid ().ToString ();
1347 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1348 createdKey.SetValue ("name1", "value1");
1351 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1353 // deleting value that exists
1354 createdKey.DeleteValue ("name1");
1355 Assert.Fail ("#A1");
1356 } catch (UnauthorizedAccessException ex) {
1357 // Cannot write to the registry key
1358 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#A2");
1359 Assert.IsNotNull (ex.Message, "#A3");
1360 Assert.IsNull (ex.InnerException, "#A4");
1364 // deleting value that exists
1365 createdKey.DeleteValue ("name1", true);
1366 Assert.Fail ("#B1");
1367 } catch (UnauthorizedAccessException ex) {
1368 // Cannot write to the registry key
1369 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#B2");
1370 Assert.IsNotNull (ex.Message, "#B3");
1371 Assert.IsNull (ex.InnerException, "#B4");
1375 // deleting value that exists
1376 createdKey.DeleteValue ("name1", false);
1377 Assert.Fail ("#C1");
1378 } catch (UnauthorizedAccessException ex) {
1379 // Cannot write to the registry key
1380 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#C2");
1381 Assert.IsNotNull (ex.Message, "#C3");
1382 Assert.IsNull (ex.InnerException, "#C4");
1386 // deleting value that does not exist
1387 createdKey.DeleteValue ("name2");
1388 Assert.Fail ("#D1");
1389 } catch (UnauthorizedAccessException ex) {
1390 // Cannot write to the registry key
1391 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#D2");
1392 Assert.IsNotNull (ex.Message, "#D3");
1393 Assert.IsNull (ex.InnerException, "#D4");
1397 // deleting value that does not exist
1398 createdKey.DeleteValue ("name2", true);
1399 Assert.Fail ("#E1");
1400 } catch (UnauthorizedAccessException ex) {
1401 // Cannot write to the registry key
1402 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#E2");
1403 Assert.IsNotNull (ex.Message, "#E3");
1404 Assert.IsNull (ex.InnerException, "#E4");
1408 // deleting value that does not exist
1409 createdKey.DeleteValue ("name2", false);
1410 Assert.Fail ("#F1");
1411 } catch (UnauthorizedAccessException ex) {
1412 // Cannot write to the registry key
1413 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#F2");
1414 Assert.IsNotNull (ex.Message, "#F3");
1415 Assert.IsNull (ex.InnerException, "#F4");
1420 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1421 if (createdKey != null) {
1422 createdKey.Close ();
1423 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1431 public void DeleteValue_Key_Removed ()
1433 string subKeyName = Guid.NewGuid ().ToString ();
1436 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1437 // check if key was successfully created
1438 Assert.IsNotNull (createdKey, "#1");
1439 createdKey.SetValue ("name1", "value1");
1441 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1442 Assert.IsNotNull (createdKey, "#2");
1443 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1444 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#3");
1446 createdKey.DeleteValue ("name1");
1447 createdKey.DeleteValue ("name1", true);
1448 createdKey.DeleteValue ("name1", false);
1452 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1453 if (createdKey != null) {
1454 createdKey.Close ();
1455 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1463 public void DeleteValue_Value_DoesNotExist ()
1465 string subKeyName = Guid.NewGuid ().ToString ();
1468 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1469 // check if key was successfully created
1470 Assert.IsNotNull (createdKey, "#A1");
1471 createdKey.SetValue ("name1", "value1");
1474 createdKey.DeleteValue ("name2");
1475 Assert.Fail ("#B1");
1476 } catch (ArgumentException ex) {
1477 // No value exists with that name
1478 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1479 Assert.IsNull (ex.InnerException, "#B3");
1480 Assert.IsNotNull (ex.Message, "#B4");
1481 Assert.IsNull (ex.ParamName, "#B5");
1485 createdKey.DeleteValue ("name2", true);
1486 Assert.Fail ("#C1");
1487 } catch (ArgumentException ex) {
1488 // No value exists with that name
1489 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1490 Assert.IsNull (ex.InnerException, "#C3");
1491 Assert.IsNotNull (ex.Message, "#C4");
1492 Assert.IsNull (ex.ParamName, "#C5");
1495 createdKey.DeleteValue ("name2", false);
1499 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1500 if (createdKey != null) {
1501 createdKey.Close ();
1502 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1510 public void DeleteValue_Name_Empty ()
1512 string subKeyName = Guid.NewGuid ().ToString ();
1515 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1516 createdKey.SetValue ("name1", "value1");
1517 createdKey.SetValue (string.Empty, "value2");
1519 string [] names = createdKey.GetValueNames ();
1520 Assert.IsNotNull (names, "#A1");
1521 Assert.AreEqual (2, names.Length, "#A2");
1522 Assert.IsNotNull (names [0], "#A3");
1524 Assert.AreEqual ("name1", names [0], "#A4");
1526 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1527 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1528 Assert.IsNotNull (names [1], "#A7");
1530 Assert.AreEqual (string.Empty, names [1], "#A8");
1532 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A9");
1533 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#A10");
1535 createdKey.DeleteValue (string.Empty);
1537 names = createdKey.GetValueNames ();
1538 Assert.IsNotNull (names, "#B1");
1539 Assert.AreEqual (1, names.Length, "#B2");
1540 Assert.IsNotNull (names [0], "#B3");
1541 Assert.AreEqual ("name1", names [0], "#B4");
1542 Assert.IsNotNull (createdKey.GetValue ("name1"), "#B5");
1543 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#B6");
1546 createdKey.DeleteValue (string.Empty);
1547 Assert.Fail ("#C1");
1548 } catch (ArgumentException ex) {
1549 // No value exists with that name
1550 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1551 Assert.IsNull (ex.InnerException, "#C3");
1552 Assert.IsNotNull (ex.Message, "#C4");
1553 Assert.IsNull (ex.ParamName, "#C5");
1557 createdKey.DeleteValue (string.Empty, true);
1558 Assert.Fail ("#D1");
1559 } catch (ArgumentException ex) {
1560 // No value exists with that name
1561 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
1562 Assert.IsNull (ex.InnerException, "#D3");
1563 Assert.IsNotNull (ex.Message, "#D4");
1564 Assert.IsNull (ex.ParamName, "#D5");
1567 createdKey.DeleteValue (string.Empty, false);
1569 names = createdKey.GetValueNames ();
1570 Assert.IsNotNull (names, "#E1");
1571 Assert.AreEqual (1, names.Length, "#E2");
1572 Assert.IsNotNull (names [0], "#E3");
1573 Assert.AreEqual ("name1", names [0], "#E4");
1574 Assert.IsNotNull (createdKey.GetValue ("name1"), "#E5");
1575 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#E6");
1579 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1580 if (createdKey != null) {
1581 createdKey.Close ();
1582 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1590 public void DeleteValue_Name_Null ()
1592 string subKeyName = Guid.NewGuid ().ToString ();
1595 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1596 createdKey.SetValue ("name1", "value1");
1597 createdKey.SetValue (null, "value2");
1599 string [] names = createdKey.GetValueNames ();
1600 Assert.IsNotNull (names, "#A1");
1601 Assert.AreEqual (2, names.Length, "#A2");
1602 Assert.IsNotNull (names [0], "#A3");
1604 Assert.AreEqual ("name1", names [0], "#A4");
1606 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1607 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1608 Assert.IsNotNull (names [1], "#A7");
1610 Assert.AreEqual (string.Empty, names [1], "#A8");
1612 Assert.IsNotNull (createdKey.GetValue (null), "#A9");
1613 Assert.AreEqual ("value2", createdKey.GetValue (null), "#A10");
1616 createdKey.DeleteValue (null);
1617 Assert.Fail ("#B1");
1618 } catch (ArgumentNullException ex) {
1619 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1620 Assert.IsNull (ex.InnerException, "#B3");
1621 Assert.IsNotNull (ex.Message, "#B4");
1622 Assert.AreEqual ("name", ex.ParamName, "#B5");
1626 createdKey.DeleteValue (null, true);
1627 Assert.Fail ("#C1");
1628 } catch (ArgumentNullException ex) {
1629 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
1630 Assert.IsNull (ex.InnerException, "#C3");
1631 Assert.IsNotNull (ex.Message, "#C4");
1632 Assert.AreEqual ("name", ex.ParamName, "#C5");
1636 createdKey.DeleteValue (null, false);
1637 Assert.Fail ("#D1");
1638 } catch (ArgumentNullException ex) {
1639 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
1640 Assert.IsNull (ex.InnerException, "#D3");
1641 Assert.IsNotNull (ex.Message, "#D4");
1642 Assert.AreEqual ("name", ex.ParamName, "#D5");
1647 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1648 if (createdKey != null) {
1649 createdKey.Close ();
1650 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1657 [DllImport ("advapi32.dll", CharSet = CharSet.Unicode)]
1658 static extern int RegOpenKeyEx (IntPtr keyBase, string keyName, IntPtr reserved, int access, out IntPtr keyHandle);
1660 const int RegCurrentUserHive = -2147483647;
1661 const int RegAccessRead = 0x00020019;
1663 // FromHandle is specially designed to retrieve a RegistryKey instance based on a IntPtr
1664 // retrieved using any unmanaged registry function, so we use directly RegOpenKeyEx to load
1665 // our handle and then pass it to the new 4.0 method.
1667 public void FromHandle ()
1669 // Not supported on Unix
1671 Assert.Ignore ("Running on Unix.");
1673 string subKeyName = Guid.NewGuid ().ToString ();
1675 using (RegistryKey key = Registry.CurrentUser.CreateSubKey (subKeyName))
1676 key.SetValue ("Name", "Mono001");
1679 int res = RegOpenKeyEx (new IntPtr (RegCurrentUserHive), subKeyName, IntPtr.Zero, RegAccessRead, out handle);
1681 using (RegistryKey key = RegistryKey.FromHandle (new SafeRegistryHandle (handle, true))) {
1682 Assert.AreEqual (String.Empty, key.Name, "#A0");
1683 Assert.AreEqual ("Mono001", key.GetValue ("Name"), "#A1");
1686 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1687 if (createdKey != null) {
1688 createdKey.Close ();
1689 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1695 public void FromHandle_InvalidHandle ()
1697 // Not supported on Unix
1699 Assert.Ignore ("Running on Unix.");
1701 string subKeyName = Guid.NewGuid ().ToString ();
1703 using (RegistryKey key = RegistryKey.FromHandle (new SafeRegistryHandle (IntPtr.Zero, true))) {
1704 Assert.AreEqual (String.Empty, key.Name, "#A0");
1706 // Any operation should throw a IOException, since even if we have a RegistryKey instance,
1707 // the handle is not valid.
1708 key.CreateSubKey ("ChildSubKey");
1709 Assert.Fail ("#Exc0");
1711 } catch (IOException) {
1713 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1714 if (createdKey != null) {
1715 createdKey.Close ();
1716 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1722 public void Handle ()
1725 Assert.Ignore ("Running on Unix.");
1727 string subKeyName = Guid.NewGuid ().ToString ();
1728 RegistryKey subkey = null;
1730 subkey = Registry.CurrentUser.CreateSubKey (subKeyName);
1731 Assert.AreEqual (true, subkey.Handle != null, "#A0");
1732 Assert.AreEqual (false, subkey.Handle.IsClosed, "#A1");
1733 Assert.AreEqual (false, subkey.Handle.IsInvalid, "#A2");
1737 if (subkey.Handle != null)
1738 Console.WriteLine (); // Avoids a warning at compile time
1739 Assert.Fail ("#Disposed");
1740 } catch (ObjectDisposedException) {
1744 if (subkey != null) {
1746 Registry.CurrentUser.DeleteSubKeyTree (subKeyName, false);
1752 public void GetValue ()
1754 string subKeyName = Guid.NewGuid ().ToString ();
1757 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1758 createdKey.SetValue ("name1", "value1");
1759 createdKey.SetValue ("name2", "value2");
1762 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1763 Assert.IsNotNull (createdKey.GetValue ("name1"), "#1");
1764 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#2");
1765 Assert.IsNotNull (createdKey.GetValue ("name2"), "#3");
1766 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#4");
1767 Assert.IsNull (createdKey.GetValue ("name3"), "#5");
1768 Assert.AreEqual ("value3", createdKey.GetValue ("name3", "value3"), "#6");
1769 Assert.IsNull (createdKey.GetValue ("name3", null), "#7");
1770 Assert.IsNull (createdKey.GetValue (new string ('a', 400)), "#8");
1774 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1775 if (createdKey != null) {
1776 createdKey.Close ();
1777 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1785 public void GetValue_Key_Removed ()
1787 string subKeyName = Guid.NewGuid ().ToString ();
1790 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1791 createdKey.SetValue ("name1", "value1");
1792 createdKey.SetValue ("name2", "value2");
1795 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1796 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1798 Assert.IsNull (createdKey.GetValue ("name1"), "#1");
1799 Assert.IsNotNull (createdKey.GetValue ("name1", "default"), "#2");
1800 Assert.AreEqual ("default", createdKey.GetValue ("name1", "default"), "#3");
1801 Assert.IsNull (createdKey.GetValue ("name3"), "#3");
1802 Assert.IsNotNull (createdKey.GetValue ("name3", "default"), "#4");
1803 Assert.AreEqual ("default", createdKey.GetValue ("name3", "default"), "#5");
1807 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1808 if (createdKey != null) {
1809 createdKey.Close ();
1810 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1818 public void GetValue_Name_Empty ()
1820 string subKeyName = Guid.NewGuid ().ToString ();
1823 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1824 createdKey.SetValue ("name1", "value1");
1825 createdKey.SetValue ("name2", "value2");
1827 Assert.IsNull (createdKey.GetValue (string.Empty), "#A1");
1828 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#A2");
1829 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#A3");
1830 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#A4");
1833 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1834 Assert.IsNull (createdKey.GetValue (string.Empty), "#B1");
1835 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#B2");
1836 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#B3");
1837 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#B4");
1840 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1841 createdKey.SetValue (string.Empty, "value1");
1842 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#C1");
1843 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#C2");
1844 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#C3");
1845 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#C4");
1848 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1849 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#D1");
1850 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#D2");
1851 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#D3");
1852 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#D4");
1856 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1857 if (createdKey != null) {
1858 createdKey.Close ();
1859 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1867 public void GetValue_Name_Null ()
1869 string subKeyName = Guid.NewGuid ().ToString ();
1872 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1873 createdKey.SetValue ("name1", "value1");
1874 createdKey.SetValue ("name2", "value2");
1876 Assert.IsNull (createdKey.GetValue (null), "#A1");
1877 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#A2");
1878 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#A3");
1879 Assert.IsNull (createdKey.GetValue (null, null), "#A4");
1882 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1883 Assert.IsNull (createdKey.GetValue (null), "#B1");
1884 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#B2");
1885 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#B3");
1886 Assert.IsNull (createdKey.GetValue (null, null), "#B4");
1889 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1890 createdKey.SetValue (string.Empty, "value1");
1891 Assert.IsNotNull (createdKey.GetValue (null), "#C1");
1892 Assert.AreEqual ("value1", createdKey.GetValue (null), "#C2");
1893 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#C3");
1894 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#C4");
1897 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1898 Assert.IsNotNull (createdKey.GetValue (null), "#D1");
1899 Assert.AreEqual ("value1", createdKey.GetValue (null), "#D2");
1900 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#D3");
1901 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#D4");
1905 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1906 if (createdKey != null) {
1907 createdKey.Close ();
1908 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1916 public void GetValue_Expand ()
1918 string subKeyName = Guid.NewGuid ().ToString ();
1921 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1922 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1923 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1925 createdKey.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1926 RegistryValueKind.ExpandString);
1927 createdKey.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1928 createdKey.SetValue ("name3", "just some text",
1929 RegistryValueKind.ExpandString);
1931 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#A1");
1932 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#A2");
1933 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#A3");
1934 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1935 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A4");
1936 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1937 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A5");
1938 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1939 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A6");
1940 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1941 null, RegistryValueOptions.None), "#A7");
1942 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1943 null, RegistryValueOptions.None), "#A8");
1944 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1945 null, RegistryValueOptions.None), "#A9");
1947 Environment.SetEnvironmentVariable ("MONO_TEST1", "789");
1948 Environment.SetEnvironmentVariable ("MONO_TEST2", "666");
1950 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#B1");
1951 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#B2");
1952 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#B3");
1953 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1954 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B4");
1955 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1956 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B5");
1957 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1958 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B6");
1959 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1960 null, RegistryValueOptions.None), "#B7");
1961 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1962 null, RegistryValueOptions.None), "#B8");
1963 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1964 null, RegistryValueOptions.None), "#B9");
1966 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1967 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#C1");
1968 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#C2");
1969 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#C3");
1970 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1971 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C4");
1972 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1973 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C5");
1974 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1975 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C6");
1976 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1977 null, RegistryValueOptions.None), "#C7");
1978 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1979 null, RegistryValueOptions.None), "#C8");
1980 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1981 null, RegistryValueOptions.None), "#C9");
1983 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1984 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1986 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#D1");
1987 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#D2");
1988 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#D3");
1989 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1990 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D4");
1991 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1992 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D5");
1993 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1994 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D6");
1995 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1996 null, RegistryValueOptions.None), "#D7");
1997 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1998 null, RegistryValueOptions.None), "#D8");
1999 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
2000 null, RegistryValueOptions.None), "#D9");
2004 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2005 if (createdKey != null) {
2006 createdKey.Close ();
2007 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2015 public void GetValueNames ()
2017 string subKeyName = Guid.NewGuid ().ToString ();
2020 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2021 string [] names = createdKey.GetValueNames ();
2022 Assert.IsNotNull (names, "#A1");
2023 Assert.AreEqual (0, names.Length, "#A2");
2025 createdKey.SetValue ("name1", "value1");
2026 createdKey.SetValue ("name2", "value2");
2027 createdKey.SetValue ("namelong", "value3");
2028 createdKey.SetValue ("name3", "value4");
2030 Assert.AreEqual (4, createdKey.ValueCount, "#B1");
2031 names = createdKey.GetValueNames ();
2032 Assert.IsNotNull (names, "#B2");
2033 Assert.AreEqual (4, names.Length, "#B3");
2036 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2037 string [] names = createdKey.GetValueNames ();
2038 Assert.IsNotNull (names, "#C1");
2039 Assert.AreEqual (4, names.Length, "#C2");
2041 // Mono's Unix registry API uses a hashtable to store the
2042 // values (and their names), so names are not returned in
2045 // to test whether the names returned by GetValueNames
2046 // match what we expect, we use these names to remove the
2047 // the values from the created keys and such we should end
2048 // up with zero values
2049 for (int i = 0; i < names.Length; i++) {
2050 string valueName = names [i];
2051 createdKey.DeleteValue (valueName);
2054 // all values should be removed now
2055 Assert.AreEqual (0, createdKey.ValueCount, "#C3");
2059 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2060 if (createdKey != null) {
2061 createdKey.Close ();
2062 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2070 public void GetValueNames_Key_Removed ()
2072 string subKeyName = Guid.NewGuid ().ToString ();
2075 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2076 createdKey.SetValue ("name1", "value1");
2077 createdKey.SetValue ("name2", "value2");
2079 string [] names = createdKey.GetValueNames ();
2080 Assert.IsNotNull (names, "#A1");
2081 Assert.AreEqual (2, names.Length, "#A2");
2084 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2085 string [] names = createdKey.GetValueNames ();
2086 Assert.IsNotNull (names, "#B1");
2087 Assert.AreEqual (2, names.Length, "#B2");
2089 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2092 createdKey.GetValueNames ();
2093 Assert.Fail ("#C1");
2094 } catch (IOException ex) {
2095 // Illegal operation attempted on a registry key that
2096 // has been marked for deletion
2097 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
2098 Assert.IsNotNull (ex.Message, "#C3");
2099 Assert.IsNull (ex.InnerException, "#C4");
2104 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2105 if (createdKey != null) {
2106 createdKey.Close ();
2107 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2114 [Test] // bug #78519
2115 public void GetSubKeyNamesTest ()
2117 string subKeyName = Guid.NewGuid ().ToString ();
2119 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2121 // check if key was successfully created
2122 Assert.IsNotNull (createdKey, "#A");
2124 RegistryKey subKey = createdKey.CreateSubKey ("foo");
2125 Assert.IsNotNull (subKey, "#B1");
2126 Assert.AreEqual (1, createdKey.SubKeyCount, "#B2");
2127 string[] subKeyNames = createdKey.GetSubKeyNames ();
2128 Assert.IsNotNull (subKeyNames, "#B3");
2129 Assert.AreEqual (1, subKeyNames.Length, "#B4");
2130 Assert.AreEqual ("foo", subKeyNames[0], "#B5");
2132 subKey = createdKey.CreateSubKey ("longfoo");
2133 Assert.IsNotNull (subKey, "#C1");
2134 Assert.AreEqual (2, createdKey.SubKeyCount, "#C2");
2135 subKeyNames = createdKey.GetSubKeyNames ();
2136 Assert.IsNotNull (subKeyNames, "#C3");
2137 Assert.AreEqual (2, subKeyNames.Length, "#C4");
2138 Assert.AreEqual ("foo", subKeyNames [0], "#C5");
2139 Assert.AreEqual ("longfoo", subKeyNames [1], "#C6");
2141 subKey = createdKey.CreateSubKey ("sfoo");
2142 Assert.IsNotNull (subKey, "#D1");
2143 Assert.AreEqual (3, createdKey.SubKeyCount, "#D2");
2144 subKeyNames = createdKey.GetSubKeyNames ();
2145 Assert.IsNotNull (subKeyNames, "#D3");
2146 Assert.AreEqual (3, subKeyNames.Length, "#D4");
2147 Assert.AreEqual ("foo", subKeyNames [0], "#D5");
2148 Assert.AreEqual ("longfoo", subKeyNames [1], "#D6");
2149 Assert.AreEqual ("sfoo", subKeyNames [2], "#D7");
2151 foreach (string name in subKeyNames) {
2152 createdKey.DeleteSubKeyTree (name);
2154 Assert.AreEqual (0, createdKey.SubKeyCount, "#E");
2157 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2162 public void OpenRemoteBaseKey ()
2164 // access to registry of remote machines is not implemented on unix
2166 Assert.Ignore ("Running on Unix.");
2168 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
2169 RegistryHive.CurrentUser, Environment.MachineName);
2170 Assert.IsNotNull (hive, "#1");
2172 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
2173 Assert.IsNotNull (key, "#2");
2180 public void OpenRemoteBaseKey_MachineName_Null ()
2183 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser, null);
2185 } catch (ArgumentNullException ex) {
2186 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2187 Assert.IsNull (ex.InnerException, "#3");
2188 Assert.IsNotNull (ex.Message, "#4");
2189 Assert.AreEqual ("machineName", ex.ParamName, "#5");
2194 // This hangs on windows
2195 [Category ("NotWorking")]
2196 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
2198 // access to registry of remote machines is not implemented on unix
2200 Assert.Ignore ("Running on Unix.");
2203 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
2206 } catch (IOException ex) {
2207 // The network path was not found
2208 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2209 Assert.IsNotNull (ex.Message, "#3");
2210 Assert.IsNull (ex.InnerException, "#4");
2214 [Test] // bug #322839
2215 public void SetValue1_EntityReferences ()
2217 string subKeyName = Guid.NewGuid ().ToString ();
2220 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2221 // we created a new subkey, so value should not exist
2222 Assert.IsNull (createdKey.GetValue ("FirstName&\"<LastName>\""), "#A1");
2224 createdKey.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
2226 object name = createdKey.GetValue ("FirstName&\"<LastName>\"");
2227 // value should exist
2228 Assert.IsNotNull (name, "#A2");
2229 // type of value should be string
2230 Assert.AreEqual (typeof (string), name.GetType (), "#A3");
2231 // ensure value matches
2232 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#A4");
2234 // we created a new subkey, so value should not exist
2235 Assert.IsNull (createdKey.GetValue ("Info"), "#B1");
2237 createdKey.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" });
2239 object info = createdKey.GetValue ("Info");
2240 // value should exist
2241 Assert.IsNotNull (info, "#B2");
2242 // type of value should be string
2243 Assert.AreEqual (typeof (string []), info.GetType (), "#B3");
2244 // ensure value matches
2245 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#B4");
2248 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2249 object name = openedKey.GetValue ("FirstName&\"<LastName>\"");
2250 Assert.IsNotNull (name, "#C1");
2251 Assert.AreEqual (typeof (string), name.GetType (), "#C2");
2252 Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#C3");
2254 object info = openedKey.GetValue ("Info");
2255 Assert.IsNotNull (info, "#D1");
2256 Assert.AreEqual (typeof (string []), info.GetType (), "#D2");
2257 Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#D3");
2261 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2265 [Test] // SetValue (String, Object)
2266 public void SetValue1_Name_Null ()
2268 string subKeyName = Guid.NewGuid ().ToString ();
2270 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2272 createdKey.SetValue (null, "value1");
2273 string [] names = createdKey.GetValueNames ();
2274 Assert.IsNotNull (names, "#A1");
2275 Assert.AreEqual (1, names.Length, "#A2");
2276 Assert.IsNotNull (names [0], "#A3");
2277 Assert.AreEqual (string.Empty, names [0], "#A4");
2278 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2279 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2280 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2281 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2283 createdKey.SetValue (string.Empty, "value2");
2284 names = createdKey.GetValueNames ();
2285 Assert.IsNotNull (names, "#B1");
2286 Assert.AreEqual (1, names.Length, "#B2");
2287 Assert.IsNotNull (names [0], "#B3");
2288 Assert.AreEqual (string.Empty, names [0], "#B4");
2289 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2290 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2291 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2292 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2295 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2299 [Test] // SetValue (String, Object)
2300 public void SetValue1_Name_Empty ()
2302 string subKeyName = Guid.NewGuid ().ToString ();
2304 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2306 createdKey.SetValue (string.Empty, "value1");
2307 string [] names = createdKey.GetValueNames ();
2308 Assert.IsNotNull (names, "#A1");
2309 Assert.AreEqual (1, names.Length, "#A2");
2310 Assert.IsNotNull (names [0], "#A3");
2311 Assert.AreEqual (string.Empty, names [0], "#A4");
2312 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2313 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2314 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2315 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2317 createdKey.SetValue (null, "value2");
2318 names = createdKey.GetValueNames ();
2319 Assert.IsNotNull (names, "#B1");
2320 Assert.AreEqual (1, names.Length, "#B2");
2321 Assert.IsNotNull (names [0], "#B3");
2322 Assert.AreEqual (string.Empty, names [0], "#B4");
2323 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2324 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2325 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2326 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2329 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2333 [Test] // SetValue (String, Object)
2334 public void SetValue1_Name_MaxLength ()
2336 string subKeyName = Guid.NewGuid ().ToString ();
2339 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2340 string name = new string ('a', 254);
2342 createdKey.SetValue (name, "value1");
2343 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2344 createdKey.DeleteValue (name);
2345 Assert.IsNull (createdKey.GetValue (name), "#A2");
2347 name = new string ('a', 255);
2349 createdKey.SetValue (name, "value2");
2350 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2351 createdKey.DeleteValue (name);
2352 Assert.IsNull (createdKey.GetValue (name), "#B2");
2354 name = new string ('a', 256);
2357 createdKey.SetValue (name, "value2");
2358 Assert.Fail ("#C1");
2359 } catch (ArgumentException ex) {
2360 // 1.x: Registry subkeys should not be
2361 // greater than or equal to 255 characters
2363 // 2.x: Registry subkeys should not be
2364 // greater than 255 characters
2365 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2366 Assert.IsNull (ex.InnerException, "#C3");
2367 Assert.IsNotNull (ex.Message, "#C4");
2368 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2369 Assert.IsNull (ex.ParamName, "#C6");
2374 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2375 if (createdKey != null) {
2376 createdKey.Close ();
2377 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2384 [Test] // SetValue (String, Object)
2385 public void SetValue1_Value_Null ()
2387 string subKeyName = Guid.NewGuid ().ToString ();
2389 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2392 createdKey.SetValue ("Name", null);
2394 } catch (ArgumentNullException ex) {
2395 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2396 Assert.IsNull (ex.InnerException, "#3");
2397 Assert.IsNotNull (ex.Message, "#4");
2398 Assert.AreEqual ("value", ex.ParamName, "#5");
2402 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2406 [Test] // SetValue (String, Object)
2407 public void SetValue1_Boolean ()
2409 string subKeyName = Guid.NewGuid ().ToString ();
2412 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2413 // we created a new subkey, so value should not exist
2414 Assert.IsNull (createdKey.GetValue ("Installed"), "#A1");
2416 createdKey.SetValue ("Installed", true);
2418 object value = createdKey.GetValue ("Installed");
2419 // value should exist
2420 Assert.IsNotNull (value, "#A2");
2421 // type of value should be string
2422 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2423 // ensure value matches
2424 Assert.AreEqual (true.ToString (), value, "#A4");
2427 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2428 object value = openedKey.GetValue ("Installed");
2429 Assert.IsNotNull (value, "#B1");
2430 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2431 Assert.AreEqual (true.ToString (), value, "#B3");
2435 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2439 [Test] // SetValue (String, Object)
2440 public void SetValue1_Byte ()
2442 string subKeyName = Guid.NewGuid ().ToString ();
2445 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2446 // we created a new subkey, so value should not exist
2447 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2449 createdKey.SetValue ("Flags", (byte) 5);
2451 object value = createdKey.GetValue ("Flags");
2452 // value should exist
2453 Assert.IsNotNull (value, "#A2");
2454 // type of value should be string
2455 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2456 // ensure value matches
2457 Assert.AreEqual ("5", value, "#A4");
2460 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2461 object value = openedKey.GetValue ("Flags");
2462 Assert.IsNotNull (value, "#B1");
2463 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2464 Assert.AreEqual ("5", value, "#B3");
2468 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2472 [Test] // SetValue (String, Object)
2473 public void SetValue1_ByteArray ()
2475 string subKeyName = Guid.NewGuid ().ToString ();
2478 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2479 // we created a new subkey, so value should not exist
2480 Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2482 createdKey.SetValue ("Flags", new byte [] { 1, 5 });
2484 object value = createdKey.GetValue ("Flags");
2485 // value should exist
2486 Assert.IsNotNull (value, "#A2");
2487 // type of value should be string
2488 Assert.AreEqual (typeof (byte []), value.GetType (), "#3");
2489 // ensure value matches
2490 Assert.AreEqual (new byte [] { 1, 5 }, value, "#4");
2493 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2494 object value = openedKey.GetValue ("Flags");
2495 Assert.IsNotNull (value, "#B1");
2496 Assert.AreEqual (typeof (byte []), value.GetType (), "#B2");
2497 Assert.AreEqual (new byte [] { 1, 5 }, value, "#B3");
2501 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2505 [Test] // SetValue (String, Object)
2506 public void SetValue1_DateTime ()
2508 string subKeyName = Guid.NewGuid ().ToString ();
2511 object rawValue = DateTime.Now;
2513 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2514 // we created a new subkey, so value should not exist
2515 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2517 createdKey.SetValue ("Path", rawValue);
2519 object value = createdKey.GetValue ("Path");
2520 // value should exist
2521 Assert.IsNotNull (value, "#A2");
2522 // type of value should be string
2523 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2524 // ensure value matches
2525 Assert.AreEqual (rawValue.ToString (), value, "#A4");
2528 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2529 object value = openedKey.GetValue ("Path");
2530 Assert.IsNotNull (value, "#B1");
2531 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2532 Assert.AreEqual (rawValue.ToString (), value, "#B3");
2536 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2541 public void SetValue_Int32 ()
2543 string subKeyName = Guid.NewGuid ().ToString ();
2546 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2547 // we created a new subkey, so value should not exist
2548 Assert.IsNull (createdKey.GetValue ("RefCount"), "#A1");
2550 createdKey.SetValue ("RefCount", 5);
2552 object value = createdKey.GetValue ("RefCount");
2553 // value should exist
2554 Assert.IsNotNull (value, "#A2");
2555 // type of value should be int
2556 Assert.AreEqual (typeof (int), value.GetType (), "#A3");
2557 // ensure value matches
2558 Assert.AreEqual (5, value, "#A4");
2561 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2562 object value = openedKey.GetValue ("RefCount");
2563 Assert.IsNotNull (value, "#B1");
2564 Assert.AreEqual (typeof (int), value.GetType (), "#B2");
2565 Assert.AreEqual (5, value, "#B3");
2569 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2573 [Test] // SetValue (String, Object)
2574 public void SetValue1_Int64 ()
2576 string subKeyName = Guid.NewGuid ().ToString ();
2579 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2580 // we created a new subkey, so value should not exist
2581 Assert.IsNull (createdKey.GetValue ("Ticks"), "#A1");
2583 createdKey.SetValue ("Ticks", 500L);
2585 object value = createdKey.GetValue ("Ticks");
2586 // value should exist
2587 Assert.IsNotNull (value, "#A2");
2588 // type of value should be string
2589 Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2590 // ensure value matches
2591 Assert.AreEqual ("500", value, "#A4");
2594 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2595 object value = openedKey.GetValue ("Ticks");
2596 Assert.IsNotNull (value, "#B1");
2597 Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2598 Assert.AreEqual ("500", value, "#B3");
2602 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2606 [Test] // SetValue (String, Object)
2607 public void SetValue1_String ()
2609 string subKeyName = Guid.NewGuid ().ToString ();
2612 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2613 // we created a new subkey, so value should not exist
2614 Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2616 createdKey.SetValue ("Path", "/usr/lib/whatever");
2618 object path = createdKey.GetValue ("Path");
2619 // value should exist
2620 Assert.IsNotNull (path, "#A2");
2621 // type of value should be string
2622 Assert.AreEqual (typeof (string), path.GetType (), "#A3");
2623 // ensure value matches
2624 Assert.AreEqual ("/usr/lib/whatever", path, "#A4");
2627 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2628 object path = openedKey.GetValue ("Path");
2629 Assert.IsNotNull (path, "#B1");
2630 Assert.AreEqual (typeof (string), path.GetType (), "#B2");
2631 Assert.AreEqual ("/usr/lib/whatever", path, "#B3");
2635 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2639 [Test] // SetValue (String, Object)
2640 public void SetValue1_StringArray ()
2642 string subKeyName = Guid.NewGuid ().ToString ();
2645 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2646 // we created a new subkey, so value should not exist
2647 Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#A1");
2649 createdKey.SetValue ("DependsOnGroup", new string [] { "A", "B" });
2651 object value = createdKey.GetValue ("DependsOnGroup");
2652 // value should exist
2653 Assert.IsNotNull (value, "#A2");
2654 // type of value should be string
2655 Assert.AreEqual (typeof (string []), value.GetType (), "#A3");
2656 // ensure value matches
2657 Assert.AreEqual (new string [] { "A", "B" }, value, "#A4");
2660 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2661 object value = openedKey.GetValue ("DependsOnGroup");
2662 Assert.IsNotNull (value, "#B1");
2663 Assert.AreEqual (typeof (string []), value.GetType (), "#B2");
2664 Assert.AreEqual (new string [] { "A", "B" }, value, "#B3");
2668 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2672 [Test] // SetValue (String, Object)
2673 public void SetValue1_Key_ReadOnly ()
2675 string subKeyName = Guid.NewGuid ().ToString ();
2677 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2679 softwareKey.SetValue ("name1", "value1");
2681 } catch (UnauthorizedAccessException ex) {
2682 // Cannot write to the registry key
2683 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2684 Assert.IsNotNull (ex.Message, "#3");
2685 Assert.IsNull (ex.InnerException, "#4");
2689 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2691 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2694 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2696 createdKey.SetValue ("name1", "value1");
2698 } catch (UnauthorizedAccessException ex) {
2699 // Cannot write to the registry key
2700 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2701 Assert.IsNotNull (ex.Message, "#3");
2702 Assert.IsNull (ex.InnerException, "#4");
2707 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2708 if (createdKey != null) {
2709 createdKey.Close ();
2710 softwareKey.DeleteSubKeyTree (subKeyName);
2718 [Test] // SetValue (String, Object)
2719 public void SetValue1_Key_Removed ()
2721 string subKeyName = Guid.NewGuid ().ToString ();
2723 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2725 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2726 softwareKey.DeleteSubKeyTree (subKeyName);
2727 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2729 createdKey.SetValue ("name1", "value1");
2731 } catch (IOException ex) {
2732 // Illegal operation attempted on a registry key that
2733 // has been marked for deletion
2734 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2735 Assert.IsNotNull (ex.Message, "#4");
2736 Assert.IsNull (ex.InnerException, "#5");
2741 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2742 if (createdKey != null) {
2743 createdKey.Close ();
2744 softwareKey.DeleteSubKeyTree (subKeyName);
2752 [Test] // SetValue (String, Object, RegistryValueKind)
2753 public void SetValue2_Key_ReadOnly ()
2755 string subKeyName = Guid.NewGuid ().ToString ();
2757 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2759 softwareKey.SetValue ("name1", "value1",
2760 RegistryValueKind.String);
2762 } catch (UnauthorizedAccessException ex) {
2763 // Cannot write to the registry key
2764 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2765 Assert.IsNotNull (ex.Message, "#3");
2766 Assert.IsNull (ex.InnerException, "#4");
2770 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2772 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2775 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2777 createdKey.SetValue ("name1", "value1",
2778 RegistryValueKind.String);
2780 } catch (UnauthorizedAccessException ex) {
2781 // Cannot write to the registry key
2782 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2783 Assert.IsNotNull (ex.Message, "#3");
2784 Assert.IsNull (ex.InnerException, "#4");
2789 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2790 if (createdKey != null) {
2791 createdKey.Close ();
2792 softwareKey.DeleteSubKeyTree (subKeyName);
2800 [Test] // SetValue (String, Object, RegistryValueKind)
2801 public void SetValue2_Key_Removed ()
2803 string subKeyName = Guid.NewGuid ().ToString ();
2805 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2807 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2808 softwareKey.DeleteSubKeyTree (subKeyName);
2809 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2811 createdKey.SetValue ("name1", "value1",
2812 RegistryValueKind.String);
2814 } catch (IOException ex) {
2815 // Illegal operation attempted on a registry key that
2816 // has been marked for deletion
2817 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2818 Assert.IsNotNull (ex.Message, "#4");
2819 Assert.IsNull (ex.InnerException, "#5");
2824 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2825 if (createdKey != null) {
2826 createdKey.Close ();
2827 softwareKey.DeleteSubKeyTree (subKeyName);
2835 [Test] // SetValue (String, Object, RegistryValueKind)
2836 public void SetValue2_Name_Empty ()
2838 string subKeyName = Guid.NewGuid ().ToString ();
2840 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2842 createdKey.SetValue (string.Empty, "value1",
2843 RegistryValueKind.String);
2844 string [] names = createdKey.GetValueNames ();
2845 Assert.IsNotNull (names, "#A1");
2846 Assert.AreEqual (1, names.Length, "#A2");
2847 Assert.IsNotNull (names [0], "#A3");
2848 Assert.AreEqual (string.Empty, names [0], "#A4");
2849 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2850 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2851 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2852 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2854 createdKey.SetValue (null, "value2",
2855 RegistryValueKind.String);
2856 names = createdKey.GetValueNames ();
2857 Assert.IsNotNull (names, "#B1");
2858 Assert.AreEqual (1, names.Length, "#B2");
2859 Assert.IsNotNull (names [0], "#B3");
2860 Assert.AreEqual (string.Empty, names [0], "#B4");
2861 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2862 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2863 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2864 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2867 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2871 [Test] // SetValue (String, Object, RegistryValueKind)
2872 public void SetValue2_Name_MaxLength ()
2874 string subKeyName = Guid.NewGuid ().ToString ();
2877 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2878 string name = new string ('a', 254);
2880 createdKey.SetValue (name, "value1",
2881 RegistryValueKind.String);
2882 Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2883 createdKey.DeleteValue (name);
2884 Assert.IsNull (createdKey.GetValue (name), "#A2");
2886 name = new string ('a', 255);
2888 createdKey.SetValue (name, "value2",
2889 RegistryValueKind.String);
2890 Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2891 createdKey.DeleteValue (name);
2892 Assert.IsNull (createdKey.GetValue (name), "#B2");
2894 name = new string ('a', 256);
2897 createdKey.SetValue (name, "value2",
2898 RegistryValueKind.String);
2899 Assert.Fail ("#C1");
2900 } catch (ArgumentException ex) {
2901 // Registry subkeys should not be
2902 // greater than 255 characters
2903 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2904 Assert.IsNull (ex.InnerException, "#C3");
2905 Assert.IsNotNull (ex.Message, "#C4");
2906 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2907 Assert.IsNull (ex.ParamName, "#C6");
2912 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2913 if (createdKey != null) {
2914 createdKey.Close ();
2915 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2922 [Test] // SetValue (String, Object, RegistryValueKind)
2923 public void SetValue2_Name_Null ()
2925 string subKeyName = Guid.NewGuid ().ToString ();
2927 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2929 createdKey.SetValue (null, "value1",
2930 RegistryValueKind.String);
2931 string [] names = createdKey.GetValueNames ();
2932 Assert.IsNotNull (names, "#A1");
2933 Assert.AreEqual (1, names.Length, "#A2");
2934 Assert.IsNotNull (names [0], "#A3");
2935 Assert.AreEqual (string.Empty, names [0], "#A4");
2936 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2937 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2938 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2939 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2941 createdKey.SetValue (string.Empty, "value2",
2942 RegistryValueKind.String);
2943 names = createdKey.GetValueNames ();
2944 Assert.IsNotNull (names, "#B1");
2945 Assert.AreEqual (1, names.Length, "#B2");
2946 Assert.IsNotNull (names [0], "#B3");
2947 Assert.AreEqual (string.Empty, names [0], "#B4");
2948 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2949 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2950 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2951 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2954 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2958 [Test] // SetValue (String, Object, RegistryValueKind)
2959 public void SetValue2_Value_Null ()
2961 string subKeyName = Guid.NewGuid ().ToString ();
2963 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2966 createdKey.SetValue ("Name", null,
2967 RegistryValueKind.String);
2969 } catch (ArgumentNullException ex) {
2970 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2971 Assert.IsNull (ex.InnerException, "#3");
2972 Assert.IsNotNull (ex.Message, "#4");
2973 Assert.AreEqual ("value", ex.ParamName, "#5");
2977 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2982 public void SubKeyCount ()
2984 string subKeyName = Guid.NewGuid ().ToString ();
2987 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2988 // check if key was successfully created
2989 Assert.IsNotNull (createdKey, "#A1");
2990 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2993 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2994 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2997 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2999 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3000 Assert.IsNotNull (createdKey, "#B1");
3001 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3003 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3004 Assert.IsNotNull (createdKey2, "#B3");
3005 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
3006 createdKey2.DeleteSubKey ("monotemp1");
3007 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
3009 Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
3013 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3014 if (createdKey != null) {
3015 createdKey.Close ();
3016 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3024 public void SubKeyCount_Key_Removed ()
3026 string subKeyName = Guid.NewGuid ().ToString ();
3029 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3030 // check if key was successfully created
3031 Assert.IsNotNull (createdKey, "#A1");
3032 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3035 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3036 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3039 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3041 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3042 Assert.IsNotNull (createdKey, "#B1");
3043 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3045 // remove created key
3046 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3049 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
3050 } catch (IOException ex) {
3051 // Illegal operation attempted on a registry key that
3052 // has been marked for deletion
3053 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3054 Assert.IsNotNull (ex.Message, "#15");
3055 Assert.IsNull (ex.InnerException, "#16");
3060 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3061 if (createdKey != null) {
3062 createdKey.Close ();
3063 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3071 public void ValueCount ()
3073 string subKeyName = Guid.NewGuid ().ToString ();
3076 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3077 // check if key was successfully created
3078 Assert.IsNotNull (createdKey, "#A1");
3079 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3080 createdKey.SetValue ("name1", "value1");
3081 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3082 createdKey.SetValue ("name2", "value2");
3083 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3084 createdKey.SetValue ("name2", "value2b");
3085 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3086 createdKey.SetValue ("name3", "value3");
3087 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3089 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3090 Assert.IsNotNull (createdKey, "#B1");
3091 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3093 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3094 Assert.IsNotNull (createdKey2, "#B3");
3095 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
3096 createdKey2.DeleteValue ("name2");
3097 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
3099 Assert.AreEqual (2, createdKey.ValueCount, "#B6");
3103 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3104 if (createdKey != null) {
3105 createdKey.Close ();
3106 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3114 public void ValueCount_Key_Removed ()
3116 string subKeyName = Guid.NewGuid ().ToString ();
3119 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3120 // check if key was successfully created
3121 Assert.IsNotNull (createdKey, "#A1");
3122 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3123 createdKey.SetValue ("name1", "value1");
3124 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3125 createdKey.SetValue ("name2", "value2");
3126 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3127 createdKey.SetValue ("name2", "value2b");
3128 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3129 createdKey.SetValue ("name3", "value3");
3130 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3132 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3133 Assert.IsNotNull (createdKey, "#B1");
3134 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3136 // remove created key
3137 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3140 Assert.Fail ("#C1: " + createdKey.ValueCount);
3141 } catch (IOException ex) {
3142 // Illegal operation attempted on a registry key that
3143 // has been marked for deletion
3144 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3145 Assert.IsNotNull (ex.Message, "#15");
3146 Assert.IsNull (ex.InnerException, "#16");
3151 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3152 if (createdKey != null) {
3153 createdKey.Close ();
3154 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3162 public void bug79051 ()
3164 string subKeyName = Guid.NewGuid ().ToString ();
3166 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3168 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3169 createdKey.SetValue ("test", "whatever");
3170 createdKey.Close ();
3171 softwareKey.DeleteSubKeyTree (subKeyName);
3175 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3176 if (createdKey != null) {
3177 createdKey.Close ();
3178 softwareKey.DeleteSubKeyTree (subKeyName);
3188 public void TypeCastTests ()
3190 string subKeyName = Guid.NewGuid ().ToString ();
3191 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3193 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3194 createdKey.SetValue ("test-int", (int) 1, RegistryValueKind.DWord);
3195 createdKey.SetValue ("test-uint", (uint) 1, RegistryValueKind.DWord);
3196 createdKey.SetValue ("test-byte", (byte) 1, RegistryValueKind.DWord);
3197 createdKey.SetValue ("test-sbyte", (sbyte) 1, RegistryValueKind.DWord);
3198 createdKey.SetValue ("test-short", (short) 1, RegistryValueKind.DWord);
3199 createdKey.SetValue ("test-ushort", (ushort) 1, RegistryValueKind.DWord);
3200 createdKey.SetValue ("test-long", (long) 1, RegistryValueKind.DWord);
3201 createdKey.SetValue ("test-ulong", (ulong) 1, RegistryValueKind.DWord);
3202 createdKey.SetValue ("test-decimal", (decimal) 1, RegistryValueKind.DWord);
3203 createdKey.SetValue ("test-float", (float) 1, RegistryValueKind.DWord);
3204 createdKey.SetValue ("test-bool", true, RegistryValueKind.DWord);
3206 createdKey.SetValue ("dtest-int", (int) 1, RegistryValueKind.QWord);
3207 createdKey.SetValue ("dtest-uint", (uint) 1, RegistryValueKind.QWord);
3208 createdKey.SetValue ("dtest-byte", (byte) 1, RegistryValueKind.QWord);
3209 createdKey.SetValue ("dtest-sbyte", (sbyte) 1, RegistryValueKind.QWord);
3210 createdKey.SetValue ("dtest-short", (short) 1, RegistryValueKind.QWord);
3211 createdKey.SetValue ("dtest-ushort", (ushort) 1, RegistryValueKind.QWord);
3212 createdKey.SetValue ("dtest-long", (long) 1, RegistryValueKind.QWord);
3213 createdKey.SetValue ("dtest-ulong", (ulong) 1, RegistryValueKind.QWord);
3214 createdKey.SetValue ("dtest-decimal", (decimal) 1, RegistryValueKind.QWord);
3215 createdKey.SetValue ("dtest-float", (float) 1, RegistryValueKind.QWord);
3216 createdKey.SetValue ("dtest-bool", true, RegistryValueKind.QWord);
3218 object r = createdKey.GetValue ("test-int");
3219 Assert.AreEqual (r is int, true);
3220 Assert.AreEqual ((int) r, 1);
3222 r = createdKey.GetValue ("test-uint");
3223 Assert.AreEqual (r is int, true);
3224 Assert.AreEqual ((int) r, 1);
3225 r = createdKey.GetValue ("test-byte");
3226 Assert.AreEqual (r is int, true);
3227 Assert.AreEqual ((int) r, 1);
3228 r = createdKey.GetValue ("test-sbyte");
3229 Assert.AreEqual (r is int, true);
3230 Assert.AreEqual ((int) r, 1);
3231 r = createdKey.GetValue ("test-short");
3232 Assert.AreEqual (r is int, true);
3233 Assert.AreEqual ((int) r, 1);
3234 r = createdKey.GetValue ("test-ushort");
3235 Assert.AreEqual (r is int, true);
3236 Assert.AreEqual ((int) r, 1);
3237 r = createdKey.GetValue ("test-long");
3238 Assert.AreEqual (r is int, true);
3239 Assert.AreEqual ((int) r, 1);
3240 r = createdKey.GetValue ("test-ulong");
3241 Assert.AreEqual (r is int, true);
3242 Assert.AreEqual ((int) r, 1);
3244 r = createdKey.GetValue ("dtest-int");
3245 Assert.AreEqual (r is long, true);
3246 Assert.AreEqual ((long) r, 1);
3247 r = createdKey.GetValue ("dtest-uint");
3248 Assert.AreEqual (r is long, true);
3249 Assert.AreEqual ((long) r, 1);
3250 r = createdKey.GetValue ("dtest-byte");
3251 Assert.AreEqual (r is long, true);
3252 Assert.AreEqual ((long) r, 1);
3253 r = createdKey.GetValue ("dtest-sbyte");
3254 Assert.AreEqual (r is long, true);
3255 Assert.AreEqual ((long) r, 1);
3256 r = createdKey.GetValue ("dtest-short");
3257 Assert.AreEqual (r is long, true);
3258 Assert.AreEqual ((long) r, 1);
3259 r = createdKey.GetValue ("dtest-ushort");
3260 Assert.AreEqual (r is long, true);
3261 Assert.AreEqual ((long) r, 1);
3262 r = createdKey.GetValue ("dtest-long");
3263 Assert.AreEqual (r is long, true);
3264 Assert.AreEqual ((long) r, 1);
3265 r = createdKey.GetValue ("dtest-ulong");
3266 Assert.AreEqual (r is long, true);
3267 Assert.AreEqual ((long) r, 1);
3268 r = createdKey.GetValue ("dtest-decimal");
3269 Assert.IsTrue (r is long);
3270 Assert.AreEqual ((long) r, 1);
3271 r = createdKey.GetValue ("dtest-float");
3272 Assert.IsTrue (r is long);
3273 Assert.AreEqual ((long) r, 1);
3274 r = createdKey.GetValue ("dtest-bool");
3275 Assert.AreEqual (typeof (long), r.GetType ());
3278 createdKey.SetValue ("test-int", uint.MaxValue, RegistryValueKind.DWord);
3279 Assert.Fail ("#100");
3281 createdKey.SetValue ("test-int", ulong.MaxValue, RegistryValueKind.QWord);
3282 Assert.Fail ("#101");
3283 } catch (ArgumentException) {
3286 createdKey.Close ();
3287 softwareKey.DeleteSubKeyTree (subKeyName);
3291 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3292 if (createdKey != null) {
3293 createdKey.Close ();
3294 softwareKey.DeleteSubKeyTree (subKeyName);
3303 public void bug79059 ()
3305 string subKeyName = Guid.NewGuid ().ToString ();
3307 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3309 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3310 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
3312 createdKey.Close ();
3313 softwareKey.DeleteSubKeyTree (subKeyName);
3317 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3318 if (createdKey != null) {
3319 createdKey.Close ();
3320 softwareKey.DeleteSubKeyTree (subKeyName);
3329 public void bugnew1 ()
3331 string subKeyName = Guid.NewGuid ().ToString ();
3333 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3335 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3336 createdKey.SetValue ("name1", "value1");
3338 RegistryKey testKey = null;
3340 testKey = createdKey.OpenSubKey ("test", true);
3341 if (testKey == null)
3342 testKey = createdKey.CreateSubKey ("test");
3343 testKey.SetValue ("another", "one");
3345 if (testKey != null)
3349 createdKey.SetValue ("name2", "value2");
3350 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
3351 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
3352 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
3353 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
3355 string [] names = createdKey.GetValueNames ();
3356 Assert.IsNotNull (names, "#6");
3357 Assert.AreEqual (2, names.Length, "#7");
3358 Assert.AreEqual ("name1", names [0], "#8");
3359 Assert.AreEqual ("name2", names [1], "#9");
3361 softwareKey.DeleteSubKeyTree (subKeyName);
3363 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
3364 Assert.IsNull (openedKey, "#10");
3367 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
3368 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
3371 createdKey.GetValueNames ();
3372 Assert.Fail ("#13");
3373 } catch (IOException ex) {
3374 // Illegal operation attempted on a registry key that
3375 // has been marked for deletion
3376 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3377 Assert.IsNotNull (ex.Message, "#15");
3378 Assert.IsNull (ex.InnerException, "#16");
3382 createdKey.SetValue ("name1", "value1");
3383 Assert.Fail ("#17");
3384 } catch (IOException ex) {
3385 // Illegal operation attempted on a registry key that
3386 // has been marked for deletion
3387 Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
3388 Assert.IsNotNull (ex.Message, "#19");
3389 Assert.IsNull (ex.InnerException, "#20");
3393 createdKey.SetValue ("newname", "value1");
3394 Assert.Fail ("#21");
3395 } catch (IOException ex) {
3396 // Illegal operation attempted on a registry key that
3397 // has been marked for deletion
3398 Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
3399 Assert.IsNotNull (ex.Message, "#23");
3400 Assert.IsNull (ex.InnerException, "#24");
3403 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
3404 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
3405 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
3406 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
3409 createdKey.CreateSubKey ("new");
3410 Assert.Fail ("#29");
3411 } catch (IOException ex) {
3412 // Illegal operation attempted on a registry key that
3413 // has been marked for deletion
3414 Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
3415 Assert.IsNotNull (ex.Message, "#31");
3416 Assert.IsNull (ex.InnerException, "#32");
3421 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3422 if (createdKey != null) {
3423 createdKey.Close ();
3424 softwareKey.DeleteSubKeyTree (subKeyName);
3433 public void bugnew2 () // values cannot be written on registry root (hive)
3435 string [] names = Registry.CurrentUser.GetValueNames ();
3436 Assert.IsNotNull (names, "#1");
3437 Registry.CurrentUser.SetValue ("name1", "value1");
3438 Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
3439 Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
3440 string [] newNames = Registry.CurrentUser.GetValueNames ();
3441 Assert.IsNotNull (newNames, "#4");
3442 Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
3443 Registry.CurrentUser.DeleteValue ("name1");
3447 public void bugnew3 () // on Windows, key cannot be closed twice
3449 string subKeyName = Guid.NewGuid ().ToString ();
3452 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3453 createdKey.Close ();
3456 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3461 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3462 if (createdKey != null) {
3463 createdKey.Close ();
3464 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3472 public void bugnew4 () // Key cannot be flushed once it has been closed
3474 string subKeyName = Guid.NewGuid ().ToString ();
3477 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3478 createdKey.Close ();
3481 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3486 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3487 if (createdKey != null) {
3488 createdKey.Close ();
3489 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3496 private bool RunningOnUnix {
3498 int p = (int) Environment.OSVersion.Platform;
3499 return ((p == 4) || (p == 128) || (p == 6));