2 // RegistryKeyTest.cs - NUnit Test Cases for Microsoft.Win32.RegistryKey
5 // mei (mei@work.email.ne.jp)
6 // Robert Jordan (robertj@gmx.net)
7 // Gert Driesen (drieseng@users.sourceforge.net)
10 // (C) 2004, 2005 Novell (http://www.novell.com)
16 using Microsoft.Win32;
18 using NUnit.Framework;
20 namespace MonoTests.Microsoft.Win32
23 public class RegistryKeyTest
25 private const string mimeroot = @"MIME\Database\Content Type";
27 [Category("NotWorking")]
28 // This will not work on Linux ever
29 public void TestGetValue ()
31 RegistryKey root = Registry.ClassesRoot;
34 key = root.OpenSubKey (mimeroot + @"\audio/wav");
35 Assert.AreEqual (".wav", key.GetValue ("Extension"), "GetValue #1");
36 key = root.OpenSubKey (mimeroot + @"\text/x-scriptlet");
37 Assert.AreEqual (null, key.GetValue ("Extension"), "GetValue #2");
41 // Unit test for bug #77212
44 public void TestHandle ()
46 // this test is for Windows only
50 // this regpath always exists under windows
51 RegistryKey k = Registry.CurrentUser
52 .OpenSubKey ("Software", false)
53 .OpenSubKey ("Microsoft", false)
54 .OpenSubKey ("Windows", false);
56 Assert.IsNotNull (k, "#01");
60 public void OpenSubKey ()
62 RegistryKey key = Registry.LocalMachine;
64 // HKEY_LOCAL_MACHINE\software should always exist on Windows
65 // and is automatically created on Linux
66 Assert.IsNotNull (key.OpenSubKey ("Software"), "#A1");
67 Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#A2");
69 key = Registry.CurrentUser;
71 // HKEY_CURRENT_USER\software should always exist on Windows
72 // and is automatically created on Linux
73 Assert.IsNotNull (key.OpenSubKey ("Software"), "#B1");
74 Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#B2");
78 // HKEY_USERS\software should not exist on Windows, and should not
79 // be created automatically on Linux
80 Assert.IsNull (key.OpenSubKey ("Software"), "#C1");
81 Assert.IsNull (key.OpenSubKey ("soFtware"), "#C2");
85 public void OpenSubKey_Key_DoesNotExist ()
87 string subKeyName = Guid.NewGuid ().ToString ();
88 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#1"); // read-only
89 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#2"); // writable
93 public void OpenSubKey_Key_Removed ()
95 string subKeyName = Guid.NewGuid ().ToString ();
98 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
99 // check if key was successfully created
100 Assert.IsNotNull (createdKey, "#1");
101 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
104 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
105 Assert.IsNotNull (createdKey, "#2");
106 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
107 Assert.IsNotNull (createdKey, "#3");
109 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
112 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
113 Assert.IsNull (createdKey.OpenSubKey ("monotemp"), "#5"); // read-only
115 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#6");
116 Assert.IsNull (createdKey.OpenSubKey ("monotemp", true), "#7");
120 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
121 if (createdKey != null) {
123 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
131 [ExpectedException (typeof (ArgumentNullException))]
132 public void OpenSubKey_Name_Null ()
134 Registry.CurrentUser.OpenSubKey (null);
138 [Category ("NotWorking")] // MS should not allow this
139 public void OpenSubKey_Name_Empty ()
142 using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty)) {
143 Assert.IsNotNull (emptyKey, "#1");
146 using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty, true)) {
147 Assert.IsNotNull (emptyKey, "#1");
152 public void Close_Local_Hive ()
154 RegistryKey hive = Registry.CurrentUser;
157 Assert.IsNotNull (hive.GetSubKeyNames (), "#1");
158 Assert.IsNull (hive.GetValue ("doesnotexist"), "#2");
159 Assert.IsNotNull (hive.GetValueNames (), "#3");
160 Assert.IsNull (hive.OpenSubKey ("doesnotexist"), "#4");
161 Assert.IsNotNull (hive.SubKeyCount, "#5");
162 Assert.IsNotNull (hive.ToString (), "#6");
164 // closing key again does not have any effect
169 public void Close_Local_Key ()
171 RegistryKey key = Registry.CurrentUser.OpenSubKey ("SOFTWARE");
174 // closing a key twice does not have any effect
178 key.CreateSubKey ("a");
180 } catch (ObjectDisposedException) {
184 key.DeleteSubKey ("doesnotexist");
186 } catch (ObjectDisposedException) {
190 key.DeleteSubKeyTree ("doesnotexist");
192 } catch (ObjectDisposedException) {
196 key.DeleteValue ("doesnotexist");
198 } catch (ObjectDisposedException) {
201 // flushing a closed key does not have any effect
205 key.GetSubKeyNames ();
207 } catch (ObjectDisposedException) {
211 key.GetValue ("doesnotexist");
213 } catch (ObjectDisposedException) {
217 key.GetValueNames ();
219 } catch (ObjectDisposedException) {
223 key.OpenSubKey ("doesnotexist");
225 } catch (ObjectDisposedException) {
229 key.SetValue ("doesnotexist", "something");
231 } catch (ObjectDisposedException) {
235 int x = key.SubKeyCount;
236 Assert.Fail ("#10:" + x);
237 } catch (ObjectDisposedException) {
243 } catch (ObjectDisposedException) {
247 int x = key.ValueCount;
248 Assert.Fail ("#12:" + x);
249 } catch (ObjectDisposedException) {
254 public void Close_Remote_Hive ()
256 // access to registry of remote machines is not implemented on unix
260 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
261 RegistryHive.CurrentUser, Environment.MachineName);
264 // closing a remote hive twice does not have any effect
268 hive.CreateSubKey ("a");
270 } catch (ObjectDisposedException) {
274 hive.DeleteSubKey ("doesnotexist");
276 } catch (ObjectDisposedException) {
280 hive.DeleteSubKeyTree ("doesnotexist");
282 } catch (ObjectDisposedException) {
286 hive.DeleteValue ("doesnotexist");
288 } catch (ObjectDisposedException) {
291 // flushing a closed hive does not have any effect
295 hive.GetSubKeyNames ();
297 } catch (ObjectDisposedException) {
301 hive.GetValue ("doesnotexist");
303 } catch (ObjectDisposedException) {
307 hive.GetValueNames ();
309 } catch (ObjectDisposedException) {
313 hive.OpenSubKey ("doesnotexist");
315 } catch (ObjectDisposedException) {
319 hive.SetValue ("doesnotexist", "something");
321 } catch (ObjectDisposedException) {
325 int x = hive.SubKeyCount;
326 Assert.Fail ("#10:" + x);
327 } catch (ObjectDisposedException) {
333 } catch (ObjectDisposedException) {
337 int x = hive.ValueCount;
338 Assert.Fail ("#12:" + x);
339 } catch (ObjectDisposedException) {
344 public void Close_Remote_Key ()
346 // access to registry of remote machines is not implemented on unix
350 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
351 RegistryHive.CurrentUser, Environment.MachineName);
352 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
355 // closing a remote key twice does not have any effect
359 key.CreateSubKey ("a");
361 } catch (ObjectDisposedException) {
365 key.DeleteSubKey ("doesnotexist");
367 } catch (ObjectDisposedException) {
371 key.DeleteSubKeyTree ("doesnotexist");
373 } catch (ObjectDisposedException) {
377 key.DeleteValue ("doesnotexist");
379 } catch (ObjectDisposedException) {
382 // flushing a closed key does not have any effect
386 key.GetSubKeyNames ();
388 } catch (ObjectDisposedException) {
392 key.GetValue ("doesnotexist");
394 } catch (ObjectDisposedException) {
398 key.GetValueNames ();
400 } catch (ObjectDisposedException) {
404 key.OpenSubKey ("doesnotexist");
406 } catch (ObjectDisposedException) {
410 key.SetValue ("doesnotexist", "something");
412 } catch (ObjectDisposedException) {
416 int x = key.SubKeyCount;
417 Assert.Fail ("#10:" + x);
418 } catch (ObjectDisposedException) {
424 } catch (ObjectDisposedException) {
428 int x = key.ValueCount;
429 Assert.Fail ("#12:" + x);
430 } catch (ObjectDisposedException) {
437 public void CreateSubKey ()
439 string subKeyName = Guid.NewGuid ().ToString ();
441 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
443 // check if key was successfully created
444 Assert.IsNotNull (createdKey, "#1");
445 // software subkey should not be created automatically
446 Assert.IsNull (createdKey.OpenSubKey ("software"), "#2");
449 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
454 public void CreateSubKey_Key_ReadOnly ()
456 string subKeyName = Guid.NewGuid ().ToString ();
458 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
459 RegistryKey createdKey = null;
462 createdKey = softwareKey.CreateSubKey (subKeyName);
464 } catch (UnauthorizedAccessException ex) {
465 // Cannot write to the registry key
466 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
467 Assert.IsNotNull (ex.Message, "#3");
468 Assert.IsNull (ex.InnerException, "#4");
471 if (createdKey != null)
478 public void CreateSubKey_Key_Removed ()
480 string subKeyName = Guid.NewGuid ().ToString ();
482 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
484 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
485 softwareKey.DeleteSubKeyTree (subKeyName);
486 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
488 createdKey.CreateSubKey ("test");
490 } catch (IOException ex) {
491 // Illegal operation attempted on a registry key that
492 // has been marked for deletion
493 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
494 Assert.IsNotNull (ex.Message, "#4");
495 Assert.IsNull (ex.InnerException, "#5");
500 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
501 if (createdKey != null) {
503 softwareKey.DeleteSubKeyTree (subKeyName);
512 [Category ("NotWorking")] // MS should not allow this
513 public void CreateSubKey_Name_Empty ()
515 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
516 using (RegistryKey emptyKey = softwareKey.CreateSubKey (string.Empty)) {
517 Assert.IsNotNull (emptyKey, "#1");
518 emptyKey.SetValue ("name1", "value1");
524 [ExpectedException (typeof (ArgumentNullException))]
525 public void CreateSubKey_Name_Null ()
527 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
528 softwareKey.CreateSubKey (null);
533 public void DeleteSubKey ()
535 string subKeyName = Guid.NewGuid ().ToString ();
538 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
539 // check if key was successfully created
540 Assert.IsNotNull (createdKey, "#1");
542 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
543 Assert.IsNotNull (createdKey, "#2");
544 Registry.CurrentUser.DeleteSubKey (subKeyName);
546 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
547 Assert.IsNull (createdKey, "#3");
551 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
552 if (createdKey != null) {
554 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
562 public void DeleteSubKey_Key_HasChildKeys ()
564 string subKeyName = Guid.NewGuid ().ToString ();
567 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
568 // check if key was successfully created
569 Assert.IsNotNull (createdKey, "#1");
570 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
574 Registry.CurrentUser.DeleteSubKey (subKeyName);
576 } catch (InvalidOperationException ex) {
577 // Registry key has subkeys and recursive removes are not
578 // supported by this method
579 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
580 Assert.IsNotNull (ex.Message, "#4");
581 Assert.IsNull (ex.InnerException, "#5");
585 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
586 if (createdKey != null) {
588 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
596 public void DeleteSubKey_Key_ReadOnly ()
598 string subKeyName = Guid.NewGuid ().ToString ();
601 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
602 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
606 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
608 softwareKey.DeleteSubKey (subKeyName);
610 } catch (UnauthorizedAccessException ex) {
611 // Cannot write to the registry key
612 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
613 Assert.IsNotNull (ex.Message, "#3");
614 Assert.IsNull (ex.InnerException, "#4");
619 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
620 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
621 if (createdKey != null) {
623 softwareKey.DeleteSubKeyTree (subKeyName);
632 public void DeleteSubKey_Key_DoesNotExist ()
634 string subKeyName = Guid.NewGuid ().ToString ();
637 Registry.CurrentUser.DeleteSubKey (subKeyName);
639 } catch (ArgumentException ex) {
640 // Cannot delete a subkey tree because the subkey does not exist
641 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
642 Assert.IsNotNull (ex.Message, "#A3");
643 Assert.IsNull (ex.InnerException, "#A4");
647 Registry.CurrentUser.DeleteSubKey (subKeyName, true);
649 } catch (ArgumentException ex) {
650 // Cannot delete a subkey tree because the subkey does not exist
651 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
652 Assert.IsNotNull (ex.Message, "#B3");
653 Assert.IsNull (ex.InnerException, "#B4");
656 Registry.CurrentUser.DeleteSubKey (subKeyName, false);
660 public void DeleteSubKey_Key_Removed ()
662 string subKeyName = Guid.NewGuid ().ToString ();
665 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
666 // check if key was successfully created
667 Assert.IsNotNull (createdKey, "#1");
668 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
671 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
672 Assert.IsNotNull (createdKey, "#2");
673 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
674 Assert.IsNotNull (createdKey, "#3");
676 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
677 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
679 createdKey.DeleteSubKey ("monotemp");
681 } catch (ArgumentException ex) {
682 // Cannot delete a subkey tree because the subkey does
684 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
685 Assert.IsNotNull (ex.Message, "#7");
686 Assert.IsNull (ex.InnerException, "#8");
691 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
692 if (createdKey != null) {
694 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
702 [Category ("NotWorking")] // MS should not allow this
703 public void DeleteSubKey_Name_Empty ()
705 string subKeyName = Guid.NewGuid ().ToString ();
707 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
709 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
710 createdKey.DeleteSubKey (string.Empty);
713 createdKey = softwareKey.OpenSubKey (subKeyName);
714 Assert.IsNull (createdKey, "#1");
717 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
718 if (createdKey != null)
720 softwareKey.DeleteSubKeyTree (subKeyName);
728 public void DeleteSubKey_Name_Null ()
730 string subKeyName = Guid.NewGuid ().ToString ();
732 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
734 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
736 createdKey.DeleteSubKey (null);
738 } catch (ArgumentNullException ex) {
739 // Value cannot be null. Parameter name: subkey
740 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
741 Assert.IsNotNull (ex.Message, "#3");
742 Assert.IsNull (ex.InnerException, "#4");
746 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
747 if (createdKey != null)
749 softwareKey.DeleteSubKeyTree (subKeyName);
757 public void DeleteSubKeyTree ()
760 // - remove key with subkeys
761 // - remove key of which some subkeys are marked for deletion
762 // - remove key with values
766 [ExpectedException (typeof (ArgumentException))]
767 public void DeleteSubKeyTree_Key_DoesNotExist ()
769 // Cannot delete a subkey tree because the subkey does not exist
770 string subKeyName = Guid.NewGuid ().ToString ();
771 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
775 public void DeleteSubKeyTree_Key_ReadOnly ()
777 string subKeyName = Guid.NewGuid ().ToString ();
780 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
781 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
785 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
787 softwareKey.DeleteSubKeyTree (subKeyName);
789 } catch (UnauthorizedAccessException ex) {
790 // Cannot write to the registry key
791 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
792 Assert.IsNotNull (ex.Message, "#3");
793 Assert.IsNull (ex.InnerException, "#4");
798 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
799 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
800 if (createdKey != null)
802 softwareKey.DeleteSubKeyTree (subKeyName);
810 public void DeleteSubKeyTree_Key_Removed ()
812 string subKeyName = Guid.NewGuid ().ToString ();
815 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
816 // check if key was successfully created
817 Assert.IsNotNull (createdKey, "#1");
818 RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
821 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
822 Assert.IsNotNull (createdKey, "#2");
823 using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
824 Assert.IsNotNull (createdKey, "#3");
826 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
827 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
829 createdKey.DeleteSubKeyTree ("monotemp");
831 } catch (ArgumentException ex) {
832 // Cannot delete a subkey tree because the subkey does
834 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
835 Assert.IsNotNull (ex.Message, "#7");
836 Assert.IsNull (ex.InnerException, "#8");
841 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
842 if (createdKey != null) {
844 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
852 [Category ("NotWorking")] // MS should not allow this
853 public void DeleteSubKeyTree_Name_Empty ()
855 string subKeyName = Guid.NewGuid ().ToString ();
857 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
859 RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
860 createdKey.DeleteSubKeyTree (string.Empty);
863 createdKey = softwareKey.OpenSubKey (subKeyName);
864 Assert.IsNull (createdKey, "#1");
867 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
868 if (createdKey != null)
870 softwareKey.DeleteSubKeyTree (subKeyName);
878 public void DeleteSubKeyTree_Name_Null ()
880 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
882 softwareKey.DeleteSubKeyTree (null);
884 } catch (ArgumentNullException ex) {
885 // Value cannot be null. Parameter name: subkey
886 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
887 Assert.IsNotNull (ex.Message, "#3");
888 Assert.IsNull (ex.InnerException, "#4");
894 public void DeleteValue ()
896 string subKeyName = Guid.NewGuid ().ToString ();
899 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
900 // check if key was successfully created
901 Assert.IsNotNull (createdKey, "#A1");
902 createdKey.SetValue ("name1", "value1");
903 createdKey.SetValue ("name2", "value2");
904 string [] names = createdKey.GetValueNames ();
905 Assert.IsNotNull (names, "#A2");
906 Assert.AreEqual (2, names.Length, "#A3");
907 Assert.IsNotNull (names [0], "#A4");
908 Assert.AreEqual ("name1", names [0], "#A5");
909 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A6");
910 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A7");
911 Assert.AreEqual ("name2", names [1], "#A8");
912 Assert.IsNotNull (createdKey.GetValue ("name2"), "#A9");
913 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#A10");
915 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
916 Assert.IsNotNull (createdKey, "#B1");
917 createdKey.DeleteValue ("name1");
918 string [] names = createdKey.GetValueNames ();
919 Assert.IsNotNull (names, "#B2");
920 Assert.AreEqual (1, names.Length, "#B3");
921 Assert.IsNotNull (names [0], "#B4");
922 Assert.AreEqual ("name2", names [0], "#B5");
923 Assert.IsNotNull (createdKey.GetValue ("name2"), "#B6");
924 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#B7");
926 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
927 string [] names = createdKey.GetValueNames ();
928 Assert.IsNotNull (names, "#C1");
929 Assert.AreEqual (1, names.Length, "#C2");
930 Assert.IsNotNull (names [0], "#C3");
931 Assert.AreEqual ("name2", names [0], "#C4");
932 Assert.IsNotNull (createdKey.GetValue ("name2"), "#C5");
933 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#C6");
937 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
938 if (createdKey != null) {
940 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
948 public void DeleteValue_Key_ReadOnly ()
950 string subKeyName = Guid.NewGuid ().ToString ();
953 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
954 createdKey.SetValue ("name1", "value1");
957 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
959 // deleting value that exists
960 createdKey.DeleteValue ("name1");
962 } catch (UnauthorizedAccessException ex) {
963 // Cannot write to the registry key
964 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#A2");
965 Assert.IsNotNull (ex.Message, "#A3");
966 Assert.IsNull (ex.InnerException, "#A4");
970 // deleting value that exists
971 createdKey.DeleteValue ("name1", true);
973 } catch (UnauthorizedAccessException ex) {
974 // Cannot write to the registry key
975 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#B2");
976 Assert.IsNotNull (ex.Message, "#B3");
977 Assert.IsNull (ex.InnerException, "#B4");
981 // deleting value that exists
982 createdKey.DeleteValue ("name1", false);
984 } catch (UnauthorizedAccessException ex) {
985 // Cannot write to the registry key
986 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#C2");
987 Assert.IsNotNull (ex.Message, "#C3");
988 Assert.IsNull (ex.InnerException, "#C4");
992 // deleting value that does not exist
993 createdKey.DeleteValue ("name2");
995 } catch (UnauthorizedAccessException ex) {
996 // Cannot write to the registry key
997 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#D2");
998 Assert.IsNotNull (ex.Message, "#D3");
999 Assert.IsNull (ex.InnerException, "#D4");
1003 // deleting value that does not exist
1004 createdKey.DeleteValue ("name2", true);
1005 Assert.Fail ("#E1");
1006 } catch (UnauthorizedAccessException ex) {
1007 // Cannot write to the registry key
1008 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#E2");
1009 Assert.IsNotNull (ex.Message, "#E3");
1010 Assert.IsNull (ex.InnerException, "#E4");
1014 // deleting value that does not exist
1015 createdKey.DeleteValue ("name2", false);
1016 Assert.Fail ("#F1");
1017 } catch (UnauthorizedAccessException ex) {
1018 // Cannot write to the registry key
1019 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#F2");
1020 Assert.IsNotNull (ex.Message, "#F3");
1021 Assert.IsNull (ex.InnerException, "#F4");
1026 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1027 if (createdKey != null) {
1028 createdKey.Close ();
1029 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1037 public void DeleteValue_Key_Removed ()
1039 string subKeyName = Guid.NewGuid ().ToString ();
1042 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1043 // check if key was successfully created
1044 Assert.IsNotNull (createdKey, "#1");
1045 createdKey.SetValue ("name1", "value1");
1047 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1048 Assert.IsNotNull (createdKey, "#2");
1049 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1050 Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#3");
1052 createdKey.DeleteValue ("name1");
1053 createdKey.DeleteValue ("name1", true);
1054 createdKey.DeleteValue ("name1", false);
1058 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1059 if (createdKey != null) {
1060 createdKey.Close ();
1061 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1069 public void DeleteValue_Value_DoesNotExist ()
1071 string subKeyName = Guid.NewGuid ().ToString ();
1074 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1075 // check if key was successfully created
1076 Assert.IsNotNull (createdKey, "#A1");
1077 createdKey.SetValue ("name1", "value1");
1080 createdKey.DeleteValue ("name2");
1081 Assert.Fail ("#B1");
1082 } catch (ArgumentException ex) {
1083 // No value exists with that name
1084 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1085 Assert.IsNotNull (ex.Message, "#B3");
1086 Assert.IsNull (ex.InnerException, "#B4");
1090 createdKey.DeleteValue ("name2", true);
1091 Assert.Fail ("#C1");
1092 } catch (ArgumentException ex) {
1093 // No value exists with that name
1094 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1095 Assert.IsNotNull (ex.Message, "#C3");
1096 Assert.IsNull (ex.InnerException, "#C4");
1099 createdKey.DeleteValue ("name2", false);
1103 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1104 if (createdKey != null) {
1105 createdKey.Close ();
1106 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1114 public void DeleteValue_Name_Empty ()
1116 string subKeyName = Guid.NewGuid ().ToString ();
1119 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1120 createdKey.SetValue ("name1", "value1");
1121 createdKey.SetValue (string.Empty, "value2");
1123 string [] names = createdKey.GetValueNames ();
1124 Assert.IsNotNull (names, "#A1");
1125 Assert.AreEqual (2, names.Length, "#A2");
1126 Assert.IsNotNull (names [0], "#A3");
1128 Assert.AreEqual ("name1", names [0], "#A4");
1130 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1131 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1132 Assert.IsNotNull (names [1], "#A7");
1134 Assert.AreEqual (string.Empty, names [1], "#A8");
1136 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A9");
1137 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#A10");
1139 createdKey.DeleteValue (string.Empty);
1141 names = createdKey.GetValueNames ();
1142 Assert.IsNotNull (names, "#B1");
1143 Assert.AreEqual (1, names.Length, "#B2");
1144 Assert.IsNotNull (names [0], "#B3");
1145 Assert.AreEqual ("name1", names [0], "#B4");
1146 Assert.IsNotNull (createdKey.GetValue ("name1"), "#B5");
1147 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#B6");
1150 createdKey.DeleteValue (string.Empty);
1151 Assert.Fail ("#C1");
1152 } catch (ArgumentException ex) {
1153 // No value exists with that name
1154 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1155 Assert.IsNotNull (ex.Message, "#C3");
1156 Assert.IsNull (ex.InnerException, "#C4");
1160 createdKey.DeleteValue (string.Empty, true);
1161 Assert.Fail ("#D1");
1162 } catch (ArgumentException ex) {
1163 // No value exists with that name
1164 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
1165 Assert.IsNotNull (ex.Message, "#D3");
1166 Assert.IsNull (ex.InnerException, "#D4");
1169 createdKey.DeleteValue (string.Empty, false);
1171 names = createdKey.GetValueNames ();
1172 Assert.IsNotNull (names, "#E1");
1173 Assert.AreEqual (1, names.Length, "#E2");
1174 Assert.IsNotNull (names [0], "#E3");
1175 Assert.AreEqual ("name1", names [0], "#E4");
1176 Assert.IsNotNull (createdKey.GetValue ("name1"), "#E5");
1177 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#E6");
1181 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1182 if (createdKey != null) {
1183 createdKey.Close ();
1184 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1192 public void DeleteValue_Name_Null ()
1194 string subKeyName = Guid.NewGuid ().ToString ();
1197 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1198 createdKey.SetValue ("name1", "value1");
1199 createdKey.SetValue (null, "value2");
1201 string [] names = createdKey.GetValueNames ();
1202 Assert.IsNotNull (names, "#A1");
1203 Assert.AreEqual (2, names.Length, "#A2");
1204 Assert.IsNotNull (names [0], "#A3");
1206 Assert.AreEqual ("name1", names [0], "#A4");
1208 Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1209 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1210 Assert.IsNotNull (names [1], "#A7");
1212 Assert.AreEqual (string.Empty, names [1], "#A8");
1214 Assert.IsNotNull (createdKey.GetValue (null), "#A9");
1215 Assert.AreEqual ("value2", createdKey.GetValue (null), "#A10");
1218 createdKey.DeleteValue (null);
1219 Assert.Fail ("#B1");
1220 } catch (ArgumentNullException ex) {
1221 // Value cannot be null. Parameter name: name
1222 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1223 Assert.IsNotNull (ex.Message, "#B3");
1224 Assert.IsNull (ex.InnerException, "#B4");
1228 createdKey.DeleteValue (null, true);
1229 Assert.Fail ("#C1");
1230 } catch (ArgumentNullException ex) {
1231 // Value cannot be null. Parameter name: name
1232 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
1233 Assert.IsNotNull (ex.Message, "#C3");
1234 Assert.IsNull (ex.InnerException, "#C4");
1238 createdKey.DeleteValue (null, false);
1239 Assert.Fail ("#D1");
1240 } catch (ArgumentNullException ex) {
1241 // Value cannot be null. Parameter name: name
1242 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
1243 Assert.IsNotNull (ex.Message, "#D3");
1244 Assert.IsNull (ex.InnerException, "#D4");
1249 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1250 if (createdKey != null) {
1251 createdKey.Close ();
1252 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1260 public void GetValue ()
1262 string subKeyName = Guid.NewGuid ().ToString ();
1265 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1266 createdKey.SetValue ("name1", "value1");
1267 createdKey.SetValue ("name2", "value2");
1270 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1271 Assert.IsNotNull (createdKey.GetValue ("name1"), "#1");
1272 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#2");
1273 Assert.IsNotNull (createdKey.GetValue ("name2"), "#3");
1274 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#4");
1275 Assert.IsNull (createdKey.GetValue ("name3"), "#5");
1276 Assert.AreEqual ("value3", createdKey.GetValue ("name3", "value3"), "#6");
1277 Assert.IsNull (createdKey.GetValue ("name3", null), "#7");
1281 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1282 if (createdKey != null) {
1283 createdKey.Close ();
1284 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1292 public void GetValue_Key_Removed ()
1294 string subKeyName = Guid.NewGuid ().ToString ();
1297 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1298 createdKey.SetValue ("name1", "value1");
1299 createdKey.SetValue ("name2", "value2");
1302 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1303 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1305 Assert.IsNull (createdKey.GetValue ("name1"), "#1");
1306 Assert.IsNotNull (createdKey.GetValue ("name1", "default"), "#2");
1307 Assert.AreEqual ("default", createdKey.GetValue ("name1", "default"), "#3");
1308 Assert.IsNull (createdKey.GetValue ("name3"), "#3");
1309 Assert.IsNotNull (createdKey.GetValue ("name3", "default"), "#4");
1310 Assert.AreEqual ("default", createdKey.GetValue ("name3", "default"), "#5");
1314 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1315 if (createdKey != null) {
1316 createdKey.Close ();
1317 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1325 public void GetValue_Name_Empty ()
1327 string subKeyName = Guid.NewGuid ().ToString ();
1330 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1331 createdKey.SetValue ("name1", "value1");
1332 createdKey.SetValue ("name2", "value2");
1334 Assert.IsNull (createdKey.GetValue (string.Empty), "#A1");
1335 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#A2");
1336 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#A3");
1337 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#A4");
1340 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1341 Assert.IsNull (createdKey.GetValue (string.Empty), "#B1");
1342 Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#B2");
1343 Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#B3");
1344 Assert.IsNull (createdKey.GetValue (string.Empty, null), "#B4");
1347 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1348 createdKey.SetValue (string.Empty, "value1");
1349 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#C1");
1350 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#C2");
1351 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#C3");
1352 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#C4");
1355 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1356 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#D1");
1357 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#D2");
1358 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#D3");
1359 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#D4");
1363 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1364 if (createdKey != null) {
1365 createdKey.Close ();
1366 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1374 public void GetValue_Name_Null ()
1376 string subKeyName = Guid.NewGuid ().ToString ();
1379 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1380 createdKey.SetValue ("name1", "value1");
1381 createdKey.SetValue ("name2", "value2");
1383 Assert.IsNull (createdKey.GetValue (null), "#A1");
1384 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#A2");
1385 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#A3");
1386 Assert.IsNull (createdKey.GetValue (null, null), "#A4");
1389 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1390 Assert.IsNull (createdKey.GetValue (null), "#B1");
1391 Assert.IsNotNull (createdKey.GetValue (null, "default"), "#B2");
1392 Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#B3");
1393 Assert.IsNull (createdKey.GetValue (null, null), "#B4");
1396 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1397 createdKey.SetValue (string.Empty, "value1");
1398 Assert.IsNotNull (createdKey.GetValue (null), "#C1");
1399 Assert.AreEqual ("value1", createdKey.GetValue (null), "#C2");
1400 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#C3");
1401 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#C4");
1404 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1405 Assert.IsNotNull (createdKey.GetValue (null), "#D1");
1406 Assert.AreEqual ("value1", createdKey.GetValue (null), "#D2");
1407 Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#D3");
1408 Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#D4");
1412 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1413 if (createdKey != null) {
1414 createdKey.Close ();
1415 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1424 public void GetValue_Expand ()
1426 string subKeyName = Guid.NewGuid ().ToString ();
1429 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1430 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1431 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1433 createdKey.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1434 RegistryValueKind.ExpandString);
1435 createdKey.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1436 createdKey.SetValue ("name3", "just some text",
1437 RegistryValueKind.ExpandString);
1439 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#A1");
1440 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#A2");
1441 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#A3");
1442 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1443 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A4");
1444 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1445 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A5");
1446 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1447 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A6");
1448 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1449 null, RegistryValueOptions.None), "#A7");
1450 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1451 null, RegistryValueOptions.None), "#A8");
1452 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1453 null, RegistryValueOptions.None), "#A9");
1455 Environment.SetEnvironmentVariable ("MONO_TEST1", "789");
1456 Environment.SetEnvironmentVariable ("MONO_TEST2", "666");
1458 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#B1");
1459 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#B2");
1460 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#B3");
1461 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1462 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B4");
1463 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1464 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B5");
1465 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1466 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B6");
1467 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1468 null, RegistryValueOptions.None), "#B7");
1469 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1470 null, RegistryValueOptions.None), "#B8");
1471 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1472 null, RegistryValueOptions.None), "#B9");
1474 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1475 Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#C1");
1476 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#C2");
1477 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#C3");
1478 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1479 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C4");
1480 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1481 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C5");
1482 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1483 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C6");
1484 Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1485 null, RegistryValueOptions.None), "#C7");
1486 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1487 null, RegistryValueOptions.None), "#C8");
1488 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1489 null, RegistryValueOptions.None), "#C9");
1491 Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1492 Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1494 Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#D1");
1495 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#D2");
1496 Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#D3");
1497 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1498 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D4");
1499 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1500 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D5");
1501 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1502 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D6");
1503 Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1504 null, RegistryValueOptions.None), "#D7");
1505 Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1506 null, RegistryValueOptions.None), "#D8");
1507 Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1508 null, RegistryValueOptions.None), "#D9");
1512 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1513 if (createdKey != null) {
1514 createdKey.Close ();
1515 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1524 public void GetValueNames ()
1526 string subKeyName = Guid.NewGuid ().ToString ();
1529 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1530 string [] names = createdKey.GetValueNames ();
1531 Assert.IsNotNull (names, "#A1");
1532 Assert.AreEqual (0, names.Length, "#A2");
1534 createdKey.SetValue ("name1", "value1");
1535 createdKey.SetValue ("name2", "value2");
1536 createdKey.SetValue ("namelong", "value3");
1537 createdKey.SetValue ("name3", "value4");
1539 Assert.AreEqual (4, createdKey.ValueCount, "#B1");
1540 names = createdKey.GetValueNames ();
1541 Assert.IsNotNull (names, "#B2");
1542 Assert.AreEqual (4, names.Length, "#B3");
1545 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1546 string [] names = createdKey.GetValueNames ();
1547 Assert.IsNotNull (names, "#C1");
1548 Assert.AreEqual (4, names.Length, "#C2");
1550 // Mono's Unix registry API uses a hashtable to store the
1551 // values (and their names), so names are not returned in
1554 // to test whether the names returned by GetValueNames
1555 // match what we expect, we use these names to remove the
1556 // the values from the created keys and such we should end
1557 // up with zero values
1558 for (int i = 0; i < names.Length; i++) {
1559 string valueName = names [i];
1560 createdKey.DeleteValue (valueName);
1563 // all values should be removed now
1564 Assert.AreEqual (0, createdKey.ValueCount, "#C3");
1568 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1569 if (createdKey != null) {
1570 createdKey.Close ();
1571 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1579 public void GetValueNames_Key_Removed ()
1581 string subKeyName = Guid.NewGuid ().ToString ();
1584 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1585 createdKey.SetValue ("name1", "value1");
1586 createdKey.SetValue ("name2", "value2");
1588 string [] names = createdKey.GetValueNames ();
1589 Assert.IsNotNull (names, "#A1");
1590 Assert.AreEqual (2, names.Length, "#A2");
1593 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1594 string [] names = createdKey.GetValueNames ();
1595 Assert.IsNotNull (names, "#B1");
1596 Assert.AreEqual (2, names.Length, "#B2");
1598 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1601 createdKey.GetValueNames ();
1602 Assert.Fail ("#C1");
1603 } catch (IOException ex) {
1604 // Illegal operation attempted on a registry key that
1605 // has been marked for deletion
1606 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
1607 Assert.IsNotNull (ex.Message, "#C3");
1608 Assert.IsNull (ex.InnerException, "#C4");
1613 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1614 if (createdKey != null) {
1615 createdKey.Close ();
1616 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1623 [Test] // bug #78519
1624 public void GetSubKeyNamesTest ()
1626 string subKeyName = Guid.NewGuid ().ToString ();
1628 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1630 // check if key was successfully created
1631 Assert.IsNotNull (createdKey, "#A");
1633 RegistryKey subKey = createdKey.CreateSubKey ("foo");
1634 Assert.IsNotNull (subKey, "#B1");
1635 Assert.AreEqual (1, createdKey.SubKeyCount, "#B2");
1636 string[] subKeyNames = createdKey.GetSubKeyNames ();
1637 Assert.IsNotNull (subKeyNames, "#B3");
1638 Assert.AreEqual (1, subKeyNames.Length, "#B4");
1639 Assert.AreEqual ("foo", subKeyNames[0], "#B5");
1641 subKey = createdKey.CreateSubKey ("longfoo");
1642 Assert.IsNotNull (subKey, "#C1");
1643 Assert.AreEqual (2, createdKey.SubKeyCount, "#C2");
1644 subKeyNames = createdKey.GetSubKeyNames ();
1645 Assert.IsNotNull (subKeyNames, "#C3");
1646 Assert.AreEqual (2, subKeyNames.Length, "#C4");
1647 Assert.AreEqual ("foo", subKeyNames [0], "#C5");
1648 Assert.AreEqual ("longfoo", subKeyNames [1], "#C6");
1650 subKey = createdKey.CreateSubKey ("sfoo");
1651 Assert.IsNotNull (subKey, "#D1");
1652 Assert.AreEqual (3, createdKey.SubKeyCount, "#D2");
1653 subKeyNames = createdKey.GetSubKeyNames ();
1654 Assert.IsNotNull (subKeyNames, "#D3");
1655 Assert.AreEqual (3, subKeyNames.Length, "#D4");
1656 Assert.AreEqual ("foo", subKeyNames [0], "#D5");
1657 Assert.AreEqual ("longfoo", subKeyNames [1], "#D6");
1658 Assert.AreEqual ("sfoo", subKeyNames [2], "#D7");
1660 foreach (string name in subKeyNames) {
1661 createdKey.DeleteSubKeyTree (name);
1663 Assert.AreEqual (0, createdKey.SubKeyCount, "#E");
1666 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1671 public void OpenRemoteBaseKey ()
1673 // access to registry of remote machines is not implemented on unix
1677 RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
1678 RegistryHive.CurrentUser, Environment.MachineName);
1679 Assert.IsNotNull (hive, "#1");
1681 RegistryKey key = hive.OpenSubKey ("SOFTWARE");
1682 Assert.IsNotNull (key, "#2");
1689 [ExpectedException (typeof (ArgumentNullException))]
1690 public void OpenRemoteBaseKey_MachineName_Null ()
1692 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser, null);
1696 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
1698 // access to registry of remote machines is not implemented on unix
1703 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
1706 } catch (IOException ex) {
1707 // The network path was not found
1708 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1709 Assert.IsNotNull (ex.Message, "#3");
1710 Assert.IsNull (ex.InnerException, "#4");
1715 public void SetValue_Name_Null ()
1717 string subKeyName = Guid.NewGuid ().ToString ();
1719 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1721 createdKey.SetValue (null, "value1");
1722 string [] names = createdKey.GetValueNames ();
1723 Assert.IsNotNull (names, "#A1");
1724 Assert.AreEqual (1, names.Length, "#A2");
1725 Assert.IsNotNull (names [0], "#A3");
1726 Assert.AreEqual (string.Empty, names [0], "#A4");
1727 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
1728 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
1729 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
1730 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
1732 createdKey.SetValue (string.Empty, "value2");
1733 names = createdKey.GetValueNames ();
1734 Assert.IsNotNull (names, "#B1");
1735 Assert.AreEqual (1, names.Length, "#B2");
1736 Assert.IsNotNull (names [0], "#B3");
1737 Assert.AreEqual (string.Empty, names [0], "#B4");
1738 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
1739 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
1740 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
1741 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
1744 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1749 public void SetValue_Name_Empty ()
1751 string subKeyName = Guid.NewGuid ().ToString ();
1753 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1755 createdKey.SetValue (string.Empty, "value1");
1756 string [] names = createdKey.GetValueNames ();
1757 Assert.IsNotNull (names, "#A1");
1758 Assert.AreEqual (1, names.Length, "#A2");
1759 Assert.IsNotNull (names [0], "#A3");
1760 Assert.AreEqual (string.Empty, names [0], "#A4");
1761 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
1762 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
1763 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
1764 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
1766 createdKey.SetValue (null, "value2");
1767 names = createdKey.GetValueNames ();
1768 Assert.IsNotNull (names, "#B1");
1769 Assert.AreEqual (1, names.Length, "#B2");
1770 Assert.IsNotNull (names [0], "#B3");
1771 Assert.AreEqual (string.Empty, names [0], "#B4");
1772 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
1773 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
1774 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
1775 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
1778 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1783 [ExpectedException (typeof (ArgumentNullException))]
1784 public void SetValue_Null ()
1786 string subKeyName = Guid.NewGuid ().ToString ();
1788 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1790 // null value should result in ArgumentNullException
1791 createdKey.SetValue ("Name", null);
1794 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1799 public void SetValue_Boolean ()
1801 string subKeyName = Guid.NewGuid ().ToString ();
1803 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1805 // we created a new subkey, so value should not exist
1806 Assert.IsNull (createdKey.GetValue ("Installed"), "#1");
1808 createdKey.SetValue ("Installed", true);
1810 object value = createdKey.GetValue ("Installed");
1811 // value should exist
1812 Assert.IsNotNull (value, "#2");
1813 // type of value should be string
1814 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1815 // ensure value matches
1816 Assert.AreEqual (true.ToString (), value, "#4");
1819 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1824 public void SetValue_Byte ()
1826 string subKeyName = Guid.NewGuid ().ToString ();
1828 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1830 // we created a new subkey, so value should not exist
1831 Assert.IsNull (createdKey.GetValue ("Flags"), "#1");
1833 createdKey.SetValue ("Flags", (byte) 5);
1835 object value = createdKey.GetValue ("Flags");
1836 // value should exist
1837 Assert.IsNotNull (value, "#2");
1838 // type of value should be string
1839 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1840 // ensure value matches
1841 Assert.AreEqual ("5", value, "#4");
1844 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1849 public void SetValue_ByteArray ()
1851 string subKeyName = Guid.NewGuid ().ToString ();
1853 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1855 // we created a new subkey, so value should not exist
1856 Assert.IsNull (createdKey.GetValue ("Flags"), "#1");
1858 createdKey.SetValue ("Flags", new byte[] { 1, 5 });
1860 object value = createdKey.GetValue ("Flags");
1861 // value should exist
1862 Assert.IsNotNull (value, "#2");
1863 // type of value should be string
1864 Assert.AreEqual (typeof (byte[]), value.GetType (), "#3");
1865 // ensure value matches
1866 Assert.AreEqual (new byte[] { 1, 5 }, value, "#4");
1869 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1874 public void SetValue_DateTime ()
1876 string subKeyName = Guid.NewGuid ().ToString ();
1878 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1880 object rawValue = DateTime.Now;
1882 // we created a new subkey, so value should not exist
1883 Assert.IsNull (createdKey.GetValue ("Path"), "#1");
1885 createdKey.SetValue ("Path", rawValue);
1887 object value = createdKey.GetValue ("Path");
1888 // value should exist
1889 Assert.IsNotNull (value, "#2");
1890 // type of value should be string
1891 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1892 // ensure value matches
1893 Assert.AreEqual (rawValue.ToString (), value, "#4");
1896 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1901 public void SetValue_Int32 ()
1903 string subKeyName = Guid.NewGuid ().ToString ();
1905 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1907 // we created a new subkey, so value should not exist
1908 Assert.IsNull (createdKey.GetValue ("RefCount"), "#1");
1910 createdKey.SetValue ("RefCount", 5);
1912 object value = createdKey.GetValue ("RefCount");
1913 // value should exist
1914 Assert.IsNotNull (value, "#2");
1915 // type of value should be int
1916 Assert.AreEqual (typeof (int), value.GetType (), "#3");
1917 // ensure value matches
1918 Assert.AreEqual (5, value, "#4");
1921 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1926 public void SetValue_Int64 ()
1928 string subKeyName = Guid.NewGuid ().ToString ();
1930 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1932 // we created a new subkey, so value should not exist
1933 Assert.IsNull (createdKey.GetValue ("Ticks"), "#1");
1935 createdKey.SetValue ("Ticks", 500L);
1937 object value = createdKey.GetValue ("Ticks");
1938 // value should exist
1939 Assert.IsNotNull (value, "#2");
1940 // type of value should be string
1941 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1942 // ensure value matches
1943 Assert.AreEqual ("500", value, "#4");
1946 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1951 public void SetValue_String ()
1953 string subKeyName = Guid.NewGuid ().ToString ();
1955 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1957 // we created a new subkey, so value should not exist
1958 Assert.IsNull (createdKey.GetValue ("Path"), "#1");
1960 createdKey.SetValue ("Path", "/usr/lib/whatever");
1962 object value = createdKey.GetValue ("Path");
1963 // value should exist
1964 Assert.IsNotNull (value, "#2");
1965 // type of value should be string
1966 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1967 // ensure value matches
1968 Assert.AreEqual ("/usr/lib/whatever", value, "#4");
1971 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1976 public void SetValue_StringArray ()
1978 string subKeyName = Guid.NewGuid ().ToString ();
1980 RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1982 // we created a new subkey, so value should not exist
1983 Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#1");
1985 createdKey.SetValue ("DependsOnGroup", new string[] { "A", "B" });
1987 object value = createdKey.GetValue ("DependsOnGroup");
1988 // value should exist
1989 Assert.IsNotNull (value, "#2");
1990 // type of value should be string
1991 Assert.AreEqual (typeof (string[]), value.GetType (), "#3");
1992 // ensure value matches
1993 Assert.AreEqual (new string[] { "A", "B" }, value, "#4");
1996 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2001 public void SetValue_Key_ReadOnly ()
2003 string subKeyName = Guid.NewGuid ().ToString ();
2005 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2007 softwareKey.SetValue ("name1", "value1");
2009 } catch (UnauthorizedAccessException ex) {
2010 // Cannot write to the registry key
2011 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2012 Assert.IsNotNull (ex.Message, "#3");
2013 Assert.IsNull (ex.InnerException, "#4");
2017 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2019 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2022 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2024 createdKey.SetValue ("name1", "value1");
2026 } catch (UnauthorizedAccessException ex) {
2027 // Cannot write to the registry key
2028 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2029 Assert.IsNotNull (ex.Message, "#3");
2030 Assert.IsNull (ex.InnerException, "#4");
2035 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2036 if (createdKey != null) {
2037 createdKey.Close ();
2038 softwareKey.DeleteSubKeyTree (subKeyName);
2047 public void SetValue_Key_Removed ()
2049 string subKeyName = Guid.NewGuid ().ToString ();
2051 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2053 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2054 softwareKey.DeleteSubKeyTree (subKeyName);
2055 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2057 createdKey.SetValue ("name1", "value1");
2059 } catch (IOException ex) {
2060 // Illegal operation attempted on a registry key that
2061 // has been marked for deletion
2062 Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2063 Assert.IsNotNull (ex.Message, "#4");
2064 Assert.IsNull (ex.InnerException, "#5");
2069 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2070 if (createdKey != null) {
2071 createdKey.Close ();
2072 softwareKey.DeleteSubKeyTree (subKeyName);
2081 public void SubKeyCount ()
2083 string subKeyName = Guid.NewGuid ().ToString ();
2086 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2087 // check if key was successfully created
2088 Assert.IsNotNull (createdKey, "#A1");
2089 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2092 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2093 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2096 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2098 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2099 Assert.IsNotNull (createdKey, "#B1");
2100 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
2102 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2103 Assert.IsNotNull (createdKey2, "#B3");
2104 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
2105 createdKey2.DeleteSubKey ("monotemp1");
2106 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
2108 Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
2112 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2113 if (createdKey != null) {
2114 createdKey.Close ();
2115 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2123 public void SubKeyCount_Key_Removed ()
2125 string subKeyName = Guid.NewGuid ().ToString ();
2128 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2129 // check if key was successfully created
2130 Assert.IsNotNull (createdKey, "#A1");
2131 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2134 Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2135 using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2138 Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2140 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2141 Assert.IsNotNull (createdKey, "#B1");
2142 Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
2144 // remove created key
2145 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2148 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
2149 } catch (IOException ex) {
2150 // Illegal operation attempted on a registry key that
2151 // has been marked for deletion
2152 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
2153 Assert.IsNotNull (ex.Message, "#15");
2154 Assert.IsNull (ex.InnerException, "#16");
2159 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2160 if (createdKey != null) {
2161 createdKey.Close ();
2162 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2170 public void ValueCount ()
2172 string subKeyName = Guid.NewGuid ().ToString ();
2175 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2176 // check if key was successfully created
2177 Assert.IsNotNull (createdKey, "#A1");
2178 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
2179 createdKey.SetValue ("name1", "value1");
2180 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
2181 createdKey.SetValue ("name2", "value2");
2182 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
2183 createdKey.SetValue ("name2", "value2b");
2184 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
2185 createdKey.SetValue ("name3", "value3");
2186 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
2188 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2189 Assert.IsNotNull (createdKey, "#B1");
2190 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
2192 using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2193 Assert.IsNotNull (createdKey2, "#B3");
2194 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
2195 createdKey2.DeleteValue ("name2");
2196 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
2198 Assert.AreEqual (2, createdKey.ValueCount, "#B6");
2202 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2203 if (createdKey != null) {
2204 createdKey.Close ();
2205 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2213 public void ValueCount_Key_Removed ()
2215 string subKeyName = Guid.NewGuid ().ToString ();
2218 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2219 // check if key was successfully created
2220 Assert.IsNotNull (createdKey, "#A1");
2221 Assert.AreEqual (0, createdKey.ValueCount, "#A2");
2222 createdKey.SetValue ("name1", "value1");
2223 Assert.AreEqual (1, createdKey.ValueCount, "#A3");
2224 createdKey.SetValue ("name2", "value2");
2225 Assert.AreEqual (2, createdKey.ValueCount, "#A4");
2226 createdKey.SetValue ("name2", "value2b");
2227 Assert.AreEqual (2, createdKey.ValueCount, "#A5");
2228 createdKey.SetValue ("name3", "value3");
2229 Assert.AreEqual (3, createdKey.ValueCount, "#A6");
2231 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2232 Assert.IsNotNull (createdKey, "#B1");
2233 Assert.AreEqual (3, createdKey.ValueCount, "#B2");
2235 // remove created key
2236 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2239 Assert.Fail ("#C1: " + createdKey.ValueCount);
2240 } catch (IOException ex) {
2241 // Illegal operation attempted on a registry key that
2242 // has been marked for deletion
2243 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
2244 Assert.IsNotNull (ex.Message, "#15");
2245 Assert.IsNull (ex.InnerException, "#16");
2250 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2251 if (createdKey != null) {
2252 createdKey.Close ();
2253 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2261 public void bug79051 ()
2263 string subKeyName = Guid.NewGuid ().ToString ();
2265 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2267 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2268 createdKey.SetValue ("test", "whatever");
2269 createdKey.Close ();
2270 softwareKey.DeleteSubKeyTree (subKeyName);
2274 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2275 if (createdKey != null) {
2276 createdKey.Close ();
2277 softwareKey.DeleteSubKeyTree (subKeyName);
2286 public void bug79059 ()
2288 string subKeyName = Guid.NewGuid ().ToString ();
2290 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2292 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2293 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
2295 createdKey.Close ();
2296 softwareKey.DeleteSubKeyTree (subKeyName);
2300 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2301 if (createdKey != null) {
2302 createdKey.Close ();
2303 softwareKey.DeleteSubKeyTree (subKeyName);
2312 public void bugnew1 ()
2314 string subKeyName = Guid.NewGuid ().ToString ();
2316 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2318 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2319 createdKey.SetValue ("name1", "value1");
2321 RegistryKey testKey = null;
2323 testKey = createdKey.OpenSubKey ("test", true);
2324 if (testKey == null)
2325 testKey = createdKey.CreateSubKey ("test");
2326 testKey.SetValue ("another", "one");
2328 if (testKey != null)
2332 createdKey.SetValue ("name2", "value2");
2333 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
2334 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
2335 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
2336 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
2338 string [] names = createdKey.GetValueNames ();
2339 Assert.IsNotNull (names, "#6");
2340 Assert.AreEqual (2, names.Length, "#7");
2341 Assert.AreEqual ("name1", names [0], "#8");
2342 Assert.AreEqual ("name2", names [1], "#9");
2344 softwareKey.DeleteSubKeyTree (subKeyName);
2346 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
2347 Assert.IsNull (openedKey, "#10");
2350 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
2351 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
2354 createdKey.GetValueNames ();
2355 Assert.Fail ("#13");
2356 } catch (IOException ex) {
2357 // Illegal operation attempted on a registry key that
2358 // has been marked for deletion
2359 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
2360 Assert.IsNotNull (ex.Message, "#15");
2361 Assert.IsNull (ex.InnerException, "#16");
2365 createdKey.SetValue ("name1", "value1");
2366 Assert.Fail ("#17");
2367 } catch (IOException ex) {
2368 // Illegal operation attempted on a registry key that
2369 // has been marked for deletion
2370 Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
2371 Assert.IsNotNull (ex.Message, "#19");
2372 Assert.IsNull (ex.InnerException, "#20");
2376 createdKey.SetValue ("newname", "value1");
2377 Assert.Fail ("#21");
2378 } catch (IOException ex) {
2379 // Illegal operation attempted on a registry key that
2380 // has been marked for deletion
2381 Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
2382 Assert.IsNotNull (ex.Message, "#23");
2383 Assert.IsNull (ex.InnerException, "#24");
2386 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
2387 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
2388 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
2389 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
2392 createdKey.CreateSubKey ("new");
2393 Assert.Fail ("#29");
2394 } catch (IOException ex) {
2395 // Illegal operation attempted on a registry key that
2396 // has been marked for deletion
2397 Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
2398 Assert.IsNotNull (ex.Message, "#31");
2399 Assert.IsNull (ex.InnerException, "#32");
2404 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2405 if (createdKey != null) {
2406 createdKey.Close ();
2407 softwareKey.DeleteSubKeyTree (subKeyName);
2416 public void bugnew2 () // values cannot be written on registry root (hive)
2418 string [] names = Registry.CurrentUser.GetValueNames ();
2419 Assert.IsNotNull (names, "#1");
2420 Registry.CurrentUser.SetValue ("name1", "value1");
2421 Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
2422 Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
2423 string [] newNames = Registry.CurrentUser.GetValueNames ();
2424 Assert.IsNotNull (newNames, "#4");
2425 Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
2426 Registry.CurrentUser.DeleteValue ("name1");
2430 public void bugnew3 () // on Windows, key cannot be closed twice
2432 string subKeyName = Guid.NewGuid ().ToString ();
2435 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2436 createdKey.Close ();
2439 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2444 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2445 if (createdKey != null) {
2446 createdKey.Close ();
2447 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2455 public void bugnew4 () // Key cannot be flushed once it has been closed
2457 string subKeyName = Guid.NewGuid ().ToString ();
2460 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2461 createdKey.Close ();
2464 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2469 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2470 if (createdKey != null) {
2471 createdKey.Close ();
2472 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2479 private bool RunningOnUnix {
2482 return Environment.OSVersion.Platform == PlatformID.Unix;
2484 int p = (int) Environment.OSVersion.Platform;
2485 return ((p == 4) || (p == 128));