New test.
[mono.git] / mcs / class / corlib / Test / Microsoft.Win32 / RegistryKeyTest.cs
1 //
2 // RegistryKeyTest.cs - NUnit Test Cases for Microsoft.Win32.RegistryKey
3 //
4 // Authors:
5 //      mei (mei@work.email.ne.jp)
6 //      Robert Jordan (robertj@gmx.net)
7 //      Gert Driesen (drieseng@users.sourceforge.net)
8 //
9 // (C) 2005 mei
10 // (C) 2004, 2005 Novell (http://www.novell.com)
11 //
12
13 using System;
14 using System.IO;
15
16 using Microsoft.Win32;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.Microsoft.Win32
21 {
22         [TestFixture]
23         public class RegistryKeyTest
24         {
25                 private const string mimeroot = @"MIME\Database\Content Type";
26                 [Test]
27                 [Category("NotWorking")]
28                 // This will not work on Linux ever
29                 public void TestGetValue ()
30                 {
31                         RegistryKey root = Registry.ClassesRoot;
32                         RegistryKey key;
33                         
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");
38                 }
39
40                 //
41                 // Unit test for bug #77212
42                 //
43                 [Test]
44                 public void TestHandle ()
45                 {
46                         // this test is for Windows only
47                         if (RunningOnUnix)
48                                 return;
49
50                         // this regpath always exists under windows
51                         RegistryKey k = Registry.CurrentUser
52                                 .OpenSubKey ("Software", false)
53                                 .OpenSubKey ("Microsoft", false)
54                                 .OpenSubKey ("Windows", false);
55                         
56                         Assert.IsNotNull (k, "#01");
57                 }
58
59                 [Test]
60                 public void OpenSubKey ()
61                 {
62                         RegistryKey key = Registry.LocalMachine;
63
64                         // HKEY_LOCAL_MACHINE\software should always exist on Windows
65                         // and is automatically created on Linux
66                         Assert.IsNotNull (key.OpenSubKey ("Software"), "#A1");
67                         Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#A2");
68
69                         key = Registry.CurrentUser;
70
71                         // HKEY_CURRENT_USER\software should always exist on Windows
72                         // and is automatically created on Linux
73                         Assert.IsNotNull (key.OpenSubKey ("Software"), "#B1");
74                         Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#B2");
75
76                         key = Registry.Users;
77
78                         // HKEY_USERS\software should not exist on Windows, and should not
79                         // be created automatically on Linux
80                         Assert.IsNull (key.OpenSubKey ("Software"), "#C1");
81                         Assert.IsNull (key.OpenSubKey ("soFtware"), "#C2");
82                 }
83
84                 [Test]
85                 public void OpenSubKey_Key_DoesNotExist ()
86                 {
87                         string subKeyName = Guid.NewGuid ().ToString ();
88                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#1"); // read-only
89                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#2"); // writable
90                 }
91
92                 [Test]
93                 public void OpenSubKey_Key_Removed ()
94                 {
95                         string subKeyName = Guid.NewGuid ().ToString ();
96
97                         try {
98                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
99                                         // check if key was successfully created
100                                         Assert.IsNotNull (createdKey, "#1");
101                                         RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
102                                         subKey.Close ();
103                                 }
104                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
105                                         Assert.IsNotNull (createdKey, "#2");
106                                         using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
107                                                 Assert.IsNotNull (createdKey, "#3");
108                                         }
109                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
110
111                                         // read-only
112                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
113                                         Assert.IsNull (createdKey.OpenSubKey ("monotemp"), "#5"); // read-only
114                                         // writable
115                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#6");
116                                         Assert.IsNull (createdKey.OpenSubKey ("monotemp", true), "#7"); 
117                                 }
118                         } finally {
119                                 try {
120                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
121                                         if (createdKey != null) {
122                                                 createdKey.Close ();
123                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
124                                         }
125                                 } catch {
126                                 }
127                         }
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (ArgumentNullException))]
132                 public void OpenSubKey_Name_Null ()
133                 {
134                         Registry.CurrentUser.OpenSubKey (null);
135                 }
136
137                 [Test]
138                 [Category ("NotWorking")] // MS should not allow this
139                 public void OpenSubKey_Name_Empty ()
140                 {
141                         // read-only
142                         using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty)) {
143                                 Assert.IsNotNull (emptyKey, "#1");
144                         }
145                         // writable
146                         using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty, true)) {
147                                 Assert.IsNotNull (emptyKey, "#1");
148                         }
149                 }
150
151                 [Test]
152                 public void Close_Local_Hive ()
153                 {
154                         RegistryKey hive = Registry.CurrentUser;
155                         hive.Close ();
156
157                         Assert.IsNotNull (hive.GetSubKeyNames (), "#1");
158                         Assert.IsNull (hive.GetValue ("doesnotexist"), "#2");
159                         Assert.IsNotNull (hive.GetValueNames (), "#3");
160                         Assert.IsNull (hive.OpenSubKey ("doesnotexist"), "#4");
161                         Assert.IsNotNull (hive.SubKeyCount, "#5");
162                         Assert.IsNotNull (hive.ToString (), "#6");
163
164                         // closing key again does not have any effect
165                         hive.Close ();
166                 }
167
168                 [Test]
169                 public void Close_Local_Key ()
170                 {
171                         RegistryKey key = Registry.CurrentUser.OpenSubKey ("SOFTWARE");
172                         key.Close ();
173
174                         // closing a key twice does not have any effect
175                         key.Close ();
176
177                         try {
178                                 key.CreateSubKey ("a");
179                                 Assert.Fail ("#1");
180                         } catch (ObjectDisposedException) {
181                         }
182
183                         try {
184                                 key.DeleteSubKey ("doesnotexist");
185                                 Assert.Fail ("#2");
186                         } catch (ObjectDisposedException) {
187                         }
188
189                         try {
190                                 key.DeleteSubKeyTree ("doesnotexist");
191                                 Assert.Fail ("#3");
192                         } catch (ObjectDisposedException) {
193                         }
194
195                         try {
196                                 key.DeleteValue ("doesnotexist");
197                                 Assert.Fail ("#4");
198                         } catch (ObjectDisposedException) {
199                         }
200
201                         // flushing a closed key does not have any effect
202                         key.Flush ();
203
204                         try {
205                                 key.GetSubKeyNames ();
206                                 Assert.Fail ("#5");
207                         } catch (ObjectDisposedException) {
208                         }
209
210                         try {
211                                 key.GetValue ("doesnotexist");
212                                 Assert.Fail ("#6");
213                         } catch (ObjectDisposedException) {
214                         }
215
216                         try {
217                                 key.GetValueNames ();
218                                 Assert.Fail ("#7");
219                         } catch (ObjectDisposedException) {
220                         }
221
222                         try {
223                                 key.OpenSubKey ("doesnotexist");
224                                 Assert.Fail ("#8");
225                         } catch (ObjectDisposedException) {
226                         }
227
228                         try {
229                                 key.SetValue ("doesnotexist", "something");
230                                 Assert.Fail ("#9");
231                         } catch (ObjectDisposedException) {
232                         }
233
234                         try {
235                                 int x = key.SubKeyCount;
236                                 Assert.Fail ("#10:" + x);
237                         } catch (ObjectDisposedException) {
238                         }
239
240                         try {
241                                 key.ToString ();
242                                 Assert.Fail ("#11");
243                         } catch (ObjectDisposedException) {
244                         }
245
246                         try {
247                                 int x = key.ValueCount;
248                                 Assert.Fail ("#12:" + x);
249                         } catch (ObjectDisposedException) {
250                         }
251                 }
252
253                 [Test]
254                 public void Close_Remote_Hive ()
255                 {
256                         // access to registry of remote machines is not implemented on unix
257                         if (RunningOnUnix)
258                                 return;
259
260                         RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
261                                 RegistryHive.CurrentUser, Environment.MachineName);
262                         hive.Close ();
263
264                         // closing a remote hive twice does not have any effect
265                         hive.Close ();
266
267                         try {
268                                 hive.CreateSubKey ("a");
269                                 Assert.Fail ("#1");
270                         } catch (ObjectDisposedException) {
271                         }
272
273                         try {
274                                 hive.DeleteSubKey ("doesnotexist");
275                                 Assert.Fail ("#2");
276                         } catch (ObjectDisposedException) {
277                         }
278
279                         try {
280                                 hive.DeleteSubKeyTree ("doesnotexist");
281                                 Assert.Fail ("#3");
282                         } catch (ObjectDisposedException) {
283                         }
284
285                         try {
286                                 hive.DeleteValue ("doesnotexist");
287                                 Assert.Fail ("#4");
288                         } catch (ObjectDisposedException) {
289                         }
290
291                         // flushing a closed hive does not have any effect
292                         hive.Flush ();
293
294                         try {
295                                 hive.GetSubKeyNames ();
296                                 Assert.Fail ("#5");
297                         } catch (ObjectDisposedException) {
298                         }
299
300                         try {
301                                 hive.GetValue ("doesnotexist");
302                                 Assert.Fail ("#6");
303                         } catch (ObjectDisposedException) {
304                         }
305
306                         try {
307                                 hive.GetValueNames ();
308                                 Assert.Fail ("#7");
309                         } catch (ObjectDisposedException) {
310                         }
311
312                         try {
313                                 hive.OpenSubKey ("doesnotexist");
314                                 Assert.Fail ("#8");
315                         } catch (ObjectDisposedException) {
316                         }
317
318                         try {
319                                 hive.SetValue ("doesnotexist", "something");
320                                 Assert.Fail ("#9");
321                         } catch (ObjectDisposedException) {
322                         }
323
324                         try {
325                                 int x = hive.SubKeyCount;
326                                 Assert.Fail ("#10:" + x);
327                         } catch (ObjectDisposedException) {
328                         }
329
330                         try {
331                                 hive.ToString ();
332                                 Assert.Fail ("#11");
333                         } catch (ObjectDisposedException) {
334                         }
335
336                         try {
337                                 int x = hive.ValueCount;
338                                 Assert.Fail ("#12:" + x);
339                         } catch (ObjectDisposedException) {
340                         }
341                 }
342
343                 [Test]
344                 public void Close_Remote_Key ()
345                 {
346                         // access to registry of remote machines is not implemented on unix
347                         if (RunningOnUnix)
348                                 return;
349
350                         RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
351                                 RegistryHive.CurrentUser, Environment.MachineName);
352                         RegistryKey key = hive.OpenSubKey ("SOFTWARE");
353                         key.Close ();
354
355                         // closing a remote key twice does not have any effect
356                         key.Close ();
357
358                         try {
359                                 key.CreateSubKey ("a");
360                                 Assert.Fail ("#1");
361                         } catch (ObjectDisposedException) {
362                         }
363
364                         try {
365                                 key.DeleteSubKey ("doesnotexist");
366                                 Assert.Fail ("#2");
367                         } catch (ObjectDisposedException) {
368                         }
369
370                         try {
371                                 key.DeleteSubKeyTree ("doesnotexist");
372                                 Assert.Fail ("#3");
373                         } catch (ObjectDisposedException) {
374                         }
375
376                         try {
377                                 key.DeleteValue ("doesnotexist");
378                                 Assert.Fail ("#4");
379                         } catch (ObjectDisposedException) {
380                         }
381
382                         // flushing a closed key does not have any effect
383                         key.Flush ();
384
385                         try {
386                                 key.GetSubKeyNames ();
387                                 Assert.Fail ("#5");
388                         } catch (ObjectDisposedException) {
389                         }
390
391                         try {
392                                 key.GetValue ("doesnotexist");
393                                 Assert.Fail ("#6");
394                         } catch (ObjectDisposedException) {
395                         }
396
397                         try {
398                                 key.GetValueNames ();
399                                 Assert.Fail ("#7");
400                         } catch (ObjectDisposedException) {
401                         }
402
403                         try {
404                                 key.OpenSubKey ("doesnotexist");
405                                 Assert.Fail ("#8");
406                         } catch (ObjectDisposedException) {
407                         }
408
409                         try {
410                                 key.SetValue ("doesnotexist", "something");
411                                 Assert.Fail ("#9");
412                         } catch (ObjectDisposedException) {
413                         }
414
415                         try {
416                                 int x = key.SubKeyCount;
417                                 Assert.Fail ("#10:" + x);
418                         } catch (ObjectDisposedException) {
419                         }
420
421                         try {
422                                 key.ToString ();
423                                 Assert.Fail ("#11");
424                         } catch (ObjectDisposedException) {
425                         }
426
427                         try {
428                                 int x = key.ValueCount;
429                                 Assert.Fail ("#12:" + x);
430                         } catch (ObjectDisposedException) {
431                         }
432
433                         hive.Close ();
434                 }
435
436                 [Test]
437                 public void CreateSubKey ()
438                 {
439                         string subKeyName = Guid.NewGuid ().ToString ();
440
441                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
442                         try {
443                                 // check if key was successfully created
444                                 Assert.IsNotNull (createdKey, "#1");
445                                 // software subkey should not be created automatically
446                                 Assert.IsNull (createdKey.OpenSubKey ("software"), "#2");
447                         } finally {
448                                 // clean-up
449                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
450                         }
451                 }
452
453                 [Test]
454                 public void CreateSubKey_Key_ReadOnly ()
455                 {
456                         string subKeyName = Guid.NewGuid ().ToString ();
457
458                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
459                                 RegistryKey createdKey = null;
460                                 try {
461                                         try {
462                                                 createdKey = softwareKey.CreateSubKey (subKeyName);
463                                                 Assert.Fail ("#1");
464                                         } catch (UnauthorizedAccessException ex) {
465                                                 // Cannot write to the registry key
466                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
467                                                 Assert.IsNotNull (ex.Message, "#3");
468                                                 Assert.IsNull (ex.InnerException, "#4");
469                                         }
470                                 } finally {
471                                         if (createdKey != null)
472                                                 createdKey.Close ();
473                                 }
474                         }
475                 }
476
477                 [Test]
478                 public void CreateSubKey_Key_Removed ()
479                 {
480                         string subKeyName = Guid.NewGuid ().ToString ();
481
482                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
483                                 try {
484                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
485                                                 softwareKey.DeleteSubKeyTree (subKeyName);
486                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
487                                                 try {
488                                                         createdKey.CreateSubKey ("test");
489                                                         Assert.Fail ("#2");
490                                                 } catch (IOException ex) {
491                                                         // Illegal operation attempted on a registry key that
492                                                         // has been marked for deletion
493                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
494                                                         Assert.IsNotNull (ex.Message, "#4");
495                                                         Assert.IsNull (ex.InnerException, "#5");
496                                                 }
497                                         }
498                                 } finally {
499                                         try {
500                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
501                                                 if (createdKey != null) {
502                                                         createdKey.Close ();
503                                                         softwareKey.DeleteSubKeyTree (subKeyName);
504                                                 }
505                                         } catch {
506                                         }
507                                 }
508                         }
509                 }
510
511                 [Test]
512                 [Category ("NotWorking")] // MS should not allow this
513                 public void CreateSubKey_Name_Empty ()
514                 {
515                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
516                                 using (RegistryKey emptyKey = softwareKey.CreateSubKey (string.Empty)) {
517                                         Assert.IsNotNull (emptyKey, "#1");
518                                         emptyKey.SetValue ("name1", "value1");
519                                 }
520                         }
521                 }
522
523                 [Test]
524                 [ExpectedException (typeof (ArgumentNullException))]
525                 public void CreateSubKey_Name_Null ()
526                 {
527                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
528                                 softwareKey.CreateSubKey (null);
529                         }
530                 }
531
532                 [Test]
533                 public void DeleteSubKey ()
534                 {
535                         string subKeyName = Guid.NewGuid ().ToString ();
536
537                         try {
538                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
539                                         // check if key was successfully created
540                                         Assert.IsNotNull (createdKey, "#1");
541                                 }
542                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
543                                         Assert.IsNotNull (createdKey, "#2");
544                                         Registry.CurrentUser.DeleteSubKey (subKeyName);
545                                 }
546                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
547                                         Assert.IsNull (createdKey, "#3");
548                                 }
549                         } finally {
550                                 try {
551                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
552                                         if (createdKey != null) {
553                                                 createdKey.Close ();
554                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
555                                         }
556                                 } catch {
557                                 }
558                         }
559                 }
560
561                 [Test]
562                 public void DeleteSubKey_Key_HasChildKeys ()
563                 {
564                         string subKeyName = Guid.NewGuid ().ToString ();
565
566                         try {
567                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
568                                         // check if key was successfully created
569                                         Assert.IsNotNull (createdKey, "#1");
570                                         RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
571                                         subKey.Close ();
572                                 }
573                                 try {
574                                         Registry.CurrentUser.DeleteSubKey (subKeyName);
575                                         Assert.Fail ("#2");
576                                 } catch (InvalidOperationException ex) {
577                                         // Registry key has subkeys and recursive removes are not
578                                         // supported by this method
579                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
580                                         Assert.IsNotNull (ex.Message, "#4");
581                                         Assert.IsNull (ex.InnerException, "#5");
582                                 }
583                         } finally {
584                                 try {
585                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
586                                         if (createdKey != null) {
587                                                 createdKey.Close ();
588                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
589                                         }
590                                 } catch {
591                                 }
592                         }
593                 }
594
595                 [Test]
596                 public void DeleteSubKey_Key_ReadOnly ()
597                 {
598                         string subKeyName = Guid.NewGuid ().ToString ();
599
600                         try {
601                                 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
602                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
603                                         createdKey.Close ();
604                                 }
605
606                                 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
607                                         try {
608                                                 softwareKey.DeleteSubKey (subKeyName);
609                                                 Assert.Fail ("#1");
610                                         } catch (UnauthorizedAccessException ex) {
611                                                 // Cannot write to the registry key
612                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
613                                                 Assert.IsNotNull (ex.Message, "#3");
614                                                 Assert.IsNull (ex.InnerException, "#4");
615                                         }
616                                 }
617                         } finally {
618                                 try {
619                                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
620                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
621                                                 if (createdKey != null) {
622                                                         createdKey.Close ();
623                                                         softwareKey.DeleteSubKeyTree (subKeyName);
624                                                 }
625                                         }
626                                 } catch {
627                                 }
628                         }
629                 }
630
631                 [Test]
632                 public void DeleteSubKey_Key_DoesNotExist ()
633                 {
634                         string subKeyName = Guid.NewGuid ().ToString ();
635
636                         try {
637                                 Registry.CurrentUser.DeleteSubKey (subKeyName);
638                                 Assert.Fail ("#A1");
639                         } catch (ArgumentException ex) {
640                                 // Cannot delete a subkey tree because the subkey does not exist
641                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
642                                 Assert.IsNotNull (ex.Message, "#A3");
643                                 Assert.IsNull (ex.InnerException, "#A4");
644                         }
645
646                         try {
647                                 Registry.CurrentUser.DeleteSubKey (subKeyName, true);
648                                 Assert.Fail ("#B1");
649                         } catch (ArgumentException ex) {
650                                 // Cannot delete a subkey tree because the subkey does not exist
651                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
652                                 Assert.IsNotNull (ex.Message, "#B3");
653                                 Assert.IsNull (ex.InnerException, "#B4");
654                         }
655
656                         Registry.CurrentUser.DeleteSubKey (subKeyName, false);
657                 }
658
659                 [Test]
660                 public void DeleteSubKey_Key_Removed ()
661                 {
662                         string subKeyName = Guid.NewGuid ().ToString ();
663
664                         try {
665                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
666                                         // check if key was successfully created
667                                         Assert.IsNotNull (createdKey, "#1");
668                                         RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
669                                         subKey.Close ();
670                                 }
671                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
672                                         Assert.IsNotNull (createdKey, "#2");
673                                         using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
674                                                 Assert.IsNotNull (createdKey, "#3");
675                                         }
676                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
677                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
678                                         try {
679                                                 createdKey.DeleteSubKey ("monotemp");
680                                                 Assert.Fail ("#5");
681                                         } catch (ArgumentException ex) {
682                                                 // Cannot delete a subkey tree because the subkey does
683                                                 // not exist
684                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
685                                                 Assert.IsNotNull (ex.Message, "#7");
686                                                 Assert.IsNull (ex.InnerException, "#8");
687                                         }
688                                 }
689                         } finally {
690                                 try {
691                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
692                                         if (createdKey != null) {
693                                                 createdKey.Close ();
694                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
695                                         }
696                                 } catch {
697                                 }
698                         }
699                 }
700
701                 [Test]
702                 [Category ("NotWorking")] // MS should not allow this
703                 public void DeleteSubKey_Name_Empty ()
704                 {
705                         string subKeyName = Guid.NewGuid ().ToString ();
706
707                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
708                                 try {
709                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
710                                         createdKey.DeleteSubKey (string.Empty);
711                                         createdKey.Close ();
712
713                                         createdKey = softwareKey.OpenSubKey (subKeyName);
714                                         Assert.IsNull (createdKey, "#1");
715                                 } finally {
716                                         try {
717                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
718                                                 if (createdKey != null)
719                                                         createdKey.Close ();
720                                                 softwareKey.DeleteSubKeyTree (subKeyName);
721                                         } catch {
722                                         }
723                                 }
724                         }
725                 }
726
727                 [Test]
728                 public void DeleteSubKey_Name_Null ()
729                 {
730                         string subKeyName = Guid.NewGuid ().ToString ();
731
732                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
733                                 try {
734                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
735                                         try {
736                                                 createdKey.DeleteSubKey (null);
737                                                 Assert.Fail ("#1");
738                                         } catch (ArgumentNullException ex) {
739                                                 // Value cannot be null. Parameter name: subkey
740                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
741                                                 Assert.IsNotNull (ex.Message, "#3");
742                                                 Assert.IsNull (ex.InnerException, "#4");
743                                         }
744                                 } finally {
745                                         try {
746                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
747                                                 if (createdKey != null)
748                                                         createdKey.Close ();
749                                                 softwareKey.DeleteSubKeyTree (subKeyName);
750                                         } catch {
751                                         }
752                                 }
753                         }
754                 }
755
756                 [Test]
757                 public void DeleteSubKeyTree ()
758                 {
759                         // TODO: 
760                         // - remove key with subkeys
761                         // - remove key of which some subkeys are marked for deletion
762                         // - remove key with values
763                 }
764
765                 [Test]
766                 [ExpectedException (typeof (ArgumentException))]
767                 public void DeleteSubKeyTree_Key_DoesNotExist ()
768                 {
769                         // Cannot delete a subkey tree because the subkey does not exist
770                         string subKeyName = Guid.NewGuid ().ToString ();
771                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
772                 }
773
774                 [Test]
775                 public void DeleteSubKeyTree_Key_ReadOnly ()
776                 {
777                         string subKeyName = Guid.NewGuid ().ToString ();
778
779                         try {
780                                 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
781                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
782                                         createdKey.Close ();
783                                 }
784
785                                 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
786                                         try {
787                                                 softwareKey.DeleteSubKeyTree (subKeyName);
788                                                 Assert.Fail ("#1");
789                                         } catch (UnauthorizedAccessException ex) {
790                                                 // Cannot write to the registry key
791                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
792                                                 Assert.IsNotNull (ex.Message, "#3");
793                                                 Assert.IsNull (ex.InnerException, "#4");
794                                         }
795                                 }
796                         } finally {
797                                 try {
798                                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
799                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
800                                                 if (createdKey != null)
801                                                         createdKey.Close ();
802                                                 softwareKey.DeleteSubKeyTree (subKeyName);
803                                         }
804                                 } catch {
805                                 }
806                         }
807                 }
808
809                 [Test]
810                 public void DeleteSubKeyTree_Key_Removed ()
811                 {
812                         string subKeyName = Guid.NewGuid ().ToString ();
813
814                         try {
815                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
816                                         // check if key was successfully created
817                                         Assert.IsNotNull (createdKey, "#1");
818                                         RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
819                                         subKey.Close ();
820                                 }
821                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
822                                         Assert.IsNotNull (createdKey, "#2");
823                                         using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
824                                                 Assert.IsNotNull (createdKey, "#3");
825                                         }
826                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
827                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
828                                         try {
829                                                 createdKey.DeleteSubKeyTree ("monotemp");
830                                                 Assert.Fail ("#5");
831                                         } catch (ArgumentException ex) {
832                                                 // Cannot delete a subkey tree because the subkey does
833                                                 // not exist
834                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
835                                                 Assert.IsNotNull (ex.Message, "#7");
836                                                 Assert.IsNull (ex.InnerException, "#8");
837                                         }
838                                 }
839                         } finally {
840                                 try {
841                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
842                                         if (createdKey != null) {
843                                                 createdKey.Close ();
844                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
845                                         }
846                                 } catch {
847                                 }
848                         }
849                 }
850
851                 [Test]
852                 [Category ("NotWorking")] // MS should not allow this
853                 public void DeleteSubKeyTree_Name_Empty ()
854                 {
855                         string subKeyName = Guid.NewGuid ().ToString ();
856
857                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
858                                 try {
859                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
860                                         createdKey.DeleteSubKeyTree (string.Empty);
861                                         createdKey.Close ();
862
863                                         createdKey = softwareKey.OpenSubKey (subKeyName);
864                                         Assert.IsNull (createdKey, "#1");
865                                 } finally {
866                                         try {
867                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
868                                                 if (createdKey != null)
869                                                         createdKey.Close ();
870                                                 softwareKey.DeleteSubKeyTree (subKeyName);
871                                         } catch {
872                                         }
873                                 }
874                         }
875                 }
876
877                 [Test]
878                 public void DeleteSubKeyTree_Name_Null ()
879                 {
880                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
881                                 try {
882                                         softwareKey.DeleteSubKeyTree (null);
883                                         Assert.Fail ("#1");
884                                 } catch (ArgumentNullException ex) {
885                                         // Value cannot be null. Parameter name: subkey
886                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
887                                         Assert.IsNotNull (ex.Message, "#3");
888                                         Assert.IsNull (ex.InnerException, "#4");
889                                 }
890                         }
891                 }
892
893                 [Test]
894                 public void DeleteValue ()
895                 {
896                         string subKeyName = Guid.NewGuid ().ToString ();
897
898                         try {
899                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
900                                         // check if key was successfully created
901                                         Assert.IsNotNull (createdKey, "#A1");
902                                         createdKey.SetValue ("name1", "value1");
903                                         createdKey.SetValue ("name2", "value2");
904                                         string [] names = createdKey.GetValueNames ();
905                                         Assert.IsNotNull (names, "#A2");
906                                         Assert.AreEqual (2, names.Length, "#A3");
907                                         Assert.IsNotNull (names [0], "#A4");
908                                         Assert.AreEqual ("name1", names [0], "#A5");
909                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A6");
910                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A7");
911                                         Assert.AreEqual ("name2", names [1], "#A8");
912                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#A9");
913                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#A10");
914                                 }
915                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
916                                         Assert.IsNotNull (createdKey, "#B1");
917                                         createdKey.DeleteValue ("name1");
918                                         string [] names = createdKey.GetValueNames ();
919                                         Assert.IsNotNull (names, "#B2");
920                                         Assert.AreEqual (1, names.Length, "#B3");
921                                         Assert.IsNotNull (names [0], "#B4");
922                                         Assert.AreEqual ("name2", names [0], "#B5");
923                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#B6");
924                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#B7");
925                                 }
926                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
927                                         string [] names = createdKey.GetValueNames ();
928                                         Assert.IsNotNull (names, "#C1");
929                                         Assert.AreEqual (1, names.Length, "#C2");
930                                         Assert.IsNotNull (names [0], "#C3");
931                                         Assert.AreEqual ("name2", names [0], "#C4");
932                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#C5");
933                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#C6");
934                                 }
935                         } finally {
936                                 try {
937                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
938                                         if (createdKey != null) {
939                                                 createdKey.Close ();
940                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
941                                         }
942                                 } catch {
943                                 }
944                         }
945                 }
946
947                 [Test]
948                 public void DeleteValue_Key_ReadOnly ()
949                 {
950                         string subKeyName = Guid.NewGuid ().ToString ();
951
952                         try {
953                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
954                                         createdKey.SetValue ("name1", "value1");
955                                 }
956
957                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
958                                         try {
959                                                 // deleting value that exists
960                                                 createdKey.DeleteValue ("name1");
961                                                 Assert.Fail ("#A1");
962                                         } catch (UnauthorizedAccessException ex) {
963                                                 // Cannot write to the registry key
964                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#A2");
965                                                 Assert.IsNotNull (ex.Message, "#A3");
966                                                 Assert.IsNull (ex.InnerException, "#A4");
967                                         }
968
969                                         try {
970                                                 // deleting value that exists
971                                                 createdKey.DeleteValue ("name1", true);
972                                                 Assert.Fail ("#B1");
973                                         } catch (UnauthorizedAccessException ex) {
974                                                 // Cannot write to the registry key
975                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#B2");
976                                                 Assert.IsNotNull (ex.Message, "#B3");
977                                                 Assert.IsNull (ex.InnerException, "#B4");
978                                         }
979
980                                         try {
981                                                 // deleting value that exists
982                                                 createdKey.DeleteValue ("name1", false);
983                                                 Assert.Fail ("#C1");
984                                         } catch (UnauthorizedAccessException ex) {
985                                                 // Cannot write to the registry key
986                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#C2");
987                                                 Assert.IsNotNull (ex.Message, "#C3");
988                                                 Assert.IsNull (ex.InnerException, "#C4");
989                                         }
990
991                                         try {
992                                                 // deleting value that does not exist
993                                                 createdKey.DeleteValue ("name2");
994                                                 Assert.Fail ("#D1");
995                                         } catch (UnauthorizedAccessException ex) {
996                                                 // Cannot write to the registry key
997                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#D2");
998                                                 Assert.IsNotNull (ex.Message, "#D3");
999                                                 Assert.IsNull (ex.InnerException, "#D4");
1000                                         }
1001
1002                                         try {
1003                                                 // deleting value that does not exist
1004                                                 createdKey.DeleteValue ("name2", true);
1005                                                 Assert.Fail ("#E1");
1006                                         } catch (UnauthorizedAccessException ex) {
1007                                                 // Cannot write to the registry key
1008                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#E2");
1009                                                 Assert.IsNotNull (ex.Message, "#E3");
1010                                                 Assert.IsNull (ex.InnerException, "#E4");
1011                                         }
1012
1013                                         try {
1014                                                 // deleting value that does not exist
1015                                                 createdKey.DeleteValue ("name2", false);
1016                                                 Assert.Fail ("#F1");
1017                                         } catch (UnauthorizedAccessException ex) {
1018                                                 // Cannot write to the registry key
1019                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#F2");
1020                                                 Assert.IsNotNull (ex.Message, "#F3");
1021                                                 Assert.IsNull (ex.InnerException, "#F4");
1022                                         }
1023                                 }
1024                         } finally {
1025                                 try {
1026                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1027                                         if (createdKey != null) {
1028                                                 createdKey.Close ();
1029                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1030                                         }
1031                                 } catch {
1032                                 }
1033                         }
1034                 }
1035
1036                 [Test]
1037                 public void DeleteValue_Key_Removed ()
1038                 {
1039                         string subKeyName = Guid.NewGuid ().ToString ();
1040
1041                         try {
1042                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1043                                         // check if key was successfully created
1044                                         Assert.IsNotNull (createdKey, "#1");
1045                                         createdKey.SetValue ("name1", "value1");
1046                                 }
1047                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1048                                         Assert.IsNotNull (createdKey, "#2");
1049                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1050                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#3");
1051
1052                                         createdKey.DeleteValue ("name1");
1053                                         createdKey.DeleteValue ("name1", true);
1054                                         createdKey.DeleteValue ("name1", false);
1055                                 }
1056                         } finally {
1057                                 try {
1058                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1059                                         if (createdKey != null) {
1060                                                 createdKey.Close ();
1061                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1062                                         }
1063                                 } catch {
1064                                 }
1065                         }
1066                 }
1067
1068                 [Test]
1069                 public void DeleteValue_Value_DoesNotExist ()
1070                 {
1071                         string subKeyName = Guid.NewGuid ().ToString ();
1072
1073                         try {
1074                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1075                                         // check if key was successfully created
1076                                         Assert.IsNotNull (createdKey, "#A1");
1077                                         createdKey.SetValue ("name1", "value1");
1078
1079                                         try {
1080                                                 createdKey.DeleteValue ("name2");
1081                                                 Assert.Fail ("#B1");
1082                                         } catch (ArgumentException ex) {
1083                                                 // No value exists with that name
1084                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1085                                                 Assert.IsNotNull (ex.Message, "#B3");
1086                                                 Assert.IsNull (ex.InnerException, "#B4");
1087                                         }
1088
1089                                         try {
1090                                                 createdKey.DeleteValue ("name2", true);
1091                                                 Assert.Fail ("#C1");
1092                                         } catch (ArgumentException ex) {
1093                                                 // No value exists with that name
1094                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1095                                                 Assert.IsNotNull (ex.Message, "#C3");
1096                                                 Assert.IsNull (ex.InnerException, "#C4");
1097                                         }
1098
1099                                         createdKey.DeleteValue ("name2", false);
1100                                 }
1101                         } finally {
1102                                 try {
1103                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1104                                         if (createdKey != null) {
1105                                                 createdKey.Close ();
1106                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1107                                         }
1108                                 } catch {
1109                                 }
1110                         }
1111                 }
1112
1113                 [Test]
1114                 public void DeleteValue_Name_Empty ()
1115                 {
1116                         string subKeyName = Guid.NewGuid ().ToString ();
1117
1118                         try {
1119                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1120                                         createdKey.SetValue ("name1", "value1");
1121                                         createdKey.SetValue (string.Empty, "value2");
1122
1123                                         string [] names = createdKey.GetValueNames ();
1124                                         Assert.IsNotNull (names, "#A1");
1125                                         Assert.AreEqual (2, names.Length, "#A2");
1126                                         Assert.IsNotNull (names [0], "#A3");
1127                                         /*
1128                                         Assert.AreEqual ("name1", names [0], "#A4");
1129                                         */
1130                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1131                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1132                                         Assert.IsNotNull (names [1], "#A7");
1133                                         /*
1134                                         Assert.AreEqual (string.Empty, names [1], "#A8");
1135                                         */
1136                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A9");
1137                                         Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#A10");
1138
1139                                         createdKey.DeleteValue (string.Empty);
1140
1141                                         names = createdKey.GetValueNames ();
1142                                         Assert.IsNotNull (names, "#B1");
1143                                         Assert.AreEqual (1, names.Length, "#B2");
1144                                         Assert.IsNotNull (names [0], "#B3");
1145                                         Assert.AreEqual ("name1", names [0], "#B4");
1146                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#B5");
1147                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#B6");
1148
1149                                         try {
1150                                                 createdKey.DeleteValue (string.Empty);
1151                                                 Assert.Fail ("#C1");
1152                                         } catch (ArgumentException ex) {
1153                                                 // No value exists with that name
1154                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1155                                                 Assert.IsNotNull (ex.Message, "#C3");
1156                                                 Assert.IsNull (ex.InnerException, "#C4");
1157                                         }
1158
1159                                         try {
1160                                                 createdKey.DeleteValue (string.Empty, true);
1161                                                 Assert.Fail ("#D1");
1162                                         } catch (ArgumentException ex) {
1163                                                 // No value exists with that name
1164                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
1165                                                 Assert.IsNotNull (ex.Message, "#D3");
1166                                                 Assert.IsNull (ex.InnerException, "#D4");
1167                                         }
1168
1169                                         createdKey.DeleteValue (string.Empty, false);
1170
1171                                         names = createdKey.GetValueNames ();
1172                                         Assert.IsNotNull (names, "#E1");
1173                                         Assert.AreEqual (1, names.Length, "#E2");
1174                                         Assert.IsNotNull (names [0], "#E3");
1175                                         Assert.AreEqual ("name1", names [0], "#E4");
1176                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#E5");
1177                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#E6");
1178                                 }
1179                         } finally {
1180                                 try {
1181                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1182                                         if (createdKey != null) {
1183                                                 createdKey.Close ();
1184                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1185                                         }
1186                                 } catch {
1187                                 }
1188                         }
1189                 }
1190
1191                 [Test]
1192                 public void DeleteValue_Name_Null ()
1193                 {
1194                         string subKeyName = Guid.NewGuid ().ToString ();
1195
1196                         try {
1197                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1198                                         createdKey.SetValue ("name1", "value1");
1199                                         createdKey.SetValue (null, "value2");
1200
1201                                         string [] names = createdKey.GetValueNames ();
1202                                         Assert.IsNotNull (names, "#A1");
1203                                         Assert.AreEqual (2, names.Length, "#A2");
1204                                         Assert.IsNotNull (names [0], "#A3");
1205                                         /*
1206                                         Assert.AreEqual ("name1", names [0], "#A4");
1207                                         */
1208                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1209                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1210                                         Assert.IsNotNull (names [1], "#A7");
1211                                         /*
1212                                         Assert.AreEqual (string.Empty, names [1], "#A8");
1213                                         */
1214                                         Assert.IsNotNull (createdKey.GetValue (null), "#A9");
1215                                         Assert.AreEqual ("value2", createdKey.GetValue (null), "#A10");
1216
1217                                         try {
1218                                                 createdKey.DeleteValue (null);
1219                                                 Assert.Fail ("#B1");
1220                                         } catch (ArgumentNullException ex) {
1221                                                 // Value cannot be null. Parameter name: name
1222                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1223                                                 Assert.IsNotNull (ex.Message, "#B3");
1224                                                 Assert.IsNull (ex.InnerException, "#B4");
1225                                         }
1226
1227                                         try {
1228                                                 createdKey.DeleteValue (null, true);
1229                                                 Assert.Fail ("#C1");
1230                                         } catch (ArgumentNullException ex) {
1231                                                 // Value cannot be null. Parameter name: name
1232                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
1233                                                 Assert.IsNotNull (ex.Message, "#C3");
1234                                                 Assert.IsNull (ex.InnerException, "#C4");
1235                                         }
1236
1237                                         try {
1238                                                 createdKey.DeleteValue (null, false);
1239                                                 Assert.Fail ("#D1");
1240                                         } catch (ArgumentNullException ex) {
1241                                                 // Value cannot be null. Parameter name: name
1242                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
1243                                                 Assert.IsNotNull (ex.Message, "#D3");
1244                                                 Assert.IsNull (ex.InnerException, "#D4");
1245                                         }
1246                                 }
1247                         } finally {
1248                                 try {
1249                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1250                                         if (createdKey != null) {
1251                                                 createdKey.Close ();
1252                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1253                                         }
1254                                 } catch {
1255                                 }
1256                         }
1257                 }
1258
1259                 [Test]
1260                 public void GetValue ()
1261                 {
1262                         string subKeyName = Guid.NewGuid ().ToString ();
1263
1264                         try {
1265                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1266                                         createdKey.SetValue ("name1", "value1");
1267                                         createdKey.SetValue ("name2", "value2");
1268                                 }
1269
1270                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1271                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#1");
1272                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#2");
1273                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#3");
1274                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#4");
1275                                         Assert.IsNull (createdKey.GetValue ("name3"), "#5");
1276                                         Assert.AreEqual ("value3", createdKey.GetValue ("name3", "value3"), "#6");
1277                                         Assert.IsNull (createdKey.GetValue ("name3", null), "#7");
1278                                 }
1279                         } finally {
1280                                 try {
1281                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1282                                         if (createdKey != null) {
1283                                                 createdKey.Close ();
1284                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1285                                         }
1286                                 } catch {
1287                                 }
1288                         }
1289                 }
1290
1291                 [Test]
1292                 public void GetValue_Key_Removed ()
1293                 {
1294                         string subKeyName = Guid.NewGuid ().ToString ();
1295
1296                         try {
1297                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1298                                         createdKey.SetValue ("name1", "value1");
1299                                         createdKey.SetValue ("name2", "value2");
1300                                 }
1301
1302                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1303                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1304
1305                                         Assert.IsNull (createdKey.GetValue ("name1"), "#1");
1306                                         Assert.IsNotNull (createdKey.GetValue ("name1", "default"), "#2");
1307                                         Assert.AreEqual ("default", createdKey.GetValue ("name1", "default"), "#3");
1308                                         Assert.IsNull (createdKey.GetValue ("name3"), "#3");
1309                                         Assert.IsNotNull (createdKey.GetValue ("name3", "default"), "#4");
1310                                         Assert.AreEqual ("default", createdKey.GetValue ("name3", "default"), "#5");
1311                                 }
1312                         } finally {
1313                                 try {
1314                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1315                                         if (createdKey != null) {
1316                                                 createdKey.Close ();
1317                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1318                                         }
1319                                 } catch {
1320                                 }
1321                         }
1322                 }
1323
1324                 [Test]
1325                 public void GetValue_Name_Empty ()
1326                 {
1327                         string subKeyName = Guid.NewGuid ().ToString ();
1328
1329                         try {
1330                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1331                                         createdKey.SetValue ("name1", "value1");
1332                                         createdKey.SetValue ("name2", "value2");
1333
1334                                         Assert.IsNull (createdKey.GetValue (string.Empty), "#A1");
1335                                         Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#A2");
1336                                         Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#A3");
1337                                         Assert.IsNull (createdKey.GetValue (string.Empty, null), "#A4");
1338                                 }
1339
1340                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1341                                         Assert.IsNull (createdKey.GetValue (string.Empty), "#B1");
1342                                         Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#B2");
1343                                         Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#B3");
1344                                         Assert.IsNull (createdKey.GetValue (string.Empty, null), "#B4");
1345                                 }
1346
1347                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1348                                         createdKey.SetValue (string.Empty, "value1");
1349                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#C1");
1350                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#C2");
1351                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#C3");
1352                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#C4");
1353                                 }
1354
1355                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1356                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#D1");
1357                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#D2");
1358                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#D3");
1359                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#D4");
1360                                 }
1361                         } finally {
1362                                 try {
1363                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1364                                         if (createdKey != null) {
1365                                                 createdKey.Close ();
1366                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1367                                         }
1368                                 } catch {
1369                                 }
1370                         }
1371                 }
1372
1373                 [Test]
1374                 public void GetValue_Name_Null ()
1375                 {
1376                         string subKeyName = Guid.NewGuid ().ToString ();
1377
1378                         try {
1379                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1380                                         createdKey.SetValue ("name1", "value1");
1381                                         createdKey.SetValue ("name2", "value2");
1382
1383                                         Assert.IsNull (createdKey.GetValue (null), "#A1");
1384                                         Assert.IsNotNull (createdKey.GetValue (null, "default"), "#A2");
1385                                         Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#A3");
1386                                         Assert.IsNull (createdKey.GetValue (null, null), "#A4");
1387                                 }
1388
1389                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1390                                         Assert.IsNull (createdKey.GetValue (null), "#B1");
1391                                         Assert.IsNotNull (createdKey.GetValue (null, "default"), "#B2");
1392                                         Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#B3");
1393                                         Assert.IsNull (createdKey.GetValue (null, null), "#B4");
1394                                 }
1395
1396                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1397                                         createdKey.SetValue (string.Empty, "value1");
1398                                         Assert.IsNotNull (createdKey.GetValue (null), "#C1");
1399                                         Assert.AreEqual ("value1", createdKey.GetValue (null), "#C2");
1400                                         Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#C3");
1401                                         Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#C4");
1402                                 }
1403
1404                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1405                                         Assert.IsNotNull (createdKey.GetValue (null), "#D1");
1406                                         Assert.AreEqual ("value1", createdKey.GetValue (null), "#D2");
1407                                         Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#D3");
1408                                         Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#D4");
1409                                 }
1410                         } finally {
1411                                 try {
1412                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1413                                         if (createdKey != null) {
1414                                                 createdKey.Close ();
1415                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1416                                         }
1417                                 } catch {
1418                                 }
1419                         }
1420                 }
1421
1422 #if NET_2_0
1423                 [Test]
1424                 public void GetValue_Expand ()
1425                 {
1426                         string subKeyName = Guid.NewGuid ().ToString ();
1427
1428                         try {
1429                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1430                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1431                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1432
1433                                         createdKey.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1434                                                 RegistryValueKind.ExpandString);
1435                                         createdKey.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1436                                         createdKey.SetValue ("name3", "just some text",
1437                                                 RegistryValueKind.ExpandString);
1438
1439                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#A1");
1440                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#A2");
1441                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#A3");
1442                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1443                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A4");
1444                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1445                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A5");
1446                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1447                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A6");
1448                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1449                                                 null, RegistryValueOptions.None), "#A7");
1450                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1451                                                 null, RegistryValueOptions.None), "#A8");
1452                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1453                                                 null, RegistryValueOptions.None), "#A9");
1454
1455                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "789");
1456                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "666");
1457
1458                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#B1");
1459                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#B2");
1460                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#B3");
1461                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1462                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B4");
1463                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1464                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B5");
1465                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1466                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B6");
1467                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1468                                                 null, RegistryValueOptions.None), "#B7");
1469                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1470                                                 null, RegistryValueOptions.None), "#B8");
1471                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1472                                                 null, RegistryValueOptions.None), "#B9");
1473                                 }
1474                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1475                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#C1");
1476                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#C2");
1477                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#C3");
1478                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1479                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C4");
1480                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1481                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C5");
1482                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1483                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C6");
1484                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1485                                                 null, RegistryValueOptions.None), "#C7");
1486                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1487                                                 null, RegistryValueOptions.None), "#C8");
1488                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1489                                                 null, RegistryValueOptions.None), "#C9");
1490
1491                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1492                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1493
1494                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#D1");
1495                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#D2");
1496                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#D3");
1497                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1498                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D4");
1499                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1500                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D5");
1501                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1502                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D6");
1503                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1504                                                 null, RegistryValueOptions.None), "#D7");
1505                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1506                                                 null, RegistryValueOptions.None), "#D8");
1507                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1508                                                 null, RegistryValueOptions.None), "#D9");
1509                                 }
1510                         } finally {
1511                                 try {
1512                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1513                                         if (createdKey != null) {
1514                                                 createdKey.Close ();
1515                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1516                                         }
1517                                 } catch {
1518                                 }
1519                         }
1520                 }
1521 #endif
1522
1523                 [Test]
1524                 public void GetValueNames ()
1525                 {
1526                         string subKeyName = Guid.NewGuid ().ToString ();
1527
1528                         try {
1529                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1530                                         string [] names = createdKey.GetValueNames ();
1531                                         Assert.IsNotNull (names, "#A1");
1532                                         Assert.AreEqual (0, names.Length, "#A2");
1533
1534                                         createdKey.SetValue ("name1", "value1");
1535                                         createdKey.SetValue ("name2", "value2");
1536                                         createdKey.SetValue ("namelong", "value3");
1537                                         createdKey.SetValue ("name3", "value4");
1538
1539                                         Assert.AreEqual (4, createdKey.ValueCount, "#B1");
1540                                         names = createdKey.GetValueNames ();
1541                                         Assert.IsNotNull (names, "#B2");
1542                                         Assert.AreEqual (4, names.Length, "#B3");
1543                                 }
1544
1545                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1546                                         string [] names = createdKey.GetValueNames ();
1547                                         Assert.IsNotNull (names, "#C1");
1548                                         Assert.AreEqual (4, names.Length, "#C2");
1549
1550                                         // Mono's Unix registry API uses a hashtable to store the
1551                                         // values (and their names), so names are not returned in
1552                                         // order
1553                                         //
1554                                         // to test whether the names returned by GetValueNames
1555                                         // match what we expect, we use these names to remove the
1556                                         // the values from the created keys and such we should end
1557                                         // up with zero values
1558                                         for (int i = 0; i < names.Length; i++) {
1559                                                 string valueName = names [i];
1560                                                 createdKey.DeleteValue (valueName);
1561                                         }
1562
1563                                         // all values should be removed now
1564                                         Assert.AreEqual (0, createdKey.ValueCount, "#C3");
1565                                 }
1566                         } finally {
1567                                 try {
1568                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1569                                         if (createdKey != null) {
1570                                                 createdKey.Close ();
1571                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1572                                         }
1573                                 } catch {
1574                                 }
1575                         }
1576                 }
1577
1578                 [Test]
1579                 public void GetValueNames_Key_Removed ()
1580                 {
1581                         string subKeyName = Guid.NewGuid ().ToString ();
1582
1583                         try {
1584                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1585                                         createdKey.SetValue ("name1", "value1");
1586                                         createdKey.SetValue ("name2", "value2");
1587
1588                                         string [] names = createdKey.GetValueNames ();
1589                                         Assert.IsNotNull (names, "#A1");
1590                                         Assert.AreEqual (2, names.Length, "#A2");
1591                                 }
1592
1593                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1594                                         string [] names = createdKey.GetValueNames ();
1595                                         Assert.IsNotNull (names, "#B1");
1596                                         Assert.AreEqual (2, names.Length, "#B2");
1597
1598                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1599
1600                                         try {
1601                                                 createdKey.GetValueNames ();
1602                                                 Assert.Fail ("#C1");
1603                                         } catch (IOException ex) {
1604                                                 // Illegal operation attempted on a registry key that
1605                                                 // has been marked for deletion
1606                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
1607                                                 Assert.IsNotNull (ex.Message, "#C3");
1608                                                 Assert.IsNull (ex.InnerException, "#C4");
1609                                         }
1610                                 }
1611                         } finally {
1612                                 try {
1613                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1614                                         if (createdKey != null) {
1615                                                 createdKey.Close ();
1616                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1617                                         }
1618                                 } catch {
1619                                 }
1620                         }
1621                 }
1622
1623                 [Test] // bug #78519
1624                 public void GetSubKeyNamesTest ()
1625                 {
1626                         string subKeyName = Guid.NewGuid ().ToString ();
1627
1628                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1629                         try {
1630                                 // check if key was successfully created
1631                                 Assert.IsNotNull (createdKey, "#A");
1632
1633                                 RegistryKey subKey = createdKey.CreateSubKey ("foo");
1634                                 Assert.IsNotNull (subKey, "#B1");
1635                                 Assert.AreEqual (1, createdKey.SubKeyCount, "#B2");
1636                                 string[] subKeyNames = createdKey.GetSubKeyNames ();
1637                                 Assert.IsNotNull (subKeyNames, "#B3");
1638                                 Assert.AreEqual (1, subKeyNames.Length, "#B4");
1639                                 Assert.AreEqual ("foo", subKeyNames[0], "#B5");
1640
1641                                 subKey = createdKey.CreateSubKey ("longfoo");
1642                                 Assert.IsNotNull (subKey, "#C1");
1643                                 Assert.AreEqual (2, createdKey.SubKeyCount, "#C2");
1644                                 subKeyNames = createdKey.GetSubKeyNames ();
1645                                 Assert.IsNotNull (subKeyNames, "#C3");
1646                                 Assert.AreEqual (2, subKeyNames.Length, "#C4");
1647                                 Assert.AreEqual ("foo", subKeyNames [0], "#C5");
1648                                 Assert.AreEqual ("longfoo", subKeyNames [1], "#C6");
1649
1650                                 subKey = createdKey.CreateSubKey ("sfoo");
1651                                 Assert.IsNotNull (subKey, "#D1");
1652                                 Assert.AreEqual (3, createdKey.SubKeyCount, "#D2");
1653                                 subKeyNames = createdKey.GetSubKeyNames ();
1654                                 Assert.IsNotNull (subKeyNames, "#D3");
1655                                 Assert.AreEqual (3, subKeyNames.Length, "#D4");
1656                                 Assert.AreEqual ("foo", subKeyNames [0], "#D5");
1657                                 Assert.AreEqual ("longfoo", subKeyNames [1], "#D6");
1658                                 Assert.AreEqual ("sfoo", subKeyNames [2], "#D7");
1659
1660                                 foreach (string name in subKeyNames) {
1661                                         createdKey.DeleteSubKeyTree (name);
1662                                 }
1663                                 Assert.AreEqual (0, createdKey.SubKeyCount, "#E");
1664                         } finally {
1665                                 // clean-up
1666                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1667                         }
1668                 }
1669
1670                 [Test]
1671                 public void OpenRemoteBaseKey ()
1672                 {
1673                         // access to registry of remote machines is not implemented on unix
1674                         if (RunningOnUnix)
1675                                 return;
1676
1677                         RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
1678                                 RegistryHive.CurrentUser, Environment.MachineName);
1679                         Assert.IsNotNull (hive, "#1");
1680
1681                         RegistryKey key = hive.OpenSubKey ("SOFTWARE");
1682                         Assert.IsNotNull (key, "#2");
1683                         key.Close ();
1684
1685                         hive.Close ();
1686                 }
1687
1688                 [Test]
1689                 [ExpectedException (typeof (ArgumentNullException))]
1690                 public void OpenRemoteBaseKey_MachineName_Null ()
1691                 {
1692                         RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser, null);
1693                 }
1694
1695                 [Test]
1696                 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
1697                 {
1698                         // access to registry of remote machines is not implemented on unix
1699                         if (RunningOnUnix)
1700                                 return;
1701
1702                         try {
1703                                 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
1704                                         "DOESNOTEXIST");
1705                                 Assert.Fail ("#1");
1706                         } catch (IOException ex) {
1707                                 // The network path was not found
1708                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1709                                 Assert.IsNotNull (ex.Message, "#3");
1710                                 Assert.IsNull (ex.InnerException, "#4");
1711                         }
1712                 }
1713
1714                 [Test]
1715                 public void SetValue_Name_Null ()
1716                 {
1717                         string subKeyName = Guid.NewGuid ().ToString ();
1718
1719                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1720                         try {
1721                                 createdKey.SetValue (null, "value1");
1722                                 string [] names = createdKey.GetValueNames ();
1723                                 Assert.IsNotNull (names, "#A1");
1724                                 Assert.AreEqual (1, names.Length, "#A2");
1725                                 Assert.IsNotNull (names [0], "#A3");
1726                                 Assert.AreEqual (string.Empty, names [0], "#A4");
1727                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
1728                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
1729                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
1730                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
1731
1732                                 createdKey.SetValue (string.Empty, "value2");
1733                                 names = createdKey.GetValueNames ();
1734                                 Assert.IsNotNull (names, "#B1");
1735                                 Assert.AreEqual (1, names.Length, "#B2");
1736                                 Assert.IsNotNull (names [0], "#B3");
1737                                 Assert.AreEqual (string.Empty, names [0], "#B4");
1738                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
1739                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
1740                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
1741                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
1742                         } finally {
1743                                 // clean-up
1744                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1745                         }
1746                 }
1747
1748                 [Test]
1749                 public void SetValue_Name_Empty ()
1750                 {
1751                         string subKeyName = Guid.NewGuid ().ToString ();
1752
1753                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1754                         try {
1755                                 createdKey.SetValue (string.Empty, "value1");
1756                                 string [] names = createdKey.GetValueNames ();
1757                                 Assert.IsNotNull (names, "#A1");
1758                                 Assert.AreEqual (1, names.Length, "#A2");
1759                                 Assert.IsNotNull (names [0], "#A3");
1760                                 Assert.AreEqual (string.Empty, names [0], "#A4");
1761                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
1762                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
1763                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
1764                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
1765
1766                                 createdKey.SetValue (null, "value2");
1767                                 names = createdKey.GetValueNames ();
1768                                 Assert.IsNotNull (names, "#B1");
1769                                 Assert.AreEqual (1, names.Length, "#B2");
1770                                 Assert.IsNotNull (names [0], "#B3");
1771                                 Assert.AreEqual (string.Empty, names [0], "#B4");
1772                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
1773                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
1774                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
1775                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
1776                         } finally {
1777                                 // clean-up
1778                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1779                         }
1780                 }
1781
1782                 [Test]
1783                 [ExpectedException (typeof (ArgumentNullException))]
1784                 public void SetValue_Null ()
1785                 {
1786                         string subKeyName = Guid.NewGuid ().ToString ();
1787
1788                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1789                         try {
1790                                 // null value should result in ArgumentNullException
1791                                 createdKey.SetValue ("Name", null);
1792                         } finally {
1793                                 // clean-up
1794                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1795                         }
1796                 }
1797
1798                 [Test]
1799                 public void SetValue_Boolean ()
1800                 {
1801                         string subKeyName = Guid.NewGuid ().ToString ();
1802
1803                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1804                         try {
1805                                 // we created a new subkey, so value should not exist
1806                                 Assert.IsNull (createdKey.GetValue ("Installed"), "#1");
1807                                 // create value
1808                                 createdKey.SetValue ("Installed", true);
1809                                 // get value
1810                                 object value = createdKey.GetValue ("Installed");
1811                                 // value should exist
1812                                 Assert.IsNotNull (value, "#2");
1813                                 // type of value should be string
1814                                 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1815                                 // ensure value matches
1816                                 Assert.AreEqual (true.ToString (), value, "#4");
1817                         } finally {
1818                                 // clean-up
1819                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1820                         }
1821                 }
1822
1823                 [Test]
1824                 public void SetValue_Byte ()
1825                 {
1826                         string subKeyName = Guid.NewGuid ().ToString ();
1827
1828                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1829                         try {
1830                                 // we created a new subkey, so value should not exist
1831                                 Assert.IsNull (createdKey.GetValue ("Flags"), "#1");
1832                                 // create value
1833                                 createdKey.SetValue ("Flags", (byte) 5);
1834                                 // get value
1835                                 object value = createdKey.GetValue ("Flags");
1836                                 // value should exist
1837                                 Assert.IsNotNull (value, "#2");
1838                                 // type of value should be string
1839                                 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1840                                 // ensure value matches
1841                                 Assert.AreEqual ("5", value, "#4");
1842                         } finally {
1843                                 // clean-up
1844                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1845                         }
1846                 }
1847
1848                 [Test]
1849                 public void SetValue_ByteArray ()
1850                 {
1851                         string subKeyName = Guid.NewGuid ().ToString ();
1852
1853                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1854                         try {
1855                                 // we created a new subkey, so value should not exist
1856                                 Assert.IsNull (createdKey.GetValue ("Flags"), "#1");
1857                                 // create value
1858                                 createdKey.SetValue ("Flags", new byte[] { 1, 5 });
1859                                 // get value
1860                                 object value = createdKey.GetValue ("Flags");
1861                                 // value should exist
1862                                 Assert.IsNotNull (value, "#2");
1863                                 // type of value should be string
1864                                 Assert.AreEqual (typeof (byte[]), value.GetType (), "#3");
1865                                 // ensure value matches
1866                                 Assert.AreEqual (new byte[] { 1, 5 }, value, "#4");
1867                         } finally {
1868                                 // clean-up
1869                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1870                         }
1871                 }
1872
1873                 [Test]
1874                 public void SetValue_DateTime ()
1875                 {
1876                         string subKeyName = Guid.NewGuid ().ToString ();
1877
1878                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1879                         try {
1880                                 object rawValue = DateTime.Now;
1881
1882                                 // we created a new subkey, so value should not exist
1883                                 Assert.IsNull (createdKey.GetValue ("Path"), "#1");
1884                                 // create value
1885                                 createdKey.SetValue ("Path", rawValue);
1886                                 // get value
1887                                 object value = createdKey.GetValue ("Path");
1888                                 // value should exist
1889                                 Assert.IsNotNull (value, "#2");
1890                                 // type of value should be string
1891                                 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1892                                 // ensure value matches
1893                                 Assert.AreEqual (rawValue.ToString (), value, "#4");
1894                         } finally {
1895                                 // clean-up
1896                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1897                         }
1898                 }
1899
1900                 [Test]
1901                 public void SetValue_Int32 ()
1902                 {
1903                         string subKeyName = Guid.NewGuid ().ToString ();
1904
1905                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1906                         try {
1907                                 // we created a new subkey, so value should not exist
1908                                 Assert.IsNull (createdKey.GetValue ("RefCount"), "#1");
1909                                 // create value
1910                                 createdKey.SetValue ("RefCount", 5);
1911                                 // get value
1912                                 object value = createdKey.GetValue ("RefCount");
1913                                 // value should exist
1914                                 Assert.IsNotNull (value, "#2");
1915                                 // type of value should be int
1916                                 Assert.AreEqual (typeof (int), value.GetType (), "#3");
1917                                 // ensure value matches
1918                                 Assert.AreEqual (5, value, "#4");
1919                         } finally {
1920                                 // clean-up
1921                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1922                         }
1923                 }
1924
1925                 [Test]
1926                 public void SetValue_Int64 ()
1927                 {
1928                         string subKeyName = Guid.NewGuid ().ToString ();
1929
1930                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1931                         try {
1932                                 // we created a new subkey, so value should not exist
1933                                 Assert.IsNull (createdKey.GetValue ("Ticks"), "#1");
1934                                 // create value
1935                                 createdKey.SetValue ("Ticks", 500L);
1936                                 // get value
1937                                 object value = createdKey.GetValue ("Ticks");
1938                                 // value should exist
1939                                 Assert.IsNotNull (value, "#2");
1940                                 // type of value should be string
1941                                 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1942                                 // ensure value matches
1943                                 Assert.AreEqual ("500", value, "#4");
1944                         } finally {
1945                                 // clean-up
1946                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1947                         }
1948                 }
1949
1950                 [Test]
1951                 public void SetValue_String ()
1952                 {
1953                         string subKeyName = Guid.NewGuid ().ToString ();
1954
1955                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1956                         try {
1957                                 // we created a new subkey, so value should not exist
1958                                 Assert.IsNull (createdKey.GetValue ("Path"), "#1");
1959                                 // create value
1960                                 createdKey.SetValue ("Path", "/usr/lib/whatever");
1961                                 // get value
1962                                 object value = createdKey.GetValue ("Path");
1963                                 // value should exist
1964                                 Assert.IsNotNull (value, "#2");
1965                                 // type of value should be string
1966                                 Assert.AreEqual (typeof (string), value.GetType (), "#3");
1967                                 // ensure value matches
1968                                 Assert.AreEqual ("/usr/lib/whatever", value, "#4");
1969                         } finally {
1970                                 // clean-up
1971                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1972                         }
1973                 }
1974
1975                 [Test]
1976                 public void SetValue_StringArray ()
1977                 {
1978                         string subKeyName = Guid.NewGuid ().ToString ();
1979
1980                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
1981                         try {
1982                                 // we created a new subkey, so value should not exist
1983                                 Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#1");
1984                                 // create value
1985                                 createdKey.SetValue ("DependsOnGroup", new string[] { "A", "B" });
1986                                 // get value
1987                                 object value = createdKey.GetValue ("DependsOnGroup");
1988                                 // value should exist
1989                                 Assert.IsNotNull (value, "#2");
1990                                 // type of value should be string
1991                                 Assert.AreEqual (typeof (string[]), value.GetType (), "#3");
1992                                 // ensure value matches
1993                                 Assert.AreEqual (new string[] { "A", "B" }, value, "#4");
1994                         } finally {
1995                                 // clean-up
1996                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1997                         }
1998                 }
1999
2000                 [Test]
2001                 public void SetValue_Key_ReadOnly ()
2002                 {
2003                         string subKeyName = Guid.NewGuid ().ToString ();
2004
2005                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2006                                 try {
2007                                         softwareKey.SetValue ("name1", "value1");
2008                                         Assert.Fail ("#1");
2009                                 } catch (UnauthorizedAccessException ex) {
2010                                         // Cannot write to the registry key
2011                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2012                                         Assert.IsNotNull (ex.Message, "#3");
2013                                         Assert.IsNull (ex.InnerException, "#4");
2014                                 }
2015                         }
2016
2017                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2018                                 try {
2019                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2020                                         }
2021
2022                                         using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2023                                                 try {
2024                                                         createdKey.SetValue ("name1", "value1");
2025                                                         Assert.Fail ("#1");
2026                                                 } catch (UnauthorizedAccessException ex) {
2027                                                         // Cannot write to the registry key
2028                                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2029                                                         Assert.IsNotNull (ex.Message, "#3");
2030                                                         Assert.IsNull (ex.InnerException, "#4");
2031                                                 }
2032                                         }
2033                                 } finally {
2034                                         try {
2035                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2036                                                 if (createdKey != null) {
2037                                                         createdKey.Close ();
2038                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2039                                                 }
2040                                         } catch {
2041                                         }
2042                                 }
2043                         }
2044                 }
2045
2046                 [Test]
2047                 public void SetValue_Key_Removed ()
2048                 {
2049                         string subKeyName = Guid.NewGuid ().ToString ();
2050
2051                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2052                                 try {
2053                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2054                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2055                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2056                                                 try {
2057                                                         createdKey.SetValue ("name1", "value1");
2058                                                         Assert.Fail ("#2");
2059                                                 } catch (IOException ex) {
2060                                                         // Illegal operation attempted on a registry key that
2061                                                         // has been marked for deletion
2062                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2063                                                         Assert.IsNotNull (ex.Message, "#4");
2064                                                         Assert.IsNull (ex.InnerException, "#5");
2065                                                 }
2066                                         }
2067                                 } finally {
2068                                         try {
2069                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2070                                                 if (createdKey != null) {
2071                                                         createdKey.Close ();
2072                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2073                                                 }
2074                                         } catch {
2075                                         }
2076                                 }
2077                         }
2078                 }
2079
2080                 [Test]
2081                 public void SubKeyCount ()
2082                 {
2083                         string subKeyName = Guid.NewGuid ().ToString ();
2084
2085                         try {
2086                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2087                                         // check if key was successfully created
2088                                         Assert.IsNotNull (createdKey, "#A1");
2089                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2090                                                 subKey.Close ();
2091                                         }
2092                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2093                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2094                                                 subKey.Close ();
2095                                         }
2096                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2097                                 }
2098                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2099                                         Assert.IsNotNull (createdKey, "#B1");
2100                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
2101
2102                                         using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2103                                                 Assert.IsNotNull (createdKey2, "#B3");
2104                                                 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
2105                                                 createdKey2.DeleteSubKey ("monotemp1");
2106                                                 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
2107                                         }
2108                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
2109                                 }
2110                         } finally {
2111                                 try {
2112                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2113                                         if (createdKey != null) {
2114                                                 createdKey.Close ();
2115                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2116                                         }
2117                                 } catch {
2118                                 }
2119                         }
2120                 }
2121
2122                 [Test]
2123                 public void SubKeyCount_Key_Removed ()
2124                 {
2125                         string subKeyName = Guid.NewGuid ().ToString ();
2126
2127                         try {
2128                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2129                                         // check if key was successfully created
2130                                         Assert.IsNotNull (createdKey, "#A1");
2131                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2132                                                 subKey.Close ();
2133                                         }
2134                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2135                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2136                                                 subKey.Close ();
2137                                         }
2138                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2139                                 }
2140                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2141                                         Assert.IsNotNull (createdKey, "#B1");
2142                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
2143
2144                                         // remove created key
2145                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2146
2147                                         try {
2148                                                 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
2149                                         } catch (IOException ex) {
2150                                                 // Illegal operation attempted on a registry key that
2151                                                 // has been marked for deletion
2152                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
2153                                                 Assert.IsNotNull (ex.Message, "#15");
2154                                                 Assert.IsNull (ex.InnerException, "#16");
2155                                         }
2156                                 }
2157                         } finally {
2158                                 try {
2159                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2160                                         if (createdKey != null) {
2161                                                 createdKey.Close ();
2162                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2163                                         }
2164                                 } catch {
2165                                 }
2166                         }
2167                 }
2168
2169                 [Test]
2170                 public void ValueCount ()
2171                 {
2172                         string subKeyName = Guid.NewGuid ().ToString ();
2173
2174                         try {
2175                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2176                                         // check if key was successfully created
2177                                         Assert.IsNotNull (createdKey, "#A1");
2178                                         Assert.AreEqual (0, createdKey.ValueCount, "#A2");
2179                                         createdKey.SetValue ("name1", "value1");
2180                                         Assert.AreEqual (1, createdKey.ValueCount, "#A3");
2181                                         createdKey.SetValue ("name2", "value2");
2182                                         Assert.AreEqual (2, createdKey.ValueCount, "#A4");
2183                                         createdKey.SetValue ("name2", "value2b");
2184                                         Assert.AreEqual (2, createdKey.ValueCount, "#A5");
2185                                         createdKey.SetValue ("name3", "value3");
2186                                         Assert.AreEqual (3, createdKey.ValueCount, "#A6");
2187                                 }
2188                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2189                                         Assert.IsNotNull (createdKey, "#B1");
2190                                         Assert.AreEqual (3, createdKey.ValueCount, "#B2");
2191
2192                                         using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2193                                                 Assert.IsNotNull (createdKey2, "#B3");
2194                                                 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
2195                                                 createdKey2.DeleteValue ("name2");
2196                                                 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
2197                                         }
2198                                         Assert.AreEqual (2, createdKey.ValueCount, "#B6");
2199                                 }
2200                         } finally {
2201                                 try {
2202                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2203                                         if (createdKey != null) {
2204                                                 createdKey.Close ();
2205                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2206                                         }
2207                                 } catch {
2208                                 }
2209                         }
2210                 }
2211
2212                 [Test]
2213                 public void ValueCount_Key_Removed ()
2214                 {
2215                         string subKeyName = Guid.NewGuid ().ToString ();
2216
2217                         try {
2218                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2219                                         // check if key was successfully created
2220                                         Assert.IsNotNull (createdKey, "#A1");
2221                                         Assert.AreEqual (0, createdKey.ValueCount, "#A2");
2222                                         createdKey.SetValue ("name1", "value1");
2223                                         Assert.AreEqual (1, createdKey.ValueCount, "#A3");
2224                                         createdKey.SetValue ("name2", "value2");
2225                                         Assert.AreEqual (2, createdKey.ValueCount, "#A4");
2226                                         createdKey.SetValue ("name2", "value2b");
2227                                         Assert.AreEqual (2, createdKey.ValueCount, "#A5");
2228                                         createdKey.SetValue ("name3", "value3");
2229                                         Assert.AreEqual (3, createdKey.ValueCount, "#A6");
2230                                 }
2231                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2232                                         Assert.IsNotNull (createdKey, "#B1");
2233                                         Assert.AreEqual (3, createdKey.ValueCount, "#B2");
2234
2235                                         // remove created key
2236                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2237
2238                                         try {
2239                                                 Assert.Fail ("#C1: " + createdKey.ValueCount);
2240                                         } catch (IOException ex) {
2241                                                 // Illegal operation attempted on a registry key that
2242                                                 // has been marked for deletion
2243                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
2244                                                 Assert.IsNotNull (ex.Message, "#15");
2245                                                 Assert.IsNull (ex.InnerException, "#16");
2246                                         }
2247                                 }
2248                         } finally {
2249                                 try {
2250                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2251                                         if (createdKey != null) {
2252                                                 createdKey.Close ();
2253                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2254                                         }
2255                                 } catch {
2256                                 }
2257                         }
2258                 }
2259
2260                 [Test]
2261                 public void bug79051 ()
2262                 {
2263                         string subKeyName = Guid.NewGuid ().ToString ();
2264
2265                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2266                                 try {
2267                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2268                                                 createdKey.SetValue ("test", "whatever");
2269                                                 createdKey.Close ();
2270                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2271                                         }
2272                                 } finally {
2273                                         try {
2274                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2275                                                 if (createdKey != null) {
2276                                                         createdKey.Close ();
2277                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2278                                                 }
2279                                         } catch {
2280                                         }
2281                                 }
2282                         }
2283                 }
2284
2285                 [Test]
2286                 public void bug79059 ()
2287                 {
2288                         string subKeyName = Guid.NewGuid ().ToString ();
2289
2290                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2291                                 try {
2292                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2293                                                 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
2294                                                 }
2295                                                 createdKey.Close ();
2296                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2297                                         }
2298                                 } finally {
2299                                         try {
2300                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2301                                                 if (createdKey != null) {
2302                                                         createdKey.Close ();
2303                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2304                                                 }
2305                                         } catch {
2306                                         }
2307                                 }
2308                         }
2309                 }
2310
2311                 [Test]
2312                 public void bugnew1 ()
2313                 {
2314                         string subKeyName = Guid.NewGuid ().ToString ();
2315
2316                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2317                                 try {
2318                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2319                                                 createdKey.SetValue ("name1", "value1");
2320
2321                                                 RegistryKey testKey = null;
2322                                                 try {
2323                                                         testKey = createdKey.OpenSubKey ("test", true);
2324                                                         if (testKey == null)
2325                                                                 testKey = createdKey.CreateSubKey ("test");
2326                                                         testKey.SetValue ("another", "one");
2327                                                 } finally {
2328                                                         if (testKey != null)
2329                                                                 testKey.Close ();
2330                                                 }
2331
2332                                                 createdKey.SetValue ("name2", "value2");
2333                                                 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
2334                                                 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
2335                                                 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
2336                                                 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
2337
2338                                                 string [] names = createdKey.GetValueNames ();
2339                                                 Assert.IsNotNull (names, "#6");
2340                                                 Assert.AreEqual (2, names.Length, "#7");
2341                                                 Assert.AreEqual ("name1", names [0], "#8");
2342                                                 Assert.AreEqual ("name2", names [1], "#9");
2343
2344                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2345
2346                                                 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
2347                                                         Assert.IsNull (openedKey, "#10");
2348                                                 }
2349
2350                                                 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
2351                                                 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
2352
2353                                                 try {
2354                                                         createdKey.GetValueNames ();
2355                                                         Assert.Fail ("#13");
2356                                                 } catch (IOException ex) {
2357                                                         // Illegal operation attempted on a registry key that
2358                                                         // has been marked for deletion
2359                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
2360                                                         Assert.IsNotNull (ex.Message, "#15");
2361                                                         Assert.IsNull (ex.InnerException, "#16");
2362                                                 }
2363
2364                                                 try {
2365                                                         createdKey.SetValue ("name1", "value1");
2366                                                         Assert.Fail ("#17");
2367                                                 } catch (IOException ex) {
2368                                                         // Illegal operation attempted on a registry key that
2369                                                         // has been marked for deletion
2370                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
2371                                                         Assert.IsNotNull (ex.Message, "#19");
2372                                                         Assert.IsNull (ex.InnerException, "#20");
2373                                                 }
2374
2375                                                 try {
2376                                                         createdKey.SetValue ("newname", "value1");
2377                                                         Assert.Fail ("#21");
2378                                                 } catch (IOException ex) {
2379                                                         // Illegal operation attempted on a registry key that
2380                                                         // has been marked for deletion
2381                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
2382                                                         Assert.IsNotNull (ex.Message, "#23");
2383                                                         Assert.IsNull (ex.InnerException, "#24");
2384                                                 }
2385
2386                                                 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
2387                                                 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
2388                                                 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
2389                                                 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
2390
2391                                                 try {
2392                                                         createdKey.CreateSubKey ("new");
2393                                                         Assert.Fail ("#29");
2394                                                 } catch (IOException ex) {
2395                                                         // Illegal operation attempted on a registry key that
2396                                                         // has been marked for deletion
2397                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
2398                                                         Assert.IsNotNull (ex.Message, "#31");
2399                                                         Assert.IsNull (ex.InnerException, "#32");
2400                                                 }
2401                                         }
2402                                 } finally {
2403                                         try {
2404                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2405                                                 if (createdKey != null) {
2406                                                         createdKey.Close ();
2407                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2408                                                 }
2409                                         } catch {
2410                                         }
2411                                 }
2412                         }
2413                 }
2414
2415                 [Test]
2416                 public void bugnew2 () // values cannot be written on registry root (hive)
2417                 {
2418                         string [] names = Registry.CurrentUser.GetValueNames ();
2419                         Assert.IsNotNull (names, "#1");
2420                         Registry.CurrentUser.SetValue ("name1", "value1");
2421                         Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
2422                         Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
2423                         string [] newNames = Registry.CurrentUser.GetValueNames ();
2424                         Assert.IsNotNull (newNames, "#4");
2425                         Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
2426                         Registry.CurrentUser.DeleteValue ("name1");
2427                 }
2428
2429                 [Test]
2430                 public void bugnew3 () // on Windows, key cannot be closed twice
2431                 {
2432                         string subKeyName = Guid.NewGuid ().ToString ();
2433
2434                         try {
2435                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2436                                         createdKey.Close ();
2437                                 }
2438
2439                                 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2440                                 openedKey.Close ();
2441                                 openedKey.Close ();
2442                         } finally {
2443                                 try {
2444                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2445                                         if (createdKey != null) {
2446                                                 createdKey.Close ();
2447                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2448                                         }
2449                                 } catch {
2450                                 }
2451                         }
2452                 }
2453
2454                 [Test]
2455                 public void bugnew4 () // Key cannot be flushed once it has been closed
2456                 {
2457                         string subKeyName = Guid.NewGuid ().ToString ();
2458
2459                         try {
2460                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2461                                         createdKey.Close ();
2462                                 }
2463
2464                                 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2465                                 openedKey.Close ();
2466                                 openedKey.Flush ();
2467                         } finally {
2468                                 try {
2469                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2470                                         if (createdKey != null) {
2471                                                 createdKey.Close ();
2472                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2473                                         }
2474                                 } catch {
2475                                 }
2476                         }
2477                 }
2478
2479                 private bool RunningOnUnix {
2480                         get {
2481 #if NET_2_0
2482                                 return Environment.OSVersion.Platform == PlatformID.Unix;
2483 #else
2484                                 int p = (int) Environment.OSVersion.Platform;
2485                                 return ((p == 4) || (p == 128));
2486 #endif
2487                         }
2488                 }
2489         }
2490 }