2 // RegistryKeyTest.cs - NUnit Test Cases for Microsoft.Win32.RegistryKey
5 // mei (mei@work.email.ne.jp)
6 // Robert Jordan (robertj@gmx.net)
7 // Gert Driesen (drieseng@users.sourceforge.net)
10 // (C) 2004, 2005 Novell (http://www.novell.com)
16 using Microsoft
.Win32
;
18 using NUnit
.Framework
;
20 namespace MonoTests
.Microsoft
.Win32
23 public class RegistryKeyTest
25 private const string mimeroot
= @"MIME\Database\Content Type";
28 [Category ("NotWorking")] // this will not work on Linux ever
29 public void TestGetValue ()
31 RegistryKey root
= Registry
.ClassesRoot
;
34 key
= root
.OpenSubKey (mimeroot
+ @"\audio/wav");
35 Assert
.AreEqual (".wav", key
.GetValue ("Extension"), "GetValue #1");
36 key
= root
.OpenSubKey (mimeroot
+ @"\text/x-scriptlet");
37 Assert
.AreEqual (null, key
.GetValue ("Extension"), "GetValue #2");
41 public void TestHandle ()
43 // this test is for Windows only
47 // this regpath always exists under windows
48 RegistryKey k
= Registry
.CurrentUser
49 .OpenSubKey ("Software", false)
50 .OpenSubKey ("Microsoft", false)
51 .OpenSubKey ("Windows", false);
53 Assert
.IsNotNull (k
, "#01");
57 public void OpenSubKey ()
59 RegistryKey key
= Registry
.LocalMachine
;
61 // HKEY_LOCAL_MACHINE\software should always exist on Windows
62 // and is automatically created on Linux
63 Assert
.IsNotNull (key
.OpenSubKey ("Software"), "#A1");
64 Assert
.IsNotNull (key
.OpenSubKey ("soFtware"), "#A2");
66 key
= Registry
.CurrentUser
;
68 // HKEY_CURRENT_USER\software should always exist on Windows
69 // and is automatically created on Linux
70 Assert
.IsNotNull (key
.OpenSubKey ("Software"), "#B1");
71 Assert
.IsNotNull (key
.OpenSubKey ("soFtware"), "#B2");
75 // HKEY_USERS\software should not exist on Windows, and should not
76 // be created automatically on Linux
77 Assert
.IsNull (key
.OpenSubKey ("Software"), "#C1");
78 Assert
.IsNull (key
.OpenSubKey ("soFtware"), "#C2");
82 public void OpenSubKey_Key_DoesNotExist ()
84 string subKeyName
= Guid
.NewGuid ().ToString ();
85 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (subKeyName
), "#1"); // read-only
86 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (subKeyName
, true), "#2"); // writable
90 public void OpenSubKey_Key_Removed ()
92 string subKeyName
= Guid
.NewGuid ().ToString ();
95 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
96 // check if key was successfully created
97 Assert
.IsNotNull (createdKey
, "#1");
98 RegistryKey subKey
= createdKey
.CreateSubKey ("monotemp");
101 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
102 Assert
.IsNotNull (createdKey
, "#2");
103 using (RegistryKey subKey
= createdKey
.OpenSubKey ("monotemp")) {
104 Assert
.IsNotNull (createdKey
, "#3");
106 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
109 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (subKeyName
), "#4");
110 Assert
.IsNull (createdKey
.OpenSubKey ("monotemp"), "#5"); // read-only
112 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (subKeyName
, true), "#6");
113 Assert
.IsNull (createdKey
.OpenSubKey ("monotemp", true), "#7");
117 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
118 if (createdKey
!= null) {
120 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
128 [Category ("NotWorking")] // MS should not allow this
129 public void OpenSubKey_Name_Empty ()
132 using (RegistryKey emptyKey
= Registry
.CurrentUser
.OpenSubKey (string.Empty
)) {
133 Assert
.IsNotNull (emptyKey
, "#1");
136 using (RegistryKey emptyKey
= Registry
.CurrentUser
.OpenSubKey (string.Empty
, true)) {
137 Assert
.IsNotNull (emptyKey
, "#1");
142 public void OpenSubKey_Name_MaxLength ()
144 string name
= new string ('a', 254);
146 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (name
), "#A1");
148 name
= new string ('a', 255);
150 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (name
), "#B1");
152 name
= new string ('a', 256);
155 Registry
.CurrentUser
.OpenSubKey (name
);
157 } catch (ArgumentException ex
) {
158 // 1.x: Registry subkeys should not be
159 // greater than or equal to 255 characters
161 // 2.x: Registry subkeys should not be
162 // greater than 255 characters
163 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#C2");
164 Assert
.IsNull (ex
.InnerException
, "#C3");
165 Assert
.IsNotNull (ex
.Message
, "#c4");
166 Assert
.IsTrue (ex
.Message
.IndexOf ("255") != -1, "#C5");
167 Assert
.IsNull (ex
.ParamName
, "#C6");
172 public void OpenSubKey_Name_Null ()
175 Registry
.CurrentUser
.OpenSubKey (null);
177 } catch (ArgumentNullException ex
) {
178 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#A2");
179 Assert
.IsNull (ex
.InnerException
, "#A3");
180 Assert
.IsNotNull (ex
.Message
, "#A4");
181 Assert
.AreEqual ("name", ex
.ParamName
, "#A5");
185 Registry
.CurrentUser
.OpenSubKey (null, true);
187 } catch (ArgumentNullException ex
) {
188 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#B2");
189 Assert
.IsNull (ex
.InnerException
, "#B3");
190 Assert
.IsNotNull (ex
.Message
, "#B4");
191 Assert
.AreEqual ("name", ex
.ParamName
, "#B5");
196 public void Close_Local_Hive ()
198 RegistryKey hive
= Registry
.CurrentUser
;
201 Assert
.IsNotNull (hive
.GetSubKeyNames (), "#1");
202 Assert
.IsNull (hive
.GetValue ("doesnotexist"), "#2");
203 Assert
.IsNotNull (hive
.GetValueNames (), "#3");
204 Assert
.IsNull (hive
.OpenSubKey ("doesnotexist"), "#4");
205 Assert
.IsNotNull (hive
.SubKeyCount
, "#5");
206 Assert
.IsNotNull (hive
.ToString (), "#6");
208 // closing key again does not have any effect
213 public void Close_Local_Key ()
215 RegistryKey key
= Registry
.CurrentUser
.OpenSubKey ("SOFTWARE");
218 // closing a key twice does not have any effect
222 key
.CreateSubKey ("a");
224 } catch (ObjectDisposedException
) {
228 key
.DeleteSubKey ("doesnotexist");
230 } catch (ObjectDisposedException
) {
234 key
.DeleteSubKeyTree ("doesnotexist");
236 } catch (ObjectDisposedException
) {
240 key
.DeleteValue ("doesnotexist");
242 } catch (ObjectDisposedException
) {
245 // flushing a closed key does not have any effect
249 key
.GetSubKeyNames ();
251 } catch (ObjectDisposedException
) {
255 key
.GetValue ("doesnotexist");
257 } catch (ObjectDisposedException
) {
261 key
.GetValueNames ();
263 } catch (ObjectDisposedException
) {
267 key
.OpenSubKey ("doesnotexist");
269 } catch (ObjectDisposedException
) {
273 key
.SetValue ("doesnotexist", "something");
275 } catch (ObjectDisposedException
) {
279 int x
= key
.SubKeyCount
;
280 Assert
.Fail ("#10:" + x
);
281 } catch (ObjectDisposedException
) {
287 } catch (ObjectDisposedException
) {
291 int x
= key
.ValueCount
;
292 Assert
.Fail ("#12:" + x
);
293 } catch (ObjectDisposedException
) {
298 public void Close_Remote_Hive ()
300 // access to registry of remote machines is not implemented on unix
304 RegistryKey hive
= RegistryKey
.OpenRemoteBaseKey (
305 RegistryHive
.CurrentUser
, Environment
.MachineName
);
308 // closing a remote hive twice does not have any effect
312 hive
.CreateSubKey ("a");
314 } catch (ObjectDisposedException
) {
318 hive
.DeleteSubKey ("doesnotexist");
320 } catch (ObjectDisposedException
) {
324 hive
.DeleteSubKeyTree ("doesnotexist");
326 } catch (ObjectDisposedException
) {
330 hive
.DeleteValue ("doesnotexist");
332 } catch (ObjectDisposedException
) {
335 // flushing a closed hive does not have any effect
339 hive
.GetSubKeyNames ();
341 } catch (ObjectDisposedException
) {
345 hive
.GetValue ("doesnotexist");
347 } catch (ObjectDisposedException
) {
351 hive
.GetValueNames ();
353 } catch (ObjectDisposedException
) {
357 hive
.OpenSubKey ("doesnotexist");
359 } catch (ObjectDisposedException
) {
363 hive
.SetValue ("doesnotexist", "something");
365 } catch (ObjectDisposedException
) {
369 int x
= hive
.SubKeyCount
;
370 Assert
.Fail ("#10:" + x
);
371 } catch (ObjectDisposedException
) {
377 } catch (ObjectDisposedException
) {
381 int x
= hive
.ValueCount
;
382 Assert
.Fail ("#12:" + x
);
383 } catch (ObjectDisposedException
) {
388 public void Close_Remote_Key ()
390 // access to registry of remote machines is not implemented on unix
394 RegistryKey hive
= RegistryKey
.OpenRemoteBaseKey (
395 RegistryHive
.CurrentUser
, Environment
.MachineName
);
396 RegistryKey key
= hive
.OpenSubKey ("SOFTWARE");
399 // closing a remote key twice does not have any effect
403 key
.CreateSubKey ("a");
405 } catch (ObjectDisposedException
) {
409 key
.DeleteSubKey ("doesnotexist");
411 } catch (ObjectDisposedException
) {
415 key
.DeleteSubKeyTree ("doesnotexist");
417 } catch (ObjectDisposedException
) {
421 key
.DeleteValue ("doesnotexist");
423 } catch (ObjectDisposedException
) {
426 // flushing a closed key does not have any effect
430 key
.GetSubKeyNames ();
432 } catch (ObjectDisposedException
) {
436 key
.GetValue ("doesnotexist");
438 } catch (ObjectDisposedException
) {
442 key
.GetValueNames ();
444 } catch (ObjectDisposedException
) {
448 key
.OpenSubKey ("doesnotexist");
450 } catch (ObjectDisposedException
) {
454 key
.SetValue ("doesnotexist", "something");
456 } catch (ObjectDisposedException
) {
460 int x
= key
.SubKeyCount
;
461 Assert
.Fail ("#10:" + x
);
462 } catch (ObjectDisposedException
) {
468 } catch (ObjectDisposedException
) {
472 int x
= key
.ValueCount
;
473 Assert
.Fail ("#12:" + x
);
474 } catch (ObjectDisposedException
) {
481 public void CreateSubKey ()
483 string subKeyName
= Guid
.NewGuid ().ToString ();
486 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
487 // check if key was successfully created
488 Assert
.IsNotNull (createdKey
, "#A1");
489 // software subkey should not be created automatically
490 Assert
.IsNull (createdKey
.OpenSubKey ("software"), "#A2");
493 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
494 // check if key was successfully created
495 Assert
.IsNotNull (createdKey
, "#B1");
496 // software subkey should not be created automatically
497 Assert
.IsNull (createdKey
.OpenSubKey ("software"), "#B2");
501 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
504 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
506 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
507 // check if key was successfully created
508 Assert
.IsNotNull (createdKey
, "#C1");
509 // software subkey should not be created automatically
510 Assert
.IsNull (softwareKey
.OpenSubKey ("software"), "#C2");
513 using (RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
)) {
514 // check if key was successfully created
515 Assert
.IsNotNull (createdKey
, "#D1");
516 // software subkey should not be created automatically
517 Assert
.IsNull (softwareKey
.OpenSubKey ("software"), "#D2");
521 softwareKey
.DeleteSubKeyTree (subKeyName
);
527 public void CreateSubKey_Key_ReadOnly ()
529 string subKeyName
= Guid
.NewGuid ().ToString ();
531 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software")) {
532 RegistryKey createdKey
= null;
535 createdKey
= softwareKey
.CreateSubKey (subKeyName
);
537 } catch (UnauthorizedAccessException ex
) {
538 // Cannot write to the registry key
539 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
540 Assert
.IsNotNull (ex
.Message
, "#3");
541 Assert
.IsNull (ex
.InnerException
, "#4");
544 if (createdKey
!= null)
551 public void CreateSubKey_Key_Removed ()
553 string subKeyName
= Guid
.NewGuid ().ToString ();
555 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
557 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
558 softwareKey
.DeleteSubKeyTree (subKeyName
);
559 Assert
.IsNull (softwareKey
.OpenSubKey (subKeyName
), "#1");
561 createdKey
.CreateSubKey ("test");
563 } catch (IOException ex
) {
564 // Illegal operation attempted on a registry key that
565 // has been marked for deletion
566 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#3");
567 Assert
.IsNotNull (ex
.Message
, "#4");
568 Assert
.IsNull (ex
.InnerException
, "#5");
573 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
574 if (createdKey
!= null) {
576 softwareKey
.DeleteSubKeyTree (subKeyName
);
585 [Category ("NotWorking")] // MS should not allow this
586 public void CreateSubKey_Name_Empty ()
588 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
589 using (RegistryKey emptyKey
= softwareKey
.CreateSubKey (string.Empty
)) {
590 Assert
.IsNotNull (emptyKey
, "#1");
591 emptyKey
.SetValue ("name1", "value1");
597 public void CreateSubKey_Name_MaxLength ()
599 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
600 string subKeyName
= new string ('a', 254);
603 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
604 Assert
.IsNotNull (createdKey
, "#A1");
605 Assert
.IsNotNull (softwareKey
.OpenSubKey (subKeyName
), "#A2");
608 softwareKey
.DeleteSubKeyTree (subKeyName
);
611 subKeyName
= new string ('a', 255);
614 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
615 Assert
.IsNotNull (createdKey
, "#B1");
616 Assert
.IsNotNull (softwareKey
.OpenSubKey (subKeyName
), "#B2");
619 softwareKey
.DeleteSubKey (subKeyName
);
622 subKeyName
= new string ('a', 256);
625 softwareKey
.CreateSubKey (subKeyName
);
627 } catch (ArgumentException ex
) {
628 // 1.x: Registry subkeys should not be
629 // greater than or equal to 255 characters
631 // 2.x: Registry subkeys should not be
632 // greater than 255 characters
633 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#C2");
634 Assert
.IsNull (ex
.InnerException
, "#C3");
635 Assert
.IsNotNull (ex
.Message
, "#C4");
636 Assert
.IsTrue (ex
.Message
.IndexOf ("255") != -1, "#C5");
637 Assert
.IsNull (ex
.ParamName
, "#C6");
643 public void CreateSubKey_Name_Null ()
645 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
647 softwareKey
.CreateSubKey (null);
649 } catch (ArgumentNullException ex
) {
650 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
651 Assert
.IsNull (ex
.InnerException
, "#3");
652 Assert
.IsNotNull (ex
.Message
, "#4");
653 Assert
.AreEqual ("name", ex
.ParamName
, "#5");
659 public void DeleteSubKey ()
661 string subKeyName
= Guid
.NewGuid ().ToString ();
664 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
665 // check if key was successfully created
666 Assert
.IsNotNull (createdKey
, "#1");
668 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
669 Assert
.IsNotNull (createdKey
, "#2");
670 Registry
.CurrentUser
.DeleteSubKey (subKeyName
);
672 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
673 Assert
.IsNull (createdKey
, "#3");
677 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
678 if (createdKey
!= null) {
680 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
688 public void DeleteSubKey_Key_HasChildKeys ()
690 string subKeyName
= Guid
.NewGuid ().ToString ();
693 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
694 // check if key was successfully created
695 Assert
.IsNotNull (createdKey
, "#1");
696 RegistryKey subKey
= createdKey
.CreateSubKey ("monotemp");
700 Registry
.CurrentUser
.DeleteSubKey (subKeyName
);
702 } catch (InvalidOperationException ex
) {
703 // Registry key has subkeys and recursive removes are not
704 // supported by this method
705 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#3");
706 Assert
.IsNotNull (ex
.Message
, "#4");
707 Assert
.IsNull (ex
.InnerException
, "#5");
711 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
712 if (createdKey
!= null) {
714 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
722 public void DeleteSubKey_Key_ReadOnly ()
724 string subKeyName
= Guid
.NewGuid ().ToString ();
727 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
728 RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
);
732 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software")) {
734 softwareKey
.DeleteSubKey (subKeyName
);
736 } catch (UnauthorizedAccessException ex
) {
737 // Cannot write to the registry key
738 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
739 Assert
.IsNotNull (ex
.Message
, "#3");
740 Assert
.IsNull (ex
.InnerException
, "#4");
745 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
746 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
747 if (createdKey
!= null) {
749 softwareKey
.DeleteSubKeyTree (subKeyName
);
758 public void DeleteSubKey_Key_DoesNotExist ()
760 string subKeyName
= Guid
.NewGuid ().ToString ();
763 Registry
.CurrentUser
.DeleteSubKey (subKeyName
);
765 } catch (ArgumentException ex
) {
766 // Cannot delete a subkey tree because the subkey does not exist
767 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#A2");
768 Assert
.IsNull (ex
.InnerException
, "#A3");
769 Assert
.IsNotNull (ex
.Message
, "#A4");
770 Assert
.IsNull (ex
.ParamName
, "#A5");
774 Registry
.CurrentUser
.DeleteSubKey (subKeyName
, true);
776 } catch (ArgumentException ex
) {
777 // Cannot delete a subkey tree because the subkey does not exist
778 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#B2");
779 Assert
.IsNull (ex
.InnerException
, "#B3");
780 Assert
.IsNotNull (ex
.Message
, "#B4");
781 Assert
.IsNull (ex
.ParamName
, "#B5");
784 Registry
.CurrentUser
.DeleteSubKey (subKeyName
, false);
788 public void DeleteSubKey_Key_Removed ()
790 string subKeyName
= Guid
.NewGuid ().ToString ();
793 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
794 // check if key was successfully created
795 Assert
.IsNotNull (createdKey
, "#1");
796 RegistryKey subKey
= createdKey
.CreateSubKey ("monotemp");
799 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
800 Assert
.IsNotNull (createdKey
, "#2");
801 using (RegistryKey subKey
= createdKey
.OpenSubKey ("monotemp")) {
802 Assert
.IsNotNull (createdKey
, "#3");
804 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
805 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (subKeyName
), "#4");
807 createdKey
.DeleteSubKey ("monotemp");
809 } catch (ArgumentException ex
) {
810 // Cannot delete a subkey tree because the subkey does
812 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#6");
813 Assert
.IsNull (ex
.InnerException
, "#7");
814 Assert
.IsNotNull (ex
.Message
, "#8");
815 Assert
.IsNull (ex
.ParamName
, "#9");
820 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
821 if (createdKey
!= null) {
823 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
831 [Category ("NotWorking")] // MS should not allow this
832 public void DeleteSubKey_Name_Empty ()
834 string subKeyName
= Guid
.NewGuid ().ToString ();
836 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
838 RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
);
839 createdKey
.DeleteSubKey (string.Empty
);
842 createdKey
= softwareKey
.OpenSubKey (subKeyName
);
843 Assert
.IsNull (createdKey
, "#1");
846 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
847 if (createdKey
!= null)
849 softwareKey
.DeleteSubKeyTree (subKeyName
);
857 public void DeleteSubKey_Name_MaxLength ()
859 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
860 string subKeyName
= new string ('a', 254);
862 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
865 using (RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
, false)) {
866 Assert
.IsNotNull (createdKey
, "#A1");
868 softwareKey
.DeleteSubKey (subKeyName
);
869 using (RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
, false)) {
870 Assert
.IsNull (createdKey
, "#A2");
873 subKeyName
= new string ('a', 256);
876 softwareKey
.DeleteSubKey (subKeyName
);
878 } catch (ArgumentException ex
) {
879 // 1.x: Registry subkeys should not be
880 // greater than or equal to 255 characters
882 // 2.x: Registry subkeys should not be
883 // greater than 255 characters
884 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#B2");
885 Assert
.IsNull (ex
.InnerException
, "#B3");
886 Assert
.IsNotNull (ex
.Message
, "#B4");
887 Assert
.IsTrue (ex
.Message
.IndexOf ("255") != -1, "#B5");
888 Assert
.IsNull (ex
.ParamName
, "#B6");
894 public void DeleteSubKey_Name_Null ()
896 string subKeyName
= Guid
.NewGuid ().ToString ();
898 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
900 RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
);
902 createdKey
.DeleteSubKey (null);
904 } catch (ArgumentNullException ex
) {
905 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
906 Assert
.IsNull (ex
.InnerException
, "#3");
907 Assert
.IsNotNull (ex
.Message
, "#4");
908 Assert
.AreEqual ("name", ex
.ParamName
, "#5");
912 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
913 if (createdKey
!= null)
915 softwareKey
.DeleteSubKeyTree (subKeyName
);
923 public void DeleteSubKeyTree ()
926 // - remove key with subkeys
927 // - remove key of which some subkeys are marked for deletion
928 // - remove key with values
932 public void DeleteSubKeyTree_Key_DoesNotExist ()
934 // Cannot delete a subkey tree because the subkey does not exist
935 string subKeyName
= Guid
.NewGuid ().ToString ();
937 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
939 } catch (ArgumentException ex
) {
940 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
941 Assert
.IsNull (ex
.InnerException
, "#3");
942 Assert
.IsNotNull (ex
.Message
, "#4");
943 Assert
.IsNull (ex
.ParamName
, "#5");
948 public void DeleteSubKeyTree_Key_ReadOnly ()
950 string subKeyName
= Guid
.NewGuid ().ToString ();
953 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
954 RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
);
958 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software")) {
960 softwareKey
.DeleteSubKeyTree (subKeyName
);
962 } catch (UnauthorizedAccessException ex
) {
963 // Cannot write to the registry key
964 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
965 Assert
.IsNotNull (ex
.Message
, "#3");
966 Assert
.IsNull (ex
.InnerException
, "#4");
971 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
972 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
973 if (createdKey
!= null)
975 softwareKey
.DeleteSubKeyTree (subKeyName
);
983 public void DeleteSubKeyTree_Key_Removed ()
985 string subKeyName
= Guid
.NewGuid ().ToString ();
988 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
989 // check if key was successfully created
990 Assert
.IsNotNull (createdKey
, "#1");
991 RegistryKey subKey
= createdKey
.CreateSubKey ("monotemp");
994 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
995 Assert
.IsNotNull (createdKey
, "#2");
996 using (RegistryKey subKey
= createdKey
.OpenSubKey ("monotemp")) {
997 Assert
.IsNotNull (createdKey
, "#3");
999 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1000 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (subKeyName
), "#4");
1002 createdKey
.DeleteSubKeyTree ("monotemp");
1004 } catch (ArgumentException ex
) {
1005 // Cannot delete a subkey tree because the subkey does
1007 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#6");
1008 Assert
.IsNull (ex
.InnerException
, "#7");
1009 Assert
.IsNotNull (ex
.Message
, "#8");
1010 Assert
.IsNull (ex
.ParamName
, "#9");
1015 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1016 if (createdKey
!= null) {
1017 createdKey
.Close ();
1018 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1026 [Category ("NotWorking")] // MS should not allow this
1027 public void DeleteSubKeyTree_Name_Empty ()
1029 string subKeyName
= Guid
.NewGuid ().ToString ();
1031 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
1033 RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
);
1034 createdKey
.DeleteSubKeyTree (string.Empty
);
1035 createdKey
.Close ();
1037 createdKey
= softwareKey
.OpenSubKey (subKeyName
);
1038 Assert
.IsNull (createdKey
, "#1");
1041 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
1042 if (createdKey
!= null)
1043 createdKey
.Close ();
1044 softwareKey
.DeleteSubKeyTree (subKeyName
);
1052 public void DeleteSubKeyTree_Name_MaxLength ()
1054 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
1055 string subKeyName
= new string ('a', 254);
1057 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
1058 createdKey
.Close ();
1060 using (RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
, false)) {
1061 Assert
.IsNotNull (createdKey
, "#A1");
1063 softwareKey
.DeleteSubKeyTree (subKeyName
);
1064 using (RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
, false)) {
1065 Assert
.IsNull (createdKey
, "#A2");
1069 subKeyName
= new string ('a', 255);
1071 subKeyName
= new string ('a', 256);
1075 softwareKey
.DeleteSubKeyTree (subKeyName
);
1076 Assert
.Fail ("#B1");
1077 } catch (ArgumentException ex
) {
1078 // 1.x: Registry subkeys should not be
1079 // greater than or equal to 255 characters
1081 // 2.x: Registry subkeys should not be
1082 // greater than 255 characters
1083 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#B2");
1084 Assert
.IsNull (ex
.InnerException
, "#B3");
1085 Assert
.IsNotNull (ex
.Message
, "#B4");
1086 Assert
.IsTrue (ex
.Message
.IndexOf ("255") != -1, "#B5");
1087 Assert
.IsNull (ex
.ParamName
, "#B6");
1093 public void DeleteSubKeyTree_Name_Null ()
1095 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
1097 softwareKey
.DeleteSubKeyTree (null);
1099 } catch (ArgumentNullException ex
) {
1100 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
1101 Assert
.IsNull (ex
.InnerException
, "#3");
1102 Assert
.IsNotNull (ex
.Message
, "#4");
1103 Assert
.AreEqual ("name", ex
.ParamName
, "#5");
1109 public void DeleteValue ()
1111 string subKeyName
= Guid
.NewGuid ().ToString ();
1114 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1115 // check if key was successfully created
1116 Assert
.IsNotNull (createdKey
, "#A1");
1117 createdKey
.SetValue ("name1", "value1");
1118 createdKey
.SetValue ("name2", "value2");
1119 string [] names
= createdKey
.GetValueNames ();
1120 Assert
.IsNotNull (names
, "#A2");
1121 Assert
.AreEqual (2, names
.Length
, "#A3");
1122 Assert
.IsNotNull (names
[0], "#A4");
1123 Assert
.AreEqual ("name1", names
[0], "#A5");
1124 Assert
.IsNotNull (createdKey
.GetValue ("name1"), "#A6");
1125 Assert
.AreEqual ("value1", createdKey
.GetValue ("name1"), "#A7");
1126 Assert
.AreEqual ("name2", names
[1], "#A8");
1127 Assert
.IsNotNull (createdKey
.GetValue ("name2"), "#A9");
1128 Assert
.AreEqual ("value2", createdKey
.GetValue ("name2"), "#A10");
1130 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
1131 Assert
.IsNotNull (createdKey
, "#B1");
1132 createdKey
.DeleteValue ("name1");
1133 string [] names
= createdKey
.GetValueNames ();
1134 Assert
.IsNotNull (names
, "#B2");
1135 Assert
.AreEqual (1, names
.Length
, "#B3");
1136 Assert
.IsNotNull (names
[0], "#B4");
1137 Assert
.AreEqual ("name2", names
[0], "#B5");
1138 Assert
.IsNotNull (createdKey
.GetValue ("name2"), "#B6");
1139 Assert
.AreEqual ("value2", createdKey
.GetValue ("name2"), "#B7");
1140 createdKey
.DeleteValue (new string ('a', 400), false);
1142 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1143 string [] names
= createdKey
.GetValueNames ();
1144 Assert
.IsNotNull (names
, "#C1");
1145 Assert
.AreEqual (1, names
.Length
, "#C2");
1146 Assert
.IsNotNull (names
[0], "#C3");
1147 Assert
.AreEqual ("name2", names
[0], "#C4");
1148 Assert
.IsNotNull (createdKey
.GetValue ("name2"), "#C5");
1149 Assert
.AreEqual ("value2", createdKey
.GetValue ("name2"), "#C6");
1153 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1154 if (createdKey
!= null) {
1155 createdKey
.Close ();
1156 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1164 public void DeleteValue_Key_ReadOnly ()
1166 string subKeyName
= Guid
.NewGuid ().ToString ();
1169 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1170 createdKey
.SetValue ("name1", "value1");
1173 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1175 // deleting value that exists
1176 createdKey
.DeleteValue ("name1");
1177 Assert
.Fail ("#A1");
1178 } catch (UnauthorizedAccessException ex
) {
1179 // Cannot write to the registry key
1180 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#A2");
1181 Assert
.IsNotNull (ex
.Message
, "#A3");
1182 Assert
.IsNull (ex
.InnerException
, "#A4");
1186 // deleting value that exists
1187 createdKey
.DeleteValue ("name1", true);
1188 Assert
.Fail ("#B1");
1189 } catch (UnauthorizedAccessException ex
) {
1190 // Cannot write to the registry key
1191 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#B2");
1192 Assert
.IsNotNull (ex
.Message
, "#B3");
1193 Assert
.IsNull (ex
.InnerException
, "#B4");
1197 // deleting value that exists
1198 createdKey
.DeleteValue ("name1", false);
1199 Assert
.Fail ("#C1");
1200 } catch (UnauthorizedAccessException ex
) {
1201 // Cannot write to the registry key
1202 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#C2");
1203 Assert
.IsNotNull (ex
.Message
, "#C3");
1204 Assert
.IsNull (ex
.InnerException
, "#C4");
1208 // deleting value that does not exist
1209 createdKey
.DeleteValue ("name2");
1210 Assert
.Fail ("#D1");
1211 } catch (UnauthorizedAccessException ex
) {
1212 // Cannot write to the registry key
1213 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#D2");
1214 Assert
.IsNotNull (ex
.Message
, "#D3");
1215 Assert
.IsNull (ex
.InnerException
, "#D4");
1219 // deleting value that does not exist
1220 createdKey
.DeleteValue ("name2", true);
1221 Assert
.Fail ("#E1");
1222 } catch (UnauthorizedAccessException ex
) {
1223 // Cannot write to the registry key
1224 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#E2");
1225 Assert
.IsNotNull (ex
.Message
, "#E3");
1226 Assert
.IsNull (ex
.InnerException
, "#E4");
1230 // deleting value that does not exist
1231 createdKey
.DeleteValue ("name2", false);
1232 Assert
.Fail ("#F1");
1233 } catch (UnauthorizedAccessException ex
) {
1234 // Cannot write to the registry key
1235 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#F2");
1236 Assert
.IsNotNull (ex
.Message
, "#F3");
1237 Assert
.IsNull (ex
.InnerException
, "#F4");
1242 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1243 if (createdKey
!= null) {
1244 createdKey
.Close ();
1245 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1253 public void DeleteValue_Key_Removed ()
1255 string subKeyName
= Guid
.NewGuid ().ToString ();
1258 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1259 // check if key was successfully created
1260 Assert
.IsNotNull (createdKey
, "#1");
1261 createdKey
.SetValue ("name1", "value1");
1263 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
1264 Assert
.IsNotNull (createdKey
, "#2");
1265 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1266 Assert
.IsNull (Registry
.CurrentUser
.OpenSubKey (subKeyName
), "#3");
1268 createdKey
.DeleteValue ("name1");
1269 createdKey
.DeleteValue ("name1", true);
1270 createdKey
.DeleteValue ("name1", false);
1274 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1275 if (createdKey
!= null) {
1276 createdKey
.Close ();
1277 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1285 public void DeleteValue_Value_DoesNotExist ()
1287 string subKeyName
= Guid
.NewGuid ().ToString ();
1290 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1291 // check if key was successfully created
1292 Assert
.IsNotNull (createdKey
, "#A1");
1293 createdKey
.SetValue ("name1", "value1");
1296 createdKey
.DeleteValue ("name2");
1297 Assert
.Fail ("#B1");
1298 } catch (ArgumentException ex
) {
1299 // No value exists with that name
1300 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#B2");
1301 Assert
.IsNull (ex
.InnerException
, "#B3");
1302 Assert
.IsNotNull (ex
.Message
, "#B4");
1303 Assert
.IsNull (ex
.ParamName
, "#B5");
1307 createdKey
.DeleteValue ("name2", true);
1308 Assert
.Fail ("#C1");
1309 } catch (ArgumentException ex
) {
1310 // No value exists with that name
1311 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#C2");
1312 Assert
.IsNull (ex
.InnerException
, "#C3");
1313 Assert
.IsNotNull (ex
.Message
, "#C4");
1314 Assert
.IsNull (ex
.ParamName
, "#C5");
1317 createdKey
.DeleteValue ("name2", false);
1321 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1322 if (createdKey
!= null) {
1323 createdKey
.Close ();
1324 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1332 public void DeleteValue_Name_Empty ()
1334 string subKeyName
= Guid
.NewGuid ().ToString ();
1337 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1338 createdKey
.SetValue ("name1", "value1");
1339 createdKey
.SetValue (string.Empty
, "value2");
1341 string [] names
= createdKey
.GetValueNames ();
1342 Assert
.IsNotNull (names
, "#A1");
1343 Assert
.AreEqual (2, names
.Length
, "#A2");
1344 Assert
.IsNotNull (names
[0], "#A3");
1346 Assert.AreEqual ("name1", names [0], "#A4");
1348 Assert
.IsNotNull (createdKey
.GetValue ("name1"), "#A5");
1349 Assert
.AreEqual ("value1", createdKey
.GetValue ("name1"), "#A6");
1350 Assert
.IsNotNull (names
[1], "#A7");
1352 Assert.AreEqual (string.Empty, names [1], "#A8");
1354 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#A9");
1355 Assert
.AreEqual ("value2", createdKey
.GetValue (string.Empty
), "#A10");
1357 createdKey
.DeleteValue (string.Empty
);
1359 names
= createdKey
.GetValueNames ();
1360 Assert
.IsNotNull (names
, "#B1");
1361 Assert
.AreEqual (1, names
.Length
, "#B2");
1362 Assert
.IsNotNull (names
[0], "#B3");
1363 Assert
.AreEqual ("name1", names
[0], "#B4");
1364 Assert
.IsNotNull (createdKey
.GetValue ("name1"), "#B5");
1365 Assert
.AreEqual ("value1", createdKey
.GetValue ("name1"), "#B6");
1368 createdKey
.DeleteValue (string.Empty
);
1369 Assert
.Fail ("#C1");
1370 } catch (ArgumentException ex
) {
1371 // No value exists with that name
1372 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#C2");
1373 Assert
.IsNull (ex
.InnerException
, "#C3");
1374 Assert
.IsNotNull (ex
.Message
, "#C4");
1375 Assert
.IsNull (ex
.ParamName
, "#C5");
1379 createdKey
.DeleteValue (string.Empty
, true);
1380 Assert
.Fail ("#D1");
1381 } catch (ArgumentException ex
) {
1382 // No value exists with that name
1383 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#D2");
1384 Assert
.IsNull (ex
.InnerException
, "#D3");
1385 Assert
.IsNotNull (ex
.Message
, "#D4");
1386 Assert
.IsNull (ex
.ParamName
, "#D5");
1389 createdKey
.DeleteValue (string.Empty
, false);
1391 names
= createdKey
.GetValueNames ();
1392 Assert
.IsNotNull (names
, "#E1");
1393 Assert
.AreEqual (1, names
.Length
, "#E2");
1394 Assert
.IsNotNull (names
[0], "#E3");
1395 Assert
.AreEqual ("name1", names
[0], "#E4");
1396 Assert
.IsNotNull (createdKey
.GetValue ("name1"), "#E5");
1397 Assert
.AreEqual ("value1", createdKey
.GetValue ("name1"), "#E6");
1401 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1402 if (createdKey
!= null) {
1403 createdKey
.Close ();
1404 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1412 public void DeleteValue_Name_Null ()
1414 string subKeyName
= Guid
.NewGuid ().ToString ();
1417 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1418 createdKey
.SetValue ("name1", "value1");
1419 createdKey
.SetValue (null, "value2");
1421 string [] names
= createdKey
.GetValueNames ();
1422 Assert
.IsNotNull (names
, "#A1");
1423 Assert
.AreEqual (2, names
.Length
, "#A2");
1424 Assert
.IsNotNull (names
[0], "#A3");
1426 Assert.AreEqual ("name1", names [0], "#A4");
1428 Assert
.IsNotNull (createdKey
.GetValue ("name1"), "#A5");
1429 Assert
.AreEqual ("value1", createdKey
.GetValue ("name1"), "#A6");
1430 Assert
.IsNotNull (names
[1], "#A7");
1432 Assert.AreEqual (string.Empty, names [1], "#A8");
1434 Assert
.IsNotNull (createdKey
.GetValue (null), "#A9");
1435 Assert
.AreEqual ("value2", createdKey
.GetValue (null), "#A10");
1438 createdKey
.DeleteValue (null);
1439 Assert
.Fail ("#B1");
1440 } catch (ArgumentNullException ex
) {
1441 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#B2");
1442 Assert
.IsNull (ex
.InnerException
, "#B3");
1443 Assert
.IsNotNull (ex
.Message
, "#B4");
1444 Assert
.AreEqual ("name", ex
.ParamName
, "#B5");
1448 createdKey
.DeleteValue (null, true);
1449 Assert
.Fail ("#C1");
1450 } catch (ArgumentNullException ex
) {
1451 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#C2");
1452 Assert
.IsNull (ex
.InnerException
, "#C3");
1453 Assert
.IsNotNull (ex
.Message
, "#C4");
1454 Assert
.AreEqual ("name", ex
.ParamName
, "#C5");
1458 createdKey
.DeleteValue (null, false);
1459 Assert
.Fail ("#D1");
1460 } catch (ArgumentNullException ex
) {
1461 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#D2");
1462 Assert
.IsNull (ex
.InnerException
, "#D3");
1463 Assert
.IsNotNull (ex
.Message
, "#D4");
1464 Assert
.AreEqual ("name", ex
.ParamName
, "#D5");
1469 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1470 if (createdKey
!= null) {
1471 createdKey
.Close ();
1472 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1480 public void GetValue ()
1482 string subKeyName
= Guid
.NewGuid ().ToString ();
1485 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1486 createdKey
.SetValue ("name1", "value1");
1487 createdKey
.SetValue ("name2", "value2");
1490 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1491 Assert
.IsNotNull (createdKey
.GetValue ("name1"), "#1");
1492 Assert
.AreEqual ("value1", createdKey
.GetValue ("name1"), "#2");
1493 Assert
.IsNotNull (createdKey
.GetValue ("name2"), "#3");
1494 Assert
.AreEqual ("value2", createdKey
.GetValue ("name2"), "#4");
1495 Assert
.IsNull (createdKey
.GetValue ("name3"), "#5");
1496 Assert
.AreEqual ("value3", createdKey
.GetValue ("name3", "value3"), "#6");
1497 Assert
.IsNull (createdKey
.GetValue ("name3", null), "#7");
1498 Assert
.IsNull (createdKey
.GetValue (new string ('a', 400)), "#8");
1502 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1503 if (createdKey
!= null) {
1504 createdKey
.Close ();
1505 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1513 public void GetValue_Key_Removed ()
1515 string subKeyName
= Guid
.NewGuid ().ToString ();
1518 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1519 createdKey
.SetValue ("name1", "value1");
1520 createdKey
.SetValue ("name2", "value2");
1523 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1524 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1526 Assert
.IsNull (createdKey
.GetValue ("name1"), "#1");
1527 Assert
.IsNotNull (createdKey
.GetValue ("name1", "default"), "#2");
1528 Assert
.AreEqual ("default", createdKey
.GetValue ("name1", "default"), "#3");
1529 Assert
.IsNull (createdKey
.GetValue ("name3"), "#3");
1530 Assert
.IsNotNull (createdKey
.GetValue ("name3", "default"), "#4");
1531 Assert
.AreEqual ("default", createdKey
.GetValue ("name3", "default"), "#5");
1535 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1536 if (createdKey
!= null) {
1537 createdKey
.Close ();
1538 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1546 public void GetValue_Name_Empty ()
1548 string subKeyName
= Guid
.NewGuid ().ToString ();
1551 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1552 createdKey
.SetValue ("name1", "value1");
1553 createdKey
.SetValue ("name2", "value2");
1555 Assert
.IsNull (createdKey
.GetValue (string.Empty
), "#A1");
1556 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
, "default"), "#A2");
1557 Assert
.AreEqual ("default", createdKey
.GetValue (string.Empty
, "default"), "#A3");
1558 Assert
.IsNull (createdKey
.GetValue (string.Empty
, null), "#A4");
1561 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1562 Assert
.IsNull (createdKey
.GetValue (string.Empty
), "#B1");
1563 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
, "default"), "#B2");
1564 Assert
.AreEqual ("default", createdKey
.GetValue (string.Empty
, "default"), "#B3");
1565 Assert
.IsNull (createdKey
.GetValue (string.Empty
, null), "#B4");
1568 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
1569 createdKey
.SetValue (string.Empty
, "value1");
1570 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#C1");
1571 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
), "#C2");
1572 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
, "default"), "#C3");
1573 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
, null), "#C4");
1576 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1577 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#D1");
1578 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
), "#D2");
1579 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
, "default"), "#D3");
1580 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
, null), "#D4");
1584 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1585 if (createdKey
!= null) {
1586 createdKey
.Close ();
1587 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1595 public void GetValue_Name_Null ()
1597 string subKeyName
= Guid
.NewGuid ().ToString ();
1600 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1601 createdKey
.SetValue ("name1", "value1");
1602 createdKey
.SetValue ("name2", "value2");
1604 Assert
.IsNull (createdKey
.GetValue (null), "#A1");
1605 Assert
.IsNotNull (createdKey
.GetValue (null, "default"), "#A2");
1606 Assert
.AreEqual ("default", createdKey
.GetValue (null, "default"), "#A3");
1607 Assert
.IsNull (createdKey
.GetValue (null, null), "#A4");
1610 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1611 Assert
.IsNull (createdKey
.GetValue (null), "#B1");
1612 Assert
.IsNotNull (createdKey
.GetValue (null, "default"), "#B2");
1613 Assert
.AreEqual ("default", createdKey
.GetValue (null, "default"), "#B3");
1614 Assert
.IsNull (createdKey
.GetValue (null, null), "#B4");
1617 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
1618 createdKey
.SetValue (string.Empty
, "value1");
1619 Assert
.IsNotNull (createdKey
.GetValue (null), "#C1");
1620 Assert
.AreEqual ("value1", createdKey
.GetValue (null), "#C2");
1621 Assert
.AreEqual ("value1", createdKey
.GetValue (null, "default"), "#C3");
1622 Assert
.AreEqual ("value1", createdKey
.GetValue (null, null), "#C4");
1625 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1626 Assert
.IsNotNull (createdKey
.GetValue (null), "#D1");
1627 Assert
.AreEqual ("value1", createdKey
.GetValue (null), "#D2");
1628 Assert
.AreEqual ("value1", createdKey
.GetValue (null, "default"), "#D3");
1629 Assert
.AreEqual ("value1", createdKey
.GetValue (null, null), "#D4");
1633 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1634 if (createdKey
!= null) {
1635 createdKey
.Close ();
1636 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1644 public void GetValue_Expand ()
1646 string subKeyName
= Guid
.NewGuid ().ToString ();
1649 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1650 Environment
.SetEnvironmentVariable ("MONO_TEST1", "123");
1651 Environment
.SetEnvironmentVariable ("MONO_TEST2", "456");
1653 createdKey
.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1654 RegistryValueKind
.ExpandString
);
1655 createdKey
.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1656 createdKey
.SetValue ("name3", "just some text",
1657 RegistryValueKind
.ExpandString
);
1659 Assert
.AreEqual ("123/456", createdKey
.GetValue ("name1"), "#A1");
1660 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2"), "#A2");
1661 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3"), "#A3");
1662 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name1",
1663 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#A4");
1664 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2",
1665 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#A5");
1666 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3",
1667 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#A6");
1668 Assert
.AreEqual ("123/456", createdKey
.GetValue ("name1",
1669 null, RegistryValueOptions
.None
), "#A7");
1670 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2",
1671 null, RegistryValueOptions
.None
), "#A8");
1672 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3",
1673 null, RegistryValueOptions
.None
), "#A9");
1675 Environment
.SetEnvironmentVariable ("MONO_TEST1", "789");
1676 Environment
.SetEnvironmentVariable ("MONO_TEST2", "666");
1678 Assert
.AreEqual ("789/666", createdKey
.GetValue ("name1"), "#B1");
1679 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2"), "#B2");
1680 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3"), "#B3");
1681 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name1",
1682 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#B4");
1683 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2",
1684 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#B5");
1685 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3",
1686 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#B6");
1687 Assert
.AreEqual ("789/666", createdKey
.GetValue ("name1",
1688 null, RegistryValueOptions
.None
), "#B7");
1689 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2",
1690 null, RegistryValueOptions
.None
), "#B8");
1691 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3",
1692 null, RegistryValueOptions
.None
), "#B9");
1694 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1695 Assert
.AreEqual ("789/666", createdKey
.GetValue ("name1"), "#C1");
1696 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2"), "#C2");
1697 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3"), "#C3");
1698 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name1",
1699 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#C4");
1700 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2",
1701 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#C5");
1702 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3",
1703 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#C6");
1704 Assert
.AreEqual ("789/666", createdKey
.GetValue ("name1",
1705 null, RegistryValueOptions
.None
), "#C7");
1706 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2",
1707 null, RegistryValueOptions
.None
), "#C8");
1708 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3",
1709 null, RegistryValueOptions
.None
), "#C9");
1711 Environment
.SetEnvironmentVariable ("MONO_TEST1", "123");
1712 Environment
.SetEnvironmentVariable ("MONO_TEST2", "456");
1714 Assert
.AreEqual ("123/456", createdKey
.GetValue ("name1"), "#D1");
1715 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2"), "#D2");
1716 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3"), "#D3");
1717 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name1",
1718 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#D4");
1719 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2",
1720 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#D5");
1721 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3",
1722 null, RegistryValueOptions
.DoNotExpandEnvironmentNames
), "#D6");
1723 Assert
.AreEqual ("123/456", createdKey
.GetValue ("name1",
1724 null, RegistryValueOptions
.None
), "#D7");
1725 Assert
.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey
.GetValue ("name2",
1726 null, RegistryValueOptions
.None
), "#D8");
1727 Assert
.AreEqual ("just some text", createdKey
.GetValue ("name3",
1728 null, RegistryValueOptions
.None
), "#D9");
1732 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1733 if (createdKey
!= null) {
1734 createdKey
.Close ();
1735 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1743 public void GetValueNames ()
1745 string subKeyName
= Guid
.NewGuid ().ToString ();
1748 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1749 string [] names
= createdKey
.GetValueNames ();
1750 Assert
.IsNotNull (names
, "#A1");
1751 Assert
.AreEqual (0, names
.Length
, "#A2");
1753 createdKey
.SetValue ("name1", "value1");
1754 createdKey
.SetValue ("name2", "value2");
1755 createdKey
.SetValue ("namelong", "value3");
1756 createdKey
.SetValue ("name3", "value4");
1758 Assert
.AreEqual (4, createdKey
.ValueCount
, "#B1");
1759 names
= createdKey
.GetValueNames ();
1760 Assert
.IsNotNull (names
, "#B2");
1761 Assert
.AreEqual (4, names
.Length
, "#B3");
1764 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
1765 string [] names
= createdKey
.GetValueNames ();
1766 Assert
.IsNotNull (names
, "#C1");
1767 Assert
.AreEqual (4, names
.Length
, "#C2");
1769 // Mono's Unix registry API uses a hashtable to store the
1770 // values (and their names), so names are not returned in
1773 // to test whether the names returned by GetValueNames
1774 // match what we expect, we use these names to remove the
1775 // the values from the created keys and such we should end
1776 // up with zero values
1777 for (int i
= 0; i
< names
.Length
; i
++) {
1778 string valueName
= names
[i
];
1779 createdKey
.DeleteValue (valueName
);
1782 // all values should be removed now
1783 Assert
.AreEqual (0, createdKey
.ValueCount
, "#C3");
1787 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1788 if (createdKey
!= null) {
1789 createdKey
.Close ();
1790 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1798 public void GetValueNames_Key_Removed ()
1800 string subKeyName
= Guid
.NewGuid ().ToString ();
1803 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1804 createdKey
.SetValue ("name1", "value1");
1805 createdKey
.SetValue ("name2", "value2");
1807 string [] names
= createdKey
.GetValueNames ();
1808 Assert
.IsNotNull (names
, "#A1");
1809 Assert
.AreEqual (2, names
.Length
, "#A2");
1812 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1813 string [] names
= createdKey
.GetValueNames ();
1814 Assert
.IsNotNull (names
, "#B1");
1815 Assert
.AreEqual (2, names
.Length
, "#B2");
1817 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1820 createdKey
.GetValueNames ();
1821 Assert
.Fail ("#C1");
1822 } catch (IOException ex
) {
1823 // Illegal operation attempted on a registry key that
1824 // has been marked for deletion
1825 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#C2");
1826 Assert
.IsNotNull (ex
.Message
, "#C3");
1827 Assert
.IsNull (ex
.InnerException
, "#C4");
1832 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
1833 if (createdKey
!= null) {
1834 createdKey
.Close ();
1835 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1842 [Test
] // bug #78519
1843 public void GetSubKeyNamesTest ()
1845 string subKeyName
= Guid
.NewGuid ().ToString ();
1847 RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
);
1849 // check if key was successfully created
1850 Assert
.IsNotNull (createdKey
, "#A");
1852 RegistryKey subKey
= createdKey
.CreateSubKey ("foo");
1853 Assert
.IsNotNull (subKey
, "#B1");
1854 Assert
.AreEqual (1, createdKey
.SubKeyCount
, "#B2");
1855 string[] subKeyNames
= createdKey
.GetSubKeyNames ();
1856 Assert
.IsNotNull (subKeyNames
, "#B3");
1857 Assert
.AreEqual (1, subKeyNames
.Length
, "#B4");
1858 Assert
.AreEqual ("foo", subKeyNames
[0], "#B5");
1860 subKey
= createdKey
.CreateSubKey ("longfoo");
1861 Assert
.IsNotNull (subKey
, "#C1");
1862 Assert
.AreEqual (2, createdKey
.SubKeyCount
, "#C2");
1863 subKeyNames
= createdKey
.GetSubKeyNames ();
1864 Assert
.IsNotNull (subKeyNames
, "#C3");
1865 Assert
.AreEqual (2, subKeyNames
.Length
, "#C4");
1866 Assert
.AreEqual ("foo", subKeyNames
[0], "#C5");
1867 Assert
.AreEqual ("longfoo", subKeyNames
[1], "#C6");
1869 subKey
= createdKey
.CreateSubKey ("sfoo");
1870 Assert
.IsNotNull (subKey
, "#D1");
1871 Assert
.AreEqual (3, createdKey
.SubKeyCount
, "#D2");
1872 subKeyNames
= createdKey
.GetSubKeyNames ();
1873 Assert
.IsNotNull (subKeyNames
, "#D3");
1874 Assert
.AreEqual (3, subKeyNames
.Length
, "#D4");
1875 Assert
.AreEqual ("foo", subKeyNames
[0], "#D5");
1876 Assert
.AreEqual ("longfoo", subKeyNames
[1], "#D6");
1877 Assert
.AreEqual ("sfoo", subKeyNames
[2], "#D7");
1879 foreach (string name
in subKeyNames
) {
1880 createdKey
.DeleteSubKeyTree (name
);
1882 Assert
.AreEqual (0, createdKey
.SubKeyCount
, "#E");
1885 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1890 public void OpenRemoteBaseKey ()
1892 // access to registry of remote machines is not implemented on unix
1896 RegistryKey hive
= RegistryKey
.OpenRemoteBaseKey (
1897 RegistryHive
.CurrentUser
, Environment
.MachineName
);
1898 Assert
.IsNotNull (hive
, "#1");
1900 RegistryKey key
= hive
.OpenSubKey ("SOFTWARE");
1901 Assert
.IsNotNull (key
, "#2");
1908 public void OpenRemoteBaseKey_MachineName_Null ()
1911 RegistryKey
.OpenRemoteBaseKey (RegistryHive
.CurrentUser
, null);
1913 } catch (ArgumentNullException ex
) {
1914 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
1915 Assert
.IsNull (ex
.InnerException
, "#3");
1916 Assert
.IsNotNull (ex
.Message
, "#4");
1917 Assert
.AreEqual ("machineName", ex
.ParamName
, "#5");
1922 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
1924 // access to registry of remote machines is not implemented on unix
1929 RegistryKey
.OpenRemoteBaseKey (RegistryHive
.CurrentUser
,
1932 } catch (IOException ex
) {
1933 // The network path was not found
1934 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#2");
1935 Assert
.IsNotNull (ex
.Message
, "#3");
1936 Assert
.IsNull (ex
.InnerException
, "#4");
1940 [Test
] // bug #322839
1941 public void SetValue1_EntityReferences ()
1943 string subKeyName
= Guid
.NewGuid ().ToString ();
1946 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
1947 // we created a new subkey, so value should not exist
1948 Assert
.IsNull (createdKey
.GetValue ("FirstName&\"<LastName>\""), "#A1");
1950 createdKey
.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
1952 object name
= createdKey
.GetValue ("FirstName&\"<LastName>\"");
1953 // value should exist
1954 Assert
.IsNotNull (name
, "#A2");
1955 // type of value should be string
1956 Assert
.AreEqual (typeof (string), name
.GetType (), "#A3");
1957 // ensure value matches
1958 Assert
.AreEqual ("<'Miguel' & \"de Icaza\">!", name
, "#A4");
1960 // we created a new subkey, so value should not exist
1961 Assert
.IsNull (createdKey
.GetValue ("Info"), "#B1");
1963 createdKey
.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" }
);
1965 object info
= createdKey
.GetValue ("Info");
1966 // value should exist
1967 Assert
.IsNotNull (info
, "#B2");
1968 // type of value should be string
1969 Assert
.AreEqual (typeof (string []), info
.GetType (), "#B3");
1970 // ensure value matches
1971 Assert
.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }
, info
, "#B4");
1974 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
1975 object name
= openedKey
.GetValue ("FirstName&\"<LastName>\"");
1976 Assert
.IsNotNull (name
, "#C1");
1977 Assert
.AreEqual (typeof (string), name
.GetType (), "#C2");
1978 Assert
.AreEqual ("<'Miguel' & \"de Icaza\">!", name
, "#C3");
1980 object info
= openedKey
.GetValue ("Info");
1981 Assert
.IsNotNull (info
, "#D1");
1982 Assert
.AreEqual (typeof (string []), info
.GetType (), "#D2");
1983 Assert
.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }
, info
, "#D3");
1987 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
1991 [Test
] // SetValue (String, Object)
1992 public void SetValue1_Name_Null ()
1994 string subKeyName
= Guid
.NewGuid ().ToString ();
1996 RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
);
1998 createdKey
.SetValue (null, "value1");
1999 string [] names
= createdKey
.GetValueNames ();
2000 Assert
.IsNotNull (names
, "#A1");
2001 Assert
.AreEqual (1, names
.Length
, "#A2");
2002 Assert
.IsNotNull (names
[0], "#A3");
2003 Assert
.AreEqual (string.Empty
, names
[0], "#A4");
2004 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#A5");
2005 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
), "#A6");
2006 Assert
.IsNotNull (createdKey
.GetValue (null), "#A7");
2007 Assert
.AreEqual ("value1", createdKey
.GetValue (null), "#A8");
2009 createdKey
.SetValue (string.Empty
, "value2");
2010 names
= createdKey
.GetValueNames ();
2011 Assert
.IsNotNull (names
, "#B1");
2012 Assert
.AreEqual (1, names
.Length
, "#B2");
2013 Assert
.IsNotNull (names
[0], "#B3");
2014 Assert
.AreEqual (string.Empty
, names
[0], "#B4");
2015 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#B5");
2016 Assert
.AreEqual ("value2", createdKey
.GetValue (string.Empty
), "#B6");
2017 Assert
.IsNotNull (createdKey
.GetValue (null), "#B7");
2018 Assert
.AreEqual ("value2", createdKey
.GetValue (null), "#B8");
2021 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2025 [Test
] // SetValue (String, Object)
2026 public void SetValue1_Name_Empty ()
2028 string subKeyName
= Guid
.NewGuid ().ToString ();
2030 RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
);
2032 createdKey
.SetValue (string.Empty
, "value1");
2033 string [] names
= createdKey
.GetValueNames ();
2034 Assert
.IsNotNull (names
, "#A1");
2035 Assert
.AreEqual (1, names
.Length
, "#A2");
2036 Assert
.IsNotNull (names
[0], "#A3");
2037 Assert
.AreEqual (string.Empty
, names
[0], "#A4");
2038 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#A5");
2039 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
), "#A6");
2040 Assert
.IsNotNull (createdKey
.GetValue (null), "#A7");
2041 Assert
.AreEqual ("value1", createdKey
.GetValue (null), "#A8");
2043 createdKey
.SetValue (null, "value2");
2044 names
= createdKey
.GetValueNames ();
2045 Assert
.IsNotNull (names
, "#B1");
2046 Assert
.AreEqual (1, names
.Length
, "#B2");
2047 Assert
.IsNotNull (names
[0], "#B3");
2048 Assert
.AreEqual (string.Empty
, names
[0], "#B4");
2049 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#B5");
2050 Assert
.AreEqual ("value2", createdKey
.GetValue (string.Empty
), "#B6");
2051 Assert
.IsNotNull (createdKey
.GetValue (null), "#B7");
2052 Assert
.AreEqual ("value2", createdKey
.GetValue (null), "#B8");
2055 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2059 [Test
] // SetValue (String, Object)
2060 public void SetValue1_Name_MaxLength ()
2062 string subKeyName
= Guid
.NewGuid ().ToString ();
2065 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2066 string name
= new string ('a', 254);
2068 createdKey
.SetValue (name
, "value1");
2069 Assert
.IsNotNull (createdKey
.GetValue (name
), "#A1");
2070 createdKey
.DeleteValue (name
);
2071 Assert
.IsNull (createdKey
.GetValue (name
), "#A2");
2073 name
= new string ('a', 255);
2075 createdKey
.SetValue (name
, "value2");
2076 Assert
.IsNotNull (createdKey
.GetValue (name
), "#B1");
2077 createdKey
.DeleteValue (name
);
2078 Assert
.IsNull (createdKey
.GetValue (name
), "#B2");
2080 name
= new string ('a', 256);
2083 createdKey
.SetValue (name
, "value2");
2084 Assert
.Fail ("#C1");
2085 } catch (ArgumentException ex
) {
2086 // 1.x: Registry subkeys should not be
2087 // greater than or equal to 255 characters
2089 // 2.x: Registry subkeys should not be
2090 // greater than 255 characters
2091 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#C2");
2092 Assert
.IsNull (ex
.InnerException
, "#C3");
2093 Assert
.IsNotNull (ex
.Message
, "#C4");
2094 Assert
.IsTrue (ex
.Message
.IndexOf ("255") != -1, "#C5");
2095 Assert
.IsNull (ex
.ParamName
, "#C6");
2100 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
2101 if (createdKey
!= null) {
2102 createdKey
.Close ();
2103 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2110 [Test
] // SetValue (String, Object)
2111 public void SetValue1_Value_Null ()
2113 string subKeyName
= Guid
.NewGuid ().ToString ();
2115 RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
);
2118 createdKey
.SetValue ("Name", null);
2120 } catch (ArgumentNullException ex
) {
2121 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
2122 Assert
.IsNull (ex
.InnerException
, "#3");
2123 Assert
.IsNotNull (ex
.Message
, "#4");
2124 Assert
.AreEqual ("value", ex
.ParamName
, "#5");
2128 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2132 [Test
] // SetValue (String, Object)
2133 public void SetValue1_Boolean ()
2135 string subKeyName
= Guid
.NewGuid ().ToString ();
2138 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2139 // we created a new subkey, so value should not exist
2140 Assert
.IsNull (createdKey
.GetValue ("Installed"), "#A1");
2142 createdKey
.SetValue ("Installed", true);
2144 object value = createdKey
.GetValue ("Installed");
2145 // value should exist
2146 Assert
.IsNotNull (value, "#A2");
2147 // type of value should be string
2148 Assert
.AreEqual (typeof (string), value.GetType (), "#A3");
2149 // ensure value matches
2150 Assert
.AreEqual (true.ToString (), value, "#A4");
2153 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2154 object value = openedKey
.GetValue ("Installed");
2155 Assert
.IsNotNull (value, "#B1");
2156 Assert
.AreEqual (typeof (string), value.GetType (), "#B2");
2157 Assert
.AreEqual (true.ToString (), value, "#B3");
2161 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2165 [Test
] // SetValue (String, Object)
2166 public void SetValue1_Byte ()
2168 string subKeyName
= Guid
.NewGuid ().ToString ();
2171 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2172 // we created a new subkey, so value should not exist
2173 Assert
.IsNull (createdKey
.GetValue ("Flags"), "#A1");
2175 createdKey
.SetValue ("Flags", (byte) 5);
2177 object value = createdKey
.GetValue ("Flags");
2178 // value should exist
2179 Assert
.IsNotNull (value, "#A2");
2180 // type of value should be string
2181 Assert
.AreEqual (typeof (string), value.GetType (), "#A3");
2182 // ensure value matches
2183 Assert
.AreEqual ("5", value, "#A4");
2186 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2187 object value = openedKey
.GetValue ("Flags");
2188 Assert
.IsNotNull (value, "#B1");
2189 Assert
.AreEqual (typeof (string), value.GetType (), "#B2");
2190 Assert
.AreEqual ("5", value, "#B3");
2194 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2198 [Test
] // SetValue (String, Object)
2199 public void SetValue1_ByteArray ()
2201 string subKeyName
= Guid
.NewGuid ().ToString ();
2204 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2205 // we created a new subkey, so value should not exist
2206 Assert
.IsNull (createdKey
.GetValue ("Flags"), "#A1");
2208 createdKey
.SetValue ("Flags", new byte [] { 1, 5 }
);
2210 object value = createdKey
.GetValue ("Flags");
2211 // value should exist
2212 Assert
.IsNotNull (value, "#A2");
2213 // type of value should be string
2214 Assert
.AreEqual (typeof (byte []), value.GetType (), "#3");
2215 // ensure value matches
2216 Assert
.AreEqual (new byte [] { 1, 5 }
, value, "#4");
2219 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2220 object value = openedKey
.GetValue ("Flags");
2221 Assert
.IsNotNull (value, "#B1");
2222 Assert
.AreEqual (typeof (byte []), value.GetType (), "#B2");
2223 Assert
.AreEqual (new byte [] { 1, 5 }
, value, "#B3");
2227 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2231 [Test
] // SetValue (String, Object)
2232 public void SetValue1_DateTime ()
2234 string subKeyName
= Guid
.NewGuid ().ToString ();
2237 object rawValue
= DateTime
.Now
;
2239 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2240 // we created a new subkey, so value should not exist
2241 Assert
.IsNull (createdKey
.GetValue ("Path"), "#A1");
2243 createdKey
.SetValue ("Path", rawValue
);
2245 object value = createdKey
.GetValue ("Path");
2246 // value should exist
2247 Assert
.IsNotNull (value, "#A2");
2248 // type of value should be string
2249 Assert
.AreEqual (typeof (string), value.GetType (), "#A3");
2250 // ensure value matches
2251 Assert
.AreEqual (rawValue
.ToString (), value, "#A4");
2254 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2255 object value = openedKey
.GetValue ("Path");
2256 Assert
.IsNotNull (value, "#B1");
2257 Assert
.AreEqual (typeof (string), value.GetType (), "#B2");
2258 Assert
.AreEqual (rawValue
.ToString (), value, "#B3");
2262 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2267 public void SetValue_Int32 ()
2269 string subKeyName
= Guid
.NewGuid ().ToString ();
2272 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2273 // we created a new subkey, so value should not exist
2274 Assert
.IsNull (createdKey
.GetValue ("RefCount"), "#A1");
2276 createdKey
.SetValue ("RefCount", 5);
2278 object value = createdKey
.GetValue ("RefCount");
2279 // value should exist
2280 Assert
.IsNotNull (value, "#A2");
2281 // type of value should be int
2282 Assert
.AreEqual (typeof (int), value.GetType (), "#A3");
2283 // ensure value matches
2284 Assert
.AreEqual (5, value, "#A4");
2287 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2288 object value = openedKey
.GetValue ("RefCount");
2289 Assert
.IsNotNull (value, "#B1");
2290 Assert
.AreEqual (typeof (int), value.GetType (), "#B2");
2291 Assert
.AreEqual (5, value, "#B3");
2295 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2299 [Test
] // SetValue (String, Object)
2300 public void SetValue1_Int64 ()
2302 string subKeyName
= Guid
.NewGuid ().ToString ();
2305 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2306 // we created a new subkey, so value should not exist
2307 Assert
.IsNull (createdKey
.GetValue ("Ticks"), "#A1");
2309 createdKey
.SetValue ("Ticks", 500L);
2311 object value = createdKey
.GetValue ("Ticks");
2312 // value should exist
2313 Assert
.IsNotNull (value, "#A2");
2314 // type of value should be string
2315 Assert
.AreEqual (typeof (string), value.GetType (), "#A3");
2316 // ensure value matches
2317 Assert
.AreEqual ("500", value, "#A4");
2320 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2321 object value = openedKey
.GetValue ("Ticks");
2322 Assert
.IsNotNull (value, "#B1");
2323 Assert
.AreEqual (typeof (string), value.GetType (), "#B2");
2324 Assert
.AreEqual ("500", value, "#B3");
2328 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2332 [Test
] // SetValue (String, Object)
2333 public void SetValue1_String ()
2335 string subKeyName
= Guid
.NewGuid ().ToString ();
2338 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2339 // we created a new subkey, so value should not exist
2340 Assert
.IsNull (createdKey
.GetValue ("Path"), "#A1");
2342 createdKey
.SetValue ("Path", "/usr/lib/whatever");
2344 object path
= createdKey
.GetValue ("Path");
2345 // value should exist
2346 Assert
.IsNotNull (path
, "#A2");
2347 // type of value should be string
2348 Assert
.AreEqual (typeof (string), path
.GetType (), "#A3");
2349 // ensure value matches
2350 Assert
.AreEqual ("/usr/lib/whatever", path
, "#A4");
2353 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2354 object path
= openedKey
.GetValue ("Path");
2355 Assert
.IsNotNull (path
, "#B1");
2356 Assert
.AreEqual (typeof (string), path
.GetType (), "#B2");
2357 Assert
.AreEqual ("/usr/lib/whatever", path
, "#B3");
2361 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2365 [Test
] // SetValue (String, Object)
2366 public void SetValue1_StringArray ()
2368 string subKeyName
= Guid
.NewGuid ().ToString ();
2371 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2372 // we created a new subkey, so value should not exist
2373 Assert
.IsNull (createdKey
.GetValue ("DependsOnGroup"), "#A1");
2375 createdKey
.SetValue ("DependsOnGroup", new string [] { "A", "B" }
);
2377 object value = createdKey
.GetValue ("DependsOnGroup");
2378 // value should exist
2379 Assert
.IsNotNull (value, "#A2");
2380 // type of value should be string
2381 Assert
.AreEqual (typeof (string []), value.GetType (), "#A3");
2382 // ensure value matches
2383 Assert
.AreEqual (new string [] { "A", "B" }
, value, "#A4");
2386 using (RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2387 object value = openedKey
.GetValue ("DependsOnGroup");
2388 Assert
.IsNotNull (value, "#B1");
2389 Assert
.AreEqual (typeof (string []), value.GetType (), "#B2");
2390 Assert
.AreEqual (new string [] { "A", "B" }
, value, "#B3");
2394 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2398 [Test
] // SetValue (String, Object)
2399 public void SetValue1_Key_ReadOnly ()
2401 string subKeyName
= Guid
.NewGuid ().ToString ();
2403 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software")) {
2405 softwareKey
.SetValue ("name1", "value1");
2407 } catch (UnauthorizedAccessException ex
) {
2408 // Cannot write to the registry key
2409 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
2410 Assert
.IsNotNull (ex
.Message
, "#3");
2411 Assert
.IsNull (ex
.InnerException
, "#4");
2415 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
2417 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
2420 using (RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
)) {
2422 createdKey
.SetValue ("name1", "value1");
2424 } catch (UnauthorizedAccessException ex
) {
2425 // Cannot write to the registry key
2426 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
2427 Assert
.IsNotNull (ex
.Message
, "#3");
2428 Assert
.IsNull (ex
.InnerException
, "#4");
2433 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
2434 if (createdKey
!= null) {
2435 createdKey
.Close ();
2436 softwareKey
.DeleteSubKeyTree (subKeyName
);
2444 [Test
] // SetValue (String, Object)
2445 public void SetValue1_Key_Removed ()
2447 string subKeyName
= Guid
.NewGuid ().ToString ();
2449 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
2451 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
2452 softwareKey
.DeleteSubKeyTree (subKeyName
);
2453 Assert
.IsNull (softwareKey
.OpenSubKey (subKeyName
), "#1");
2455 createdKey
.SetValue ("name1", "value1");
2457 } catch (IOException ex
) {
2458 // Illegal operation attempted on a registry key that
2459 // has been marked for deletion
2460 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#3");
2461 Assert
.IsNotNull (ex
.Message
, "#4");
2462 Assert
.IsNull (ex
.InnerException
, "#5");
2467 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
2468 if (createdKey
!= null) {
2469 createdKey
.Close ();
2470 softwareKey
.DeleteSubKeyTree (subKeyName
);
2478 [Test
] // SetValue (String, Object, RegistryValueKind)
2479 public void SetValue2_Key_ReadOnly ()
2481 string subKeyName
= Guid
.NewGuid ().ToString ();
2483 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software")) {
2485 softwareKey
.SetValue ("name1", "value1",
2486 RegistryValueKind
.String
);
2488 } catch (UnauthorizedAccessException ex
) {
2489 // Cannot write to the registry key
2490 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
2491 Assert
.IsNotNull (ex
.Message
, "#3");
2492 Assert
.IsNull (ex
.InnerException
, "#4");
2496 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
2498 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
2501 using (RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
)) {
2503 createdKey
.SetValue ("name1", "value1",
2504 RegistryValueKind
.String
);
2506 } catch (UnauthorizedAccessException ex
) {
2507 // Cannot write to the registry key
2508 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
2509 Assert
.IsNotNull (ex
.Message
, "#3");
2510 Assert
.IsNull (ex
.InnerException
, "#4");
2515 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
2516 if (createdKey
!= null) {
2517 createdKey
.Close ();
2518 softwareKey
.DeleteSubKeyTree (subKeyName
);
2526 [Test
] // SetValue (String, Object, RegistryValueKind)
2527 public void SetValue2_Key_Removed ()
2529 string subKeyName
= Guid
.NewGuid ().ToString ();
2531 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
2533 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
2534 softwareKey
.DeleteSubKeyTree (subKeyName
);
2535 Assert
.IsNull (softwareKey
.OpenSubKey (subKeyName
), "#1");
2537 createdKey
.SetValue ("name1", "value1",
2538 RegistryValueKind
.String
);
2540 } catch (IOException ex
) {
2541 // Illegal operation attempted on a registry key that
2542 // has been marked for deletion
2543 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#3");
2544 Assert
.IsNotNull (ex
.Message
, "#4");
2545 Assert
.IsNull (ex
.InnerException
, "#5");
2550 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
2551 if (createdKey
!= null) {
2552 createdKey
.Close ();
2553 softwareKey
.DeleteSubKeyTree (subKeyName
);
2561 [Test
] // SetValue (String, Object, RegistryValueKind)
2562 public void SetValue2_Name_Empty ()
2564 string subKeyName
= Guid
.NewGuid ().ToString ();
2566 RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
);
2568 createdKey
.SetValue (string.Empty
, "value1",
2569 RegistryValueKind
.String
);
2570 string [] names
= createdKey
.GetValueNames ();
2571 Assert
.IsNotNull (names
, "#A1");
2572 Assert
.AreEqual (1, names
.Length
, "#A2");
2573 Assert
.IsNotNull (names
[0], "#A3");
2574 Assert
.AreEqual (string.Empty
, names
[0], "#A4");
2575 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#A5");
2576 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
), "#A6");
2577 Assert
.IsNotNull (createdKey
.GetValue (null), "#A7");
2578 Assert
.AreEqual ("value1", createdKey
.GetValue (null), "#A8");
2580 createdKey
.SetValue (null, "value2",
2581 RegistryValueKind
.String
);
2582 names
= createdKey
.GetValueNames ();
2583 Assert
.IsNotNull (names
, "#B1");
2584 Assert
.AreEqual (1, names
.Length
, "#B2");
2585 Assert
.IsNotNull (names
[0], "#B3");
2586 Assert
.AreEqual (string.Empty
, names
[0], "#B4");
2587 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#B5");
2588 Assert
.AreEqual ("value2", createdKey
.GetValue (string.Empty
), "#B6");
2589 Assert
.IsNotNull (createdKey
.GetValue (null), "#B7");
2590 Assert
.AreEqual ("value2", createdKey
.GetValue (null), "#B8");
2593 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2597 [Test
] // SetValue (String, Object, RegistryValueKind)
2598 public void SetValue2_Name_MaxLength ()
2600 string subKeyName
= Guid
.NewGuid ().ToString ();
2603 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2604 string name
= new string ('a', 254);
2606 createdKey
.SetValue (name
, "value1",
2607 RegistryValueKind
.String
);
2608 Assert
.IsNotNull (createdKey
.GetValue (name
), "#A1");
2609 createdKey
.DeleteValue (name
);
2610 Assert
.IsNull (createdKey
.GetValue (name
), "#A2");
2612 name
= new string ('a', 255);
2614 createdKey
.SetValue (name
, "value2",
2615 RegistryValueKind
.String
);
2616 Assert
.IsNotNull (createdKey
.GetValue (name
), "#B1");
2617 createdKey
.DeleteValue (name
);
2618 Assert
.IsNull (createdKey
.GetValue (name
), "#B2");
2620 name
= new string ('a', 256);
2623 createdKey
.SetValue (name
, "value2",
2624 RegistryValueKind
.String
);
2625 Assert
.Fail ("#C1");
2626 } catch (ArgumentException ex
) {
2627 // Registry subkeys should not be
2628 // greater than 255 characters
2629 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#C2");
2630 Assert
.IsNull (ex
.InnerException
, "#C3");
2631 Assert
.IsNotNull (ex
.Message
, "#C4");
2632 Assert
.IsTrue (ex
.Message
.IndexOf ("255") != -1, "#C5");
2633 Assert
.IsNull (ex
.ParamName
, "#C6");
2638 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
2639 if (createdKey
!= null) {
2640 createdKey
.Close ();
2641 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2648 [Test
] // SetValue (String, Object, RegistryValueKind)
2649 public void SetValue2_Name_Null ()
2651 string subKeyName
= Guid
.NewGuid ().ToString ();
2653 RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
);
2655 createdKey
.SetValue (null, "value1",
2656 RegistryValueKind
.String
);
2657 string [] names
= createdKey
.GetValueNames ();
2658 Assert
.IsNotNull (names
, "#A1");
2659 Assert
.AreEqual (1, names
.Length
, "#A2");
2660 Assert
.IsNotNull (names
[0], "#A3");
2661 Assert
.AreEqual (string.Empty
, names
[0], "#A4");
2662 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#A5");
2663 Assert
.AreEqual ("value1", createdKey
.GetValue (string.Empty
), "#A6");
2664 Assert
.IsNotNull (createdKey
.GetValue (null), "#A7");
2665 Assert
.AreEqual ("value1", createdKey
.GetValue (null), "#A8");
2667 createdKey
.SetValue (string.Empty
, "value2",
2668 RegistryValueKind
.String
);
2669 names
= createdKey
.GetValueNames ();
2670 Assert
.IsNotNull (names
, "#B1");
2671 Assert
.AreEqual (1, names
.Length
, "#B2");
2672 Assert
.IsNotNull (names
[0], "#B3");
2673 Assert
.AreEqual (string.Empty
, names
[0], "#B4");
2674 Assert
.IsNotNull (createdKey
.GetValue (string.Empty
), "#B5");
2675 Assert
.AreEqual ("value2", createdKey
.GetValue (string.Empty
), "#B6");
2676 Assert
.IsNotNull (createdKey
.GetValue (null), "#B7");
2677 Assert
.AreEqual ("value2", createdKey
.GetValue (null), "#B8");
2680 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2684 [Test
] // SetValue (String, Object, RegistryValueKind)
2685 public void SetValue2_Value_Null ()
2687 string subKeyName
= Guid
.NewGuid ().ToString ();
2689 RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
);
2692 createdKey
.SetValue ("Name", null,
2693 RegistryValueKind
.String
);
2695 } catch (ArgumentNullException ex
) {
2696 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
2697 Assert
.IsNull (ex
.InnerException
, "#3");
2698 Assert
.IsNotNull (ex
.Message
, "#4");
2699 Assert
.AreEqual ("value", ex
.ParamName
, "#5");
2703 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2708 public void SubKeyCount ()
2710 string subKeyName
= Guid
.NewGuid ().ToString ();
2713 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2714 // check if key was successfully created
2715 Assert
.IsNotNull (createdKey
, "#A1");
2716 using (RegistryKey subKey
= createdKey
.CreateSubKey ("monotemp1")) {
2719 Assert
.AreEqual (1, createdKey
.SubKeyCount
, "#A2");
2720 using (RegistryKey subKey
= createdKey
.CreateSubKey ("monotemp2")) {
2723 Assert
.AreEqual (2, createdKey
.SubKeyCount
, "#A3");
2725 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2726 Assert
.IsNotNull (createdKey
, "#B1");
2727 Assert
.AreEqual (2, createdKey
.SubKeyCount
, "#B2");
2729 using (RegistryKey createdKey2
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
2730 Assert
.IsNotNull (createdKey2
, "#B3");
2731 Assert
.AreEqual (2, createdKey2
.SubKeyCount
, "#B4");
2732 createdKey2
.DeleteSubKey ("monotemp1");
2733 Assert
.AreEqual (1, createdKey2
.SubKeyCount
, "#B5");
2735 Assert
.AreEqual (1, createdKey
.SubKeyCount
, "#B6");
2739 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
2740 if (createdKey
!= null) {
2741 createdKey
.Close ();
2742 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2750 public void SubKeyCount_Key_Removed ()
2752 string subKeyName
= Guid
.NewGuid ().ToString ();
2755 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2756 // check if key was successfully created
2757 Assert
.IsNotNull (createdKey
, "#A1");
2758 using (RegistryKey subKey
= createdKey
.CreateSubKey ("monotemp1")) {
2761 Assert
.AreEqual (1, createdKey
.SubKeyCount
, "#A2");
2762 using (RegistryKey subKey
= createdKey
.CreateSubKey ("monotemp2")) {
2765 Assert
.AreEqual (2, createdKey
.SubKeyCount
, "#A3");
2767 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2768 Assert
.IsNotNull (createdKey
, "#B1");
2769 Assert
.AreEqual (2, createdKey
.SubKeyCount
, "#B2");
2771 // remove created key
2772 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2775 Assert
.Fail ("#C1: " + createdKey
.SubKeyCount
);
2776 } catch (IOException ex
) {
2777 // Illegal operation attempted on a registry key that
2778 // has been marked for deletion
2779 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#14");
2780 Assert
.IsNotNull (ex
.Message
, "#15");
2781 Assert
.IsNull (ex
.InnerException
, "#16");
2786 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
2787 if (createdKey
!= null) {
2788 createdKey
.Close ();
2789 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2797 public void ValueCount ()
2799 string subKeyName
= Guid
.NewGuid ().ToString ();
2802 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2803 // check if key was successfully created
2804 Assert
.IsNotNull (createdKey
, "#A1");
2805 Assert
.AreEqual (0, createdKey
.ValueCount
, "#A2");
2806 createdKey
.SetValue ("name1", "value1");
2807 Assert
.AreEqual (1, createdKey
.ValueCount
, "#A3");
2808 createdKey
.SetValue ("name2", "value2");
2809 Assert
.AreEqual (2, createdKey
.ValueCount
, "#A4");
2810 createdKey
.SetValue ("name2", "value2b");
2811 Assert
.AreEqual (2, createdKey
.ValueCount
, "#A5");
2812 createdKey
.SetValue ("name3", "value3");
2813 Assert
.AreEqual (3, createdKey
.ValueCount
, "#A6");
2815 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2816 Assert
.IsNotNull (createdKey
, "#B1");
2817 Assert
.AreEqual (3, createdKey
.ValueCount
, "#B2");
2819 using (RegistryKey createdKey2
= Registry
.CurrentUser
.OpenSubKey (subKeyName
, true)) {
2820 Assert
.IsNotNull (createdKey2
, "#B3");
2821 Assert
.AreEqual (3, createdKey2
.ValueCount
, "#B4");
2822 createdKey2
.DeleteValue ("name2");
2823 Assert
.AreEqual (2, createdKey2
.ValueCount
, "#B5");
2825 Assert
.AreEqual (2, createdKey
.ValueCount
, "#B6");
2829 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
2830 if (createdKey
!= null) {
2831 createdKey
.Close ();
2832 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2840 public void ValueCount_Key_Removed ()
2842 string subKeyName
= Guid
.NewGuid ().ToString ();
2845 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
2846 // check if key was successfully created
2847 Assert
.IsNotNull (createdKey
, "#A1");
2848 Assert
.AreEqual (0, createdKey
.ValueCount
, "#A2");
2849 createdKey
.SetValue ("name1", "value1");
2850 Assert
.AreEqual (1, createdKey
.ValueCount
, "#A3");
2851 createdKey
.SetValue ("name2", "value2");
2852 Assert
.AreEqual (2, createdKey
.ValueCount
, "#A4");
2853 createdKey
.SetValue ("name2", "value2b");
2854 Assert
.AreEqual (2, createdKey
.ValueCount
, "#A5");
2855 createdKey
.SetValue ("name3", "value3");
2856 Assert
.AreEqual (3, createdKey
.ValueCount
, "#A6");
2858 using (RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
)) {
2859 Assert
.IsNotNull (createdKey
, "#B1");
2860 Assert
.AreEqual (3, createdKey
.ValueCount
, "#B2");
2862 // remove created key
2863 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2866 Assert
.Fail ("#C1: " + createdKey
.ValueCount
);
2867 } catch (IOException ex
) {
2868 // Illegal operation attempted on a registry key that
2869 // has been marked for deletion
2870 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#14");
2871 Assert
.IsNotNull (ex
.Message
, "#15");
2872 Assert
.IsNull (ex
.InnerException
, "#16");
2877 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
2878 if (createdKey
!= null) {
2879 createdKey
.Close ();
2880 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
2888 public void bug79051 ()
2890 string subKeyName
= Guid
.NewGuid ().ToString ();
2892 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
2894 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
2895 createdKey
.SetValue ("test", "whatever");
2896 createdKey
.Close ();
2897 softwareKey
.DeleteSubKeyTree (subKeyName
);
2901 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
2902 if (createdKey
!= null) {
2903 createdKey
.Close ();
2904 softwareKey
.DeleteSubKeyTree (subKeyName
);
2913 public void bug79059 ()
2915 string subKeyName
= Guid
.NewGuid ().ToString ();
2917 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
2919 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
2920 using (RegistryKey softwareKey2
= Registry
.CurrentUser
.OpenSubKey ("software")) {
2922 createdKey
.Close ();
2923 softwareKey
.DeleteSubKeyTree (subKeyName
);
2927 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
2928 if (createdKey
!= null) {
2929 createdKey
.Close ();
2930 softwareKey
.DeleteSubKeyTree (subKeyName
);
2939 public void bugnew1 ()
2941 string subKeyName
= Guid
.NewGuid ().ToString ();
2943 using (RegistryKey softwareKey
= Registry
.CurrentUser
.OpenSubKey ("software", true)) {
2945 using (RegistryKey createdKey
= softwareKey
.CreateSubKey (subKeyName
)) {
2946 createdKey
.SetValue ("name1", "value1");
2948 RegistryKey testKey
= null;
2950 testKey
= createdKey
.OpenSubKey ("test", true);
2951 if (testKey
== null)
2952 testKey
= createdKey
.CreateSubKey ("test");
2953 testKey
.SetValue ("another", "one");
2955 if (testKey
!= null)
2959 createdKey
.SetValue ("name2", "value2");
2960 Assert
.IsNotNull (createdKey
.GetValue ("name1"), "#2");
2961 Assert
.AreEqual ("value1", createdKey
.GetValue ("name1"), "#3");
2962 Assert
.IsNotNull (createdKey
.GetValue ("name2"), "#4");
2963 Assert
.AreEqual ("value2", createdKey
.GetValue ("name2"), "#5");
2965 string [] names
= createdKey
.GetValueNames ();
2966 Assert
.IsNotNull (names
, "#6");
2967 Assert
.AreEqual (2, names
.Length
, "#7");
2968 Assert
.AreEqual ("name1", names
[0], "#8");
2969 Assert
.AreEqual ("name2", names
[1], "#9");
2971 softwareKey
.DeleteSubKeyTree (subKeyName
);
2973 using (RegistryKey openedKey
= softwareKey
.OpenSubKey (subKeyName
, true)) {
2974 Assert
.IsNull (openedKey
, "#10");
2977 Assert
.IsNull (createdKey
.GetValue ("name1"), "#11");
2978 Assert
.IsNull (createdKey
.GetValue ("name2"), "#12");
2981 createdKey
.GetValueNames ();
2982 Assert
.Fail ("#13");
2983 } catch (IOException ex
) {
2984 // Illegal operation attempted on a registry key that
2985 // has been marked for deletion
2986 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#14");
2987 Assert
.IsNotNull (ex
.Message
, "#15");
2988 Assert
.IsNull (ex
.InnerException
, "#16");
2992 createdKey
.SetValue ("name1", "value1");
2993 Assert
.Fail ("#17");
2994 } catch (IOException ex
) {
2995 // Illegal operation attempted on a registry key that
2996 // has been marked for deletion
2997 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#18");
2998 Assert
.IsNotNull (ex
.Message
, "#19");
2999 Assert
.IsNull (ex
.InnerException
, "#20");
3003 createdKey
.SetValue ("newname", "value1");
3004 Assert
.Fail ("#21");
3005 } catch (IOException ex
) {
3006 // Illegal operation attempted on a registry key that
3007 // has been marked for deletion
3008 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#22");
3009 Assert
.IsNotNull (ex
.Message
, "#23");
3010 Assert
.IsNull (ex
.InnerException
, "#24");
3013 Assert
.IsNull (createdKey
.OpenSubKey ("test"), "#25");
3014 Assert
.IsNull (createdKey
.OpenSubKey ("test", true), "#26");
3015 Assert
.IsNull (createdKey
.OpenSubKey ("new"), "#27");
3016 Assert
.IsNull (createdKey
.OpenSubKey ("new", true), "#28");
3019 createdKey
.CreateSubKey ("new");
3020 Assert
.Fail ("#29");
3021 } catch (IOException ex
) {
3022 // Illegal operation attempted on a registry key that
3023 // has been marked for deletion
3024 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#30");
3025 Assert
.IsNotNull (ex
.Message
, "#31");
3026 Assert
.IsNull (ex
.InnerException
, "#32");
3031 RegistryKey createdKey
= softwareKey
.OpenSubKey (subKeyName
);
3032 if (createdKey
!= null) {
3033 createdKey
.Close ();
3034 softwareKey
.DeleteSubKeyTree (subKeyName
);
3043 public void bugnew2 () // values cannot be written on registry root (hive)
3045 string [] names
= Registry
.CurrentUser
.GetValueNames ();
3046 Assert
.IsNotNull (names
, "#1");
3047 Registry
.CurrentUser
.SetValue ("name1", "value1");
3048 Assert
.IsNotNull (Registry
.CurrentUser
.GetValue ("name1"), "#2");
3049 Assert
.AreEqual ("value1", Registry
.CurrentUser
.GetValue ("name1"), "#3");
3050 string [] newNames
= Registry
.CurrentUser
.GetValueNames ();
3051 Assert
.IsNotNull (newNames
, "#4");
3052 Assert
.AreEqual (names
.Length
+ 1, newNames
.Length
, "#5");
3053 Registry
.CurrentUser
.DeleteValue ("name1");
3057 public void bugnew3 () // on Windows, key cannot be closed twice
3059 string subKeyName
= Guid
.NewGuid ().ToString ();
3062 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
3063 createdKey
.Close ();
3066 RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
3071 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
3072 if (createdKey
!= null) {
3073 createdKey
.Close ();
3074 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
3082 public void bugnew4 () // Key cannot be flushed once it has been closed
3084 string subKeyName
= Guid
.NewGuid ().ToString ();
3087 using (RegistryKey createdKey
= Registry
.CurrentUser
.CreateSubKey (subKeyName
)) {
3088 createdKey
.Close ();
3091 RegistryKey openedKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
3096 RegistryKey createdKey
= Registry
.CurrentUser
.OpenSubKey (subKeyName
);
3097 if (createdKey
!= null) {
3098 createdKey
.Close ();
3099 Registry
.CurrentUser
.DeleteSubKeyTree (subKeyName
);
3106 private bool RunningOnUnix
{
3108 int p
= (int) Environment
.OSVersion
.Platform
;
3109 return ((p
== 4) || (p
== 128) || (p
== 6));