New test for the 4.0 DeleteSubKeyTree overload.
[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
27                 [Test]
28                 [Category ("NotWorking")] // 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                 [Test] // bug #77212
41                 public void TestHandle ()
42                 {
43                         // this test is for Windows only
44                         if (RunningOnUnix)
45                                 return;
46
47                         // this regpath always exists under windows
48                         RegistryKey k = Registry.CurrentUser
49                                 .OpenSubKey ("Software", false)
50                                 .OpenSubKey ("Microsoft", false)
51                                 .OpenSubKey ("Windows", false);
52                         
53                         Assert.IsNotNull (k, "#01");
54                 }
55
56                 [Test]
57                 public void OpenSubKey ()
58                 {
59                         RegistryKey key = Registry.LocalMachine;
60
61                         // HKEY_LOCAL_MACHINE\software should always exist on Windows
62                         // and is automatically created on Linux
63                         Assert.IsNotNull (key.OpenSubKey ("Software"), "#A1");
64                         Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#A2");
65
66                         key = Registry.CurrentUser;
67
68                         // HKEY_CURRENT_USER\software should always exist on Windows
69                         // and is automatically created on Linux
70                         Assert.IsNotNull (key.OpenSubKey ("Software"), "#B1");
71                         Assert.IsNotNull (key.OpenSubKey ("soFtware"), "#B2");
72
73                         key = Registry.Users;
74
75                         // HKEY_USERS\software should not exist on Windows, and should not
76                         // be created automatically on Linux
77                         Assert.IsNull (key.OpenSubKey ("Software"), "#C1");
78                         Assert.IsNull (key.OpenSubKey ("soFtware"), "#C2");
79                 }
80
81                 [Test]
82                 public void OpenSubKey_Key_DoesNotExist ()
83                 {
84                         string subKeyName = Guid.NewGuid ().ToString ();
85                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#1"); // read-only
86                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#2"); // writable
87                 }
88
89                 [Test]
90                 public void OpenSubKey_Key_Removed ()
91                 {
92                         string subKeyName = Guid.NewGuid ().ToString ();
93
94                         try {
95                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
96                                         // check if key was successfully created
97                                         Assert.IsNotNull (createdKey, "#1");
98                                         RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
99                                         subKey.Close ();
100                                 }
101                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
102                                         Assert.IsNotNull (createdKey, "#2");
103                                         using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
104                                                 Assert.IsNotNull (createdKey, "#3");
105                                         }
106                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
107
108                                         // read-only
109                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
110                                         Assert.IsNull (createdKey.OpenSubKey ("monotemp"), "#5"); // read-only
111                                         // writable
112                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName, true), "#6");
113                                         Assert.IsNull (createdKey.OpenSubKey ("monotemp", true), "#7"); 
114                                 }
115                         } finally {
116                                 try {
117                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
118                                         if (createdKey != null) {
119                                                 createdKey.Close ();
120                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
121                                         }
122                                 } catch {
123                                 }
124                         }
125                 }
126
127                 [Test]
128                 [Category ("NotWorking")] // MS should not allow this
129                 public void OpenSubKey_Name_Empty ()
130                 {
131                         // read-only
132                         using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty)) {
133                                 Assert.IsNotNull (emptyKey, "#1");
134                         }
135                         // writable
136                         using (RegistryKey emptyKey = Registry.CurrentUser.OpenSubKey (string.Empty, true)) {
137                                 Assert.IsNotNull (emptyKey, "#1");
138                         }
139                 }
140
141                 [Test]
142                 public void OpenSubKey_Name_MaxLength ()
143                 {
144                         string name = new string ('a', 254);
145
146                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (name), "#A1");
147
148                         name = new string ('a', 255);
149
150                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (name), "#B1");
151
152                         name = new string ('a', 256);
153
154                         try {
155                                 Registry.CurrentUser.OpenSubKey (name);
156                                 Assert.Fail ("#C1");
157                         } catch (ArgumentException ex) {
158                                 // 1.x: Registry subkeys should not be
159                                 // greater than or equal to 255 characters
160                                 //
161                                 // 2.x: Registry subkeys should not be
162                                 // greater than 255 characters
163                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
164                                 Assert.IsNull (ex.InnerException, "#C3");
165                                 Assert.IsNotNull (ex.Message, "#c4");
166                                 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
167                                 Assert.IsNull (ex.ParamName, "#C6");
168                         }
169                 }
170
171                 [Test]
172                 public void OpenSubKey_Name_Null ()
173                 {
174                         try {
175                                 Registry.CurrentUser.OpenSubKey (null);
176                                 Assert.Fail ("#A1");
177                         } catch (ArgumentNullException ex) {
178                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
179                                 Assert.IsNull (ex.InnerException, "#A3");
180                                 Assert.IsNotNull (ex.Message, "#A4");
181                                 Assert.AreEqual ("name", ex.ParamName, "#A5");
182                         }
183
184                         try {
185                                 Registry.CurrentUser.OpenSubKey (null, true);
186                                 Assert.Fail ("#B1");
187                         } catch (ArgumentNullException ex) {
188                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
189                                 Assert.IsNull (ex.InnerException, "#B3");
190                                 Assert.IsNotNull (ex.Message, "#B4");
191                                 Assert.AreEqual ("name", ex.ParamName, "#B5");
192                         }
193                 }
194
195                 [Test]
196                 public void Close_Local_Hive ()
197                 {
198                         RegistryKey hive = Registry.CurrentUser;
199                         hive.Close ();
200
201                         Assert.IsNotNull (hive.GetSubKeyNames (), "#1");
202                         Assert.IsNull (hive.GetValue ("doesnotexist"), "#2");
203                         Assert.IsNotNull (hive.GetValueNames (), "#3");
204                         Assert.IsNull (hive.OpenSubKey ("doesnotexist"), "#4");
205                         Assert.IsNotNull (hive.SubKeyCount, "#5");
206                         Assert.IsNotNull (hive.ToString (), "#6");
207
208                         // closing key again does not have any effect
209                         hive.Close ();
210                 }
211
212                 [Test]
213                 public void Close_Local_Key ()
214                 {
215                         RegistryKey key = Registry.CurrentUser.OpenSubKey ("SOFTWARE");
216                         key.Close ();
217
218                         // closing a key twice does not have any effect
219                         key.Close ();
220
221                         try {
222                                 key.CreateSubKey ("a");
223                                 Assert.Fail ("#1");
224                         } catch (ObjectDisposedException) {
225                         }
226
227                         try {
228                                 key.DeleteSubKey ("doesnotexist");
229                                 Assert.Fail ("#2");
230                         } catch (ObjectDisposedException) {
231                         }
232
233                         try {
234                                 key.DeleteSubKeyTree ("doesnotexist");
235                                 Assert.Fail ("#3");
236                         } catch (ObjectDisposedException) {
237                         }
238
239                         try {
240                                 key.DeleteValue ("doesnotexist");
241                                 Assert.Fail ("#4");
242                         } catch (ObjectDisposedException) {
243                         }
244
245                         // flushing a closed key does not have any effect
246                         key.Flush ();
247
248                         try {
249                                 key.GetSubKeyNames ();
250                                 Assert.Fail ("#5");
251                         } catch (ObjectDisposedException) {
252                         }
253
254                         try {
255                                 key.GetValue ("doesnotexist");
256                                 Assert.Fail ("#6");
257                         } catch (ObjectDisposedException) {
258                         }
259
260                         try {
261                                 key.GetValueNames ();
262                                 Assert.Fail ("#7");
263                         } catch (ObjectDisposedException) {
264                         }
265
266                         try {
267                                 key.OpenSubKey ("doesnotexist");
268                                 Assert.Fail ("#8");
269                         } catch (ObjectDisposedException) {
270                         }
271
272                         try {
273                                 key.SetValue ("doesnotexist", "something");
274                                 Assert.Fail ("#9");
275                         } catch (ObjectDisposedException) {
276                         }
277
278                         try {
279                                 int x = key.SubKeyCount;
280                                 Assert.Fail ("#10:" + x);
281                         } catch (ObjectDisposedException) {
282                         }
283
284                         try {
285                                 key.ToString ();
286                                 Assert.Fail ("#11");
287                         } catch (ObjectDisposedException) {
288                         }
289
290                         try {
291                                 int x = key.ValueCount;
292                                 Assert.Fail ("#12:" + x);
293                         } catch (ObjectDisposedException) {
294                         }
295                 }
296
297                 [Test]
298                 public void Close_Remote_Hive ()
299                 {
300                         // access to registry of remote machines is not implemented on unix
301                         if (RunningOnUnix)
302                                 return;
303
304                         RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
305                                 RegistryHive.CurrentUser, Environment.MachineName);
306                         hive.Close ();
307
308                         // closing a remote hive twice does not have any effect
309                         hive.Close ();
310
311                         try {
312                                 hive.CreateSubKey ("a");
313                                 Assert.Fail ("#1");
314                         } catch (ObjectDisposedException) {
315                         }
316
317                         try {
318                                 hive.DeleteSubKey ("doesnotexist");
319                                 Assert.Fail ("#2");
320                         } catch (ObjectDisposedException) {
321                         }
322
323                         try {
324                                 hive.DeleteSubKeyTree ("doesnotexist");
325                                 Assert.Fail ("#3");
326                         } catch (ObjectDisposedException) {
327                         }
328
329                         try {
330                                 hive.DeleteValue ("doesnotexist");
331                                 Assert.Fail ("#4");
332                         } catch (ObjectDisposedException) {
333                         }
334
335                         // flushing a closed hive does not have any effect
336                         hive.Flush ();
337
338                         try {
339                                 hive.GetSubKeyNames ();
340                                 Assert.Fail ("#5");
341                         } catch (ObjectDisposedException) {
342                         }
343
344                         try {
345                                 hive.GetValue ("doesnotexist");
346                                 Assert.Fail ("#6");
347                         } catch (ObjectDisposedException) {
348                         }
349
350                         try {
351                                 hive.GetValueNames ();
352                                 Assert.Fail ("#7");
353                         } catch (ObjectDisposedException) {
354                         }
355
356                         try {
357                                 hive.OpenSubKey ("doesnotexist");
358                                 Assert.Fail ("#8");
359                         } catch (ObjectDisposedException) {
360                         }
361
362                         try {
363                                 hive.SetValue ("doesnotexist", "something");
364                                 Assert.Fail ("#9");
365                         } catch (ObjectDisposedException) {
366                         }
367
368                         try {
369                                 int x = hive.SubKeyCount;
370                                 Assert.Fail ("#10:" + x);
371                         } catch (ObjectDisposedException) {
372                         }
373
374                         try {
375                                 hive.ToString ();
376                                 Assert.Fail ("#11");
377                         } catch (ObjectDisposedException) {
378                         }
379
380                         try {
381                                 int x = hive.ValueCount;
382                                 Assert.Fail ("#12:" + x);
383                         } catch (ObjectDisposedException) {
384                         }
385                 }
386
387                 [Test]
388                 public void Close_Remote_Key ()
389                 {
390                         // access to registry of remote machines is not implemented on unix
391                         if (RunningOnUnix)
392                                 return;
393
394                         RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
395                                 RegistryHive.CurrentUser, Environment.MachineName);
396                         RegistryKey key = hive.OpenSubKey ("SOFTWARE");
397                         key.Close ();
398
399                         // closing a remote key twice does not have any effect
400                         key.Close ();
401
402                         try {
403                                 key.CreateSubKey ("a");
404                                 Assert.Fail ("#1");
405                         } catch (ObjectDisposedException) {
406                         }
407
408                         try {
409                                 key.DeleteSubKey ("doesnotexist");
410                                 Assert.Fail ("#2");
411                         } catch (ObjectDisposedException) {
412                         }
413
414                         try {
415                                 key.DeleteSubKeyTree ("doesnotexist");
416                                 Assert.Fail ("#3");
417                         } catch (ObjectDisposedException) {
418                         }
419
420                         try {
421                                 key.DeleteValue ("doesnotexist");
422                                 Assert.Fail ("#4");
423                         } catch (ObjectDisposedException) {
424                         }
425
426                         // flushing a closed key does not have any effect
427                         key.Flush ();
428
429                         try {
430                                 key.GetSubKeyNames ();
431                                 Assert.Fail ("#5");
432                         } catch (ObjectDisposedException) {
433                         }
434
435                         try {
436                                 key.GetValue ("doesnotexist");
437                                 Assert.Fail ("#6");
438                         } catch (ObjectDisposedException) {
439                         }
440
441                         try {
442                                 key.GetValueNames ();
443                                 Assert.Fail ("#7");
444                         } catch (ObjectDisposedException) {
445                         }
446
447                         try {
448                                 key.OpenSubKey ("doesnotexist");
449                                 Assert.Fail ("#8");
450                         } catch (ObjectDisposedException) {
451                         }
452
453                         try {
454                                 key.SetValue ("doesnotexist", "something");
455                                 Assert.Fail ("#9");
456                         } catch (ObjectDisposedException) {
457                         }
458
459                         try {
460                                 int x = key.SubKeyCount;
461                                 Assert.Fail ("#10:" + x);
462                         } catch (ObjectDisposedException) {
463                         }
464
465                         try {
466                                 key.ToString ();
467                                 Assert.Fail ("#11");
468                         } catch (ObjectDisposedException) {
469                         }
470
471                         try {
472                                 int x = key.ValueCount;
473                                 Assert.Fail ("#12:" + x);
474                         } catch (ObjectDisposedException) {
475                         }
476
477                         hive.Close ();
478                 }
479
480                 [Test]
481                 public void CreateSubKey ()
482                 {
483                         string subKeyName = Guid.NewGuid ().ToString ();
484
485                         try {
486                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
487                                         // check if key was successfully created
488                                         Assert.IsNotNull (createdKey, "#A1");
489                                         // software subkey should not be created automatically
490                                         Assert.IsNull (createdKey.OpenSubKey ("software"), "#A2");
491                                 }
492
493                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
494                                         // check if key was successfully created
495                                         Assert.IsNotNull (createdKey, "#B1");
496                                         // software subkey should not be created automatically
497                                         Assert.IsNull (createdKey.OpenSubKey ("software"), "#B2");
498                                 }
499                         } finally {
500                                 // clean-up
501                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
502                         }
503
504                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
505                                 try {
506                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
507                                                 // check if key was successfully created
508                                                 Assert.IsNotNull (createdKey, "#C1");
509                                                 // software subkey should not be created automatically
510                                                 Assert.IsNull (softwareKey.OpenSubKey ("software"), "#C2");
511                                         }
512
513                                         using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
514                                                 // check if key was successfully created
515                                                 Assert.IsNotNull (createdKey, "#D1");
516                                                 // software subkey should not be created automatically
517                                                 Assert.IsNull (softwareKey.OpenSubKey ("software"), "#D2");
518                                         }
519                                 } finally {
520                                         // clean-up
521                                         softwareKey.DeleteSubKeyTree (subKeyName);
522                                 }
523                         }
524                 }
525
526                 [Test]
527                 public void CreateSubKey_Key_ReadOnly ()
528                 {
529                         string subKeyName = Guid.NewGuid ().ToString ();
530
531                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
532                                 RegistryKey createdKey = null;
533                                 try {
534                                         try {
535                                                 createdKey = softwareKey.CreateSubKey (subKeyName);
536                                                 Assert.Fail ("#1");
537                                         } catch (UnauthorizedAccessException ex) {
538                                                 // Cannot write to the registry key
539                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
540                                                 Assert.IsNotNull (ex.Message, "#3");
541                                                 Assert.IsNull (ex.InnerException, "#4");
542                                         }
543                                 } finally {
544                                         if (createdKey != null)
545                                                 createdKey.Close ();
546                                 }
547                         }
548                 }
549
550                 [Test]
551                 public void CreateSubKey_Key_Removed ()
552                 {
553                         string subKeyName = Guid.NewGuid ().ToString ();
554
555                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
556                                 try {
557                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
558                                                 softwareKey.DeleteSubKeyTree (subKeyName);
559                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
560                                                 try {
561                                                         createdKey.CreateSubKey ("test");
562                                                         Assert.Fail ("#2");
563                                                 } catch (IOException ex) {
564                                                         // Illegal operation attempted on a registry key that
565                                                         // has been marked for deletion
566                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
567                                                         Assert.IsNotNull (ex.Message, "#4");
568                                                         Assert.IsNull (ex.InnerException, "#5");
569                                                 }
570                                         }
571                                 } finally {
572                                         try {
573                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
574                                                 if (createdKey != null) {
575                                                         createdKey.Close ();
576                                                         softwareKey.DeleteSubKeyTree (subKeyName);
577                                                 }
578                                         } catch {
579                                         }
580                                 }
581                         }
582                 }
583
584                 [Test]
585                 [Category ("NotWorking")] // MS should not allow this
586                 public void CreateSubKey_Name_Empty ()
587                 {
588                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
589                                 using (RegistryKey emptyKey = softwareKey.CreateSubKey (string.Empty)) {
590                                         Assert.IsNotNull (emptyKey, "#1");
591                                         emptyKey.SetValue ("name1", "value1");
592                                 }
593                         }
594                 }
595
596                 [Test]
597                 public void CreateSubKey_Name_MaxLength ()
598                 {
599                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
600                                 string subKeyName = new string ('a', 254);
601
602                                 try {
603                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
604                                                 Assert.IsNotNull (createdKey, "#A1");
605                                                 Assert.IsNotNull (softwareKey.OpenSubKey (subKeyName), "#A2");
606                                         }
607                                 } finally {
608                                         softwareKey.DeleteSubKeyTree (subKeyName);
609                                 }
610
611                                 subKeyName = new string ('a', 255);
612
613                                 try {
614                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
615                                                 Assert.IsNotNull (createdKey, "#B1");
616                                                 Assert.IsNotNull (softwareKey.OpenSubKey (subKeyName), "#B2");
617                                         }
618                                 } finally {
619                                         softwareKey.DeleteSubKey (subKeyName);
620                                 }
621
622                                 subKeyName = new string ('a', 256);
623
624                                 try {
625                                         softwareKey.CreateSubKey (subKeyName);
626                                         Assert.Fail ("#C1");
627                                 } catch (ArgumentException ex) {
628                                         // 1.x: Registry subkeys should not be
629                                         // greater than or equal to 255 characters
630                                         //
631                                         // 2.x: Registry subkeys should not be
632                                         // greater than 255 characters
633                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
634                                         Assert.IsNull (ex.InnerException, "#C3");
635                                         Assert.IsNotNull (ex.Message, "#C4");
636                                         Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
637                                         Assert.IsNull (ex.ParamName, "#C6");
638                                 }
639                         }
640                 }
641
642                 [Test]
643                 public void CreateSubKey_Name_Null ()
644                 {
645                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
646                                 try {
647                                         softwareKey.CreateSubKey (null);
648                                         Assert.Fail ("#1");
649                                 } catch (ArgumentNullException ex) {
650                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
651                                         Assert.IsNull (ex.InnerException, "#3");
652                                         Assert.IsNotNull (ex.Message, "#4");
653                                         Assert.AreEqual ("name", ex.ParamName, "#5");
654                                 }
655                         }
656                 }
657
658 #if NET_4_0
659                 // Unfortunately we can't test that the scenario where a volatile
660                 // key is not alive after a reboot, but we can test other bits.
661                 [Test]
662                 public void CreateSubKey_Volatile ()
663                 {
664                         RegistryKey key = null;
665                         RegistryKey subkey = null;
666                         string subKeyName = "VolatileKey";
667
668                         try {
669                                 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
670                                 subkey = key.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
671                                 key.Close ();
672
673                                 key = Registry.CurrentUser.OpenSubKey (subKeyName);
674                                 subkey = key.OpenSubKey ("Child");
675                                 Assert.AreEqual (true, subkey != null, "#A1");
676                         } finally {
677                                 if (subkey != null)
678                                         subkey.Close ();
679                                 if (key != null)
680                                         key.Close ();
681                         }
682                 }
683
684                 [Test]
685                 public void CreateSubKey_Volatile_Child ()
686                 {
687                         RegistryKey key = null;
688                         RegistryKey subkey = null;
689                         string subKeyName = "VolatileKey";
690
691                         try {
692                                 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
693                                 subkey = key.CreateSubKey ("Child"); // Non volatile child
694                                 Assert.Fail ("#Exc");
695                         } catch (IOException) {
696                         } finally {
697                                 if (subkey != null)
698                                         subkey.Close ();
699                                 if (key != null)
700                                         key.Close ();
701                         }
702                 }
703
704                 [Test]
705                 public void CreateSubKey_Volatile_Conflict ()
706                 {
707                         RegistryKey key = null;
708                         RegistryKey key2 = null;
709                         RegistryKey subkey = null;
710                         string subKeyName = "VolatileKey";
711
712                         try {
713                                 // 
714                                 // Create a volatile key and try to open it as a normal one
715                                 //
716                                 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
717                                 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
718                                 Assert.AreEqual (key.Name, key2.Name, "A0");
719
720                                 subkey = key2.CreateSubKey ("Child", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
721                                 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#A1");
722                                 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#A2");
723
724                                 subkey.Close ();
725                                 key.Close ();
726                                 key2.Close ();
727
728                                 // 
729                                 // Create a non-volatile key and try to open it as a volatile one
730                                 //
731                                 subKeyName = "NonVolatileKey";
732                                 key2 = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
733                                 key2.SetValue ("Name", "Mono");
734                                 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
735                                 Assert.AreEqual (key.Name, key2.Name, "B0");
736                                 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#B1");
737                                 Assert.AreEqual ("Mono", key2.GetValue ("Name"), "#B2");
738
739                                 key.CreateSubKey ("Child");
740                                 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#B3");
741                                 Assert.AreEqual (true, key2.OpenSubKey ("Child") != null, "#B4");
742
743                                 // 
744                                 // Close the non-volatile key and try to re-open it as a volatile one
745                                 //
746                                 key.Close ();
747                                 key2.Close ();
748                                 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
749                                 Assert.AreEqual ("Mono", key.GetValue ("Name"), "#C0");
750                                 Assert.AreEqual (true, key.OpenSubKey ("Child") != null, "#C1");
751                         } finally {
752                                 if (subkey != null)
753                                         subkey.Close ();
754                                 if (key != null)
755                                         key.Close ();
756                                 if (key2 != null)
757                                         key2.Close ();
758                         }
759                 }
760
761                 [Test]
762                 public void DeleteSubKey_Volatile ()
763                 {                       
764                         RegistryKey key = null;
765                         RegistryKey subkey = null;
766                         string subKeyName = "VolatileKey";
767
768                         try {
769                                 key = Registry.CurrentUser.CreateSubKey (subKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
770                                 key.CreateSubKey ("VolatileKeyChild", RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
771                                 key.SetValue ("Name", "Mono");
772                                 key.Close ();
773
774                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
775
776                                 key = Registry.CurrentUser.OpenSubKey (subKeyName);
777                                 Assert.AreEqual (null, key, "#A0");
778                         } finally {
779                                 if (subkey != null)
780                                         subkey.Close ();
781                                 if (key != null)
782                                         key.Close ();
783                         }
784                 }
785
786                 // Define a normal key, and create a normal and a volatile key under it, and retrieve their names.
787                 [Test]
788                 public void GetSubKeyNames_Volatile ()
789                 {           
790                         RegistryKey key = null;
791                         RegistryKey subkey = null;
792                         string subKeyName = Guid.NewGuid ().ToString ();
793                         string volChildKeyName = "volatilechildkey";
794                         string childKeyName = "childkey";
795
796                         try {
797                                 key = Registry.CurrentUser.CreateSubKey (subKeyName);
798                                 key.CreateSubKey (volChildKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.Volatile);
799                                 key.CreateSubKey (childKeyName, RegistryKeyPermissionCheck.Default, RegistryOptions.None);
800                                 key.Close ();
801
802                                 key = Registry.CurrentUser.OpenSubKey (subKeyName);
803                                 string [] keyNames = key.GetSubKeyNames ();
804
805                                 // we can guarantee the order of the child keys, so we sort the two of them
806                                 Array.Sort (keyNames);
807
808                                 Assert.AreEqual (2, keyNames.Length, "#A0");
809                                 Assert.AreEqual (childKeyName, keyNames [0], "#A1");
810                                 Assert.AreEqual (volChildKeyName, keyNames [1], "#A2");
811                         } finally {
812                                 if (subkey != null)
813                                         subkey.Close ();
814                                 if (key != null)
815                                         key.Close ();
816                         }
817
818                 }
819 #endif
820
821                 [Test]
822                 public void DeleteSubKey ()
823                 {
824                         string subKeyName = Guid.NewGuid ().ToString ();
825
826                         try {
827                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
828                                         // check if key was successfully created
829                                         Assert.IsNotNull (createdKey, "#1");
830                                 }
831                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
832                                         Assert.IsNotNull (createdKey, "#2");
833                                         Registry.CurrentUser.DeleteSubKey (subKeyName);
834                                 }
835                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
836                                         Assert.IsNull (createdKey, "#3");
837                                 }
838                         } finally {
839                                 try {
840                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
841                                         if (createdKey != null) {
842                                                 createdKey.Close ();
843                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
844                                         }
845                                 } catch {
846                                 }
847                         }
848                 }
849
850                 [Test]
851                 public void DeleteSubKey_Key_HasChildKeys ()
852                 {
853                         string subKeyName = Guid.NewGuid ().ToString ();
854
855                         try {
856                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
857                                         // check if key was successfully created
858                                         Assert.IsNotNull (createdKey, "#1");
859                                         RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
860                                         subKey.Close ();
861                                 }
862                                 try {
863                                         Registry.CurrentUser.DeleteSubKey (subKeyName);
864                                         Assert.Fail ("#2");
865                                 } catch (InvalidOperationException ex) {
866                                         // Registry key has subkeys and recursive removes are not
867                                         // supported by this method
868                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
869                                         Assert.IsNotNull (ex.Message, "#4");
870                                         Assert.IsNull (ex.InnerException, "#5");
871                                 }
872                         } finally {
873                                 try {
874                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
875                                         if (createdKey != null) {
876                                                 createdKey.Close ();
877                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
878                                         }
879                                 } catch {
880                                 }
881                         }
882                 }
883
884                 [Test]
885                 public void DeleteSubKey_Key_ReadOnly ()
886                 {
887                         string subKeyName = Guid.NewGuid ().ToString ();
888
889                         try {
890                                 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
891                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
892                                         createdKey.Close ();
893                                 }
894
895                                 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
896                                         try {
897                                                 softwareKey.DeleteSubKey (subKeyName);
898                                                 Assert.Fail ("#1");
899                                         } catch (UnauthorizedAccessException ex) {
900                                                 // Cannot write to the registry key
901                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
902                                                 Assert.IsNotNull (ex.Message, "#3");
903                                                 Assert.IsNull (ex.InnerException, "#4");
904                                         }
905                                 }
906                         } finally {
907                                 try {
908                                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
909                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
910                                                 if (createdKey != null) {
911                                                         createdKey.Close ();
912                                                         softwareKey.DeleteSubKeyTree (subKeyName);
913                                                 }
914                                         }
915                                 } catch {
916                                 }
917                         }
918                 }
919
920                 [Test]
921                 public void DeleteSubKey_Key_DoesNotExist ()
922                 {
923                         string subKeyName = Guid.NewGuid ().ToString ();
924
925                         try {
926                                 Registry.CurrentUser.DeleteSubKey (subKeyName);
927                                 Assert.Fail ("#A1");
928                         } catch (ArgumentException ex) {
929                                 // Cannot delete a subkey tree because the subkey does not exist
930                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
931                                 Assert.IsNull (ex.InnerException, "#A3");
932                                 Assert.IsNotNull (ex.Message, "#A4");
933                                 Assert.IsNull (ex.ParamName, "#A5");
934                         }
935
936                         try {
937                                 Registry.CurrentUser.DeleteSubKey (subKeyName, true);
938                                 Assert.Fail ("#B1");
939                         } catch (ArgumentException ex) {
940                                 // Cannot delete a subkey tree because the subkey does not exist
941                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
942                                 Assert.IsNull (ex.InnerException, "#B3");
943                                 Assert.IsNotNull (ex.Message, "#B4");
944                                 Assert.IsNull (ex.ParamName, "#B5");
945                         }
946
947                         Registry.CurrentUser.DeleteSubKey (subKeyName, false);
948                 }
949
950                 [Test]
951                 public void DeleteSubKey_Key_Removed ()
952                 {
953                         string subKeyName = Guid.NewGuid ().ToString ();
954
955                         try {
956                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
957                                         // check if key was successfully created
958                                         Assert.IsNotNull (createdKey, "#1");
959                                         RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
960                                         subKey.Close ();
961                                 }
962                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
963                                         Assert.IsNotNull (createdKey, "#2");
964                                         using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
965                                                 Assert.IsNotNull (createdKey, "#3");
966                                         }
967                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
968                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
969                                         try {
970                                                 createdKey.DeleteSubKey ("monotemp");
971                                                 Assert.Fail ("#5");
972                                         } catch (ArgumentException ex) {
973                                                 // Cannot delete a subkey tree because the subkey does
974                                                 // not exist
975                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
976                                                 Assert.IsNull (ex.InnerException, "#7");
977                                                 Assert.IsNotNull (ex.Message, "#8");
978                                                 Assert.IsNull (ex.ParamName, "#9");
979                                         }
980                                 }
981                         } finally {
982                                 try {
983                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
984                                         if (createdKey != null) {
985                                                 createdKey.Close ();
986                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
987                                         }
988                                 } catch {
989                                 }
990                         }
991                 }
992
993                 [Test]
994                 [Category ("NotWorking")] // MS should not allow this
995                 public void DeleteSubKey_Name_Empty ()
996                 {
997                         string subKeyName = Guid.NewGuid ().ToString ();
998
999                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1000                                 try {
1001                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1002                                         createdKey.DeleteSubKey (string.Empty);
1003                                         createdKey.Close ();
1004
1005                                         createdKey = softwareKey.OpenSubKey (subKeyName);
1006                                         Assert.IsNull (createdKey, "#1");
1007                                 } finally {
1008                                         try {
1009                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1010                                                 if (createdKey != null)
1011                                                         createdKey.Close ();
1012                                                 softwareKey.DeleteSubKeyTree (subKeyName);
1013                                         } catch {
1014                                         }
1015                                 }
1016                         }
1017                 }
1018
1019                 [Test]
1020                 public void DeleteSubKey_Name_MaxLength ()
1021                 {
1022                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1023                                 string subKeyName = new string ('a', 254);
1024
1025                                 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1026                                         createdKey.Close ();
1027                                 }
1028                                 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1029                                         Assert.IsNotNull (createdKey, "#A1");
1030                                 }
1031                                 softwareKey.DeleteSubKey (subKeyName);
1032                                 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1033                                         Assert.IsNull (createdKey, "#A2");
1034                                 }
1035
1036                                 subKeyName = new string ('a', 256);
1037
1038                                 try {
1039                                         softwareKey.DeleteSubKey (subKeyName);
1040                                         Assert.Fail ("#B1");
1041                                 } catch (ArgumentException ex) {
1042                                         // 1.x: Registry subkeys should not be
1043                                         // greater than or equal to 255 characters
1044                                         //
1045                                         // 2.x: Registry subkeys should not be
1046                                         // greater than 255 characters
1047                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1048                                         Assert.IsNull (ex.InnerException, "#B3");
1049                                         Assert.IsNotNull (ex.Message, "#B4");
1050                                         Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1051                                         Assert.IsNull (ex.ParamName, "#B6");
1052                                 }
1053                         }
1054                 }
1055
1056                 [Test]
1057                 public void DeleteSubKey_Name_Null ()
1058                 {
1059                         string subKeyName = Guid.NewGuid ().ToString ();
1060
1061                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1062                                 try {
1063                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1064                                         try {
1065                                                 createdKey.DeleteSubKey (null);
1066                                                 Assert.Fail ("#1");
1067                                         } catch (ArgumentNullException ex) {
1068                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1069                                                 Assert.IsNull (ex.InnerException, "#3");
1070                                                 Assert.IsNotNull (ex.Message, "#4");
1071                                                 Assert.AreEqual ("name", ex.ParamName, "#5");
1072                                         }
1073                                 } finally {
1074                                         try {
1075                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1076                                                 if (createdKey != null)
1077                                                         createdKey.Close ();
1078                                                 softwareKey.DeleteSubKeyTree (subKeyName);
1079                                         } catch {
1080                                         }
1081                                 }
1082                         }
1083                 }
1084
1085                 [Test]
1086                 public void DeleteSubKeyTree ()
1087                 {
1088                         // TODO: 
1089                         // - remove key with subkeys
1090                         // - remove key of which some subkeys are marked for deletion
1091                         // - remove key with values
1092                 }
1093
1094                 [Test]
1095                 public void DeleteSubKeyTree_Key_DoesNotExist ()
1096                 {
1097                         // Cannot delete a subkey tree because the subkey does not exist
1098                         string subKeyName = Guid.NewGuid ().ToString ();
1099                         try {
1100                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1101                                 Assert.Fail ("#1");
1102                         } catch (ArgumentException ex) {
1103                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1104                                 Assert.IsNull (ex.InnerException, "#3");
1105                                 Assert.IsNotNull (ex.Message, "#4");
1106                                 Assert.IsNull (ex.ParamName, "#5");
1107                         }
1108                 }
1109
1110 #if NET_4_0
1111                 [Test]
1112                 public void DeleteSubKeyTree_Key_DoesNotExist_Overload ()
1113                 {
1114                         // Cannot delete a subkey tree because the subkey does not exist
1115                         string subKeyName = Guid.NewGuid ().ToString ();
1116                         try {
1117                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName, true);
1118                                 Assert.Fail ("#1");
1119                         } catch (ArgumentException ex) {
1120                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1121                                 Assert.IsNull (ex.InnerException, "#3");
1122                                 Assert.IsNotNull (ex.Message, "#4");
1123                                 Assert.IsNull (ex.ParamName, "#5");
1124                         }
1125
1126                         // It's enough to know this line is not throwing an exception.
1127                         Registry.CurrentUser.DeleteSubKey (subKeyName, false);
1128                 }
1129 #endif
1130
1131                 [Test]
1132                 public void DeleteSubKeyTree_Key_ReadOnly ()
1133                 {
1134                         string subKeyName = Guid.NewGuid ().ToString ();
1135
1136                         try {
1137                                 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1138                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1139                                         createdKey.Close ();
1140                                 }
1141
1142                                 using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
1143                                         try {
1144                                                 softwareKey.DeleteSubKeyTree (subKeyName);
1145                                                 Assert.Fail ("#1");
1146                                         } catch (UnauthorizedAccessException ex) {
1147                                                 // Cannot write to the registry key
1148                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1149                                                 Assert.IsNotNull (ex.Message, "#3");
1150                                                 Assert.IsNull (ex.InnerException, "#4");
1151                                         }
1152                                 }
1153                         } finally {
1154                                 try {
1155                                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1156                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1157                                                 if (createdKey != null)
1158                                                         createdKey.Close ();
1159                                                 softwareKey.DeleteSubKeyTree (subKeyName);
1160                                         }
1161                                 } catch {
1162                                 }
1163                         }
1164                 }
1165
1166                 [Test]
1167                 public void DeleteSubKeyTree_Key_Removed ()
1168                 {
1169                         string subKeyName = Guid.NewGuid ().ToString ();
1170
1171                         try {
1172                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1173                                         // check if key was successfully created
1174                                         Assert.IsNotNull (createdKey, "#1");
1175                                         RegistryKey subKey = createdKey.CreateSubKey ("monotemp");
1176                                         subKey.Close ();
1177                                 }
1178                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1179                                         Assert.IsNotNull (createdKey, "#2");
1180                                         using (RegistryKey subKey = createdKey.OpenSubKey ("monotemp")) {
1181                                                 Assert.IsNotNull (createdKey, "#3");
1182                                         }
1183                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1184                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#4");
1185                                         try {
1186                                                 createdKey.DeleteSubKeyTree ("monotemp");
1187                                                 Assert.Fail ("#5");
1188                                         } catch (ArgumentException ex) {
1189                                                 // Cannot delete a subkey tree because the subkey does
1190                                                 // not exist
1191                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#6");
1192                                                 Assert.IsNull (ex.InnerException, "#7");
1193                                                 Assert.IsNotNull (ex.Message, "#8");
1194                                                 Assert.IsNull (ex.ParamName, "#9");
1195                                         }
1196                                 }
1197                         } finally {
1198                                 try {
1199                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1200                                         if (createdKey != null) {
1201                                                 createdKey.Close ();
1202                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1203                                         }
1204                                 } catch {
1205                                 }
1206                         }
1207                 }
1208
1209                 [Test]
1210                 [Category ("NotWorking")] // MS should not allow this
1211                 public void DeleteSubKeyTree_Name_Empty ()
1212                 {
1213                         string subKeyName = Guid.NewGuid ().ToString ();
1214
1215                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1216                                 try {
1217                                         RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName);
1218                                         createdKey.DeleteSubKeyTree (string.Empty);
1219                                         createdKey.Close ();
1220
1221                                         createdKey = softwareKey.OpenSubKey (subKeyName);
1222                                         Assert.IsNull (createdKey, "#1");
1223                                 } finally {
1224                                         try {
1225                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
1226                                                 if (createdKey != null)
1227                                                         createdKey.Close ();
1228                                                 softwareKey.DeleteSubKeyTree (subKeyName);
1229                                         } catch {
1230                                         }
1231                                 }
1232                         }
1233                 }
1234
1235                 [Test]
1236                 public void DeleteSubKeyTree_Name_MaxLength ()
1237                 {
1238                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1239                                 string subKeyName = new string ('a', 254);
1240
1241                                 using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
1242                                         createdKey.Close ();
1243                                 }
1244                                 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1245                                         Assert.IsNotNull (createdKey, "#A1");
1246                                 }
1247                                 softwareKey.DeleteSubKeyTree (subKeyName);
1248                                 using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName, false)) {
1249                                         Assert.IsNull (createdKey, "#A2");
1250                                 }
1251
1252 #if ONLY_1_1
1253                                 subKeyName = new string ('a', 255);
1254 #else
1255                                 subKeyName = new string ('a', 256);
1256 #endif
1257
1258                                 try {
1259                                         softwareKey.DeleteSubKeyTree (subKeyName);
1260                                         Assert.Fail ("#B1");
1261                                 } catch (ArgumentException ex) {
1262                                         // 1.x: Registry subkeys should not be
1263                                         // greater than or equal to 255 characters
1264                                         //
1265                                         // 2.x: Registry subkeys should not be
1266                                         // greater than 255 characters
1267                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1268                                         Assert.IsNull (ex.InnerException, "#B3");
1269                                         Assert.IsNotNull (ex.Message, "#B4");
1270                                         Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1271                                         Assert.IsNull (ex.ParamName, "#B6");
1272                                 }
1273                         }
1274                 }
1275
1276                 [Test]
1277                 public void DeleteSubKeyTree_Name_Null ()
1278                 {
1279                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1280                                 try {
1281                                         softwareKey.DeleteSubKeyTree (null);
1282                                         Assert.Fail ("#1");
1283                                 } catch (ArgumentNullException ex) {
1284                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1285                                         Assert.IsNull (ex.InnerException, "#3");
1286                                         Assert.IsNotNull (ex.Message, "#4");
1287                                         Assert.AreEqual ("name", ex.ParamName, "#5");
1288                                 }
1289                         }
1290                 }
1291
1292                 [Test]
1293                 public void DeleteValue ()
1294                 {
1295                         string subKeyName = Guid.NewGuid ().ToString ();
1296
1297                         try {
1298                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1299                                         // check if key was successfully created
1300                                         Assert.IsNotNull (createdKey, "#A1");
1301                                         createdKey.SetValue ("name1", "value1");
1302                                         createdKey.SetValue ("name2", "value2");
1303                                         string [] names = createdKey.GetValueNames ();
1304                                         Assert.IsNotNull (names, "#A2");
1305                                         Assert.AreEqual (2, names.Length, "#A3");
1306                                         Assert.IsNotNull (names [0], "#A4");
1307                                         Assert.AreEqual ("name1", names [0], "#A5");
1308                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A6");
1309                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A7");
1310                                         Assert.AreEqual ("name2", names [1], "#A8");
1311                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#A9");
1312                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#A10");
1313                                 }
1314                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1315                                         Assert.IsNotNull (createdKey, "#B1");
1316                                         createdKey.DeleteValue ("name1");
1317                                         string [] names = createdKey.GetValueNames ();
1318                                         Assert.IsNotNull (names, "#B2");
1319                                         Assert.AreEqual (1, names.Length, "#B3");
1320                                         Assert.IsNotNull (names [0], "#B4");
1321                                         Assert.AreEqual ("name2", names [0], "#B5");
1322                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#B6");
1323                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#B7");
1324                                         createdKey.DeleteValue (new string ('a', 400), false);
1325                                 }
1326                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1327                                         string [] names = createdKey.GetValueNames ();
1328                                         Assert.IsNotNull (names, "#C1");
1329                                         Assert.AreEqual (1, names.Length, "#C2");
1330                                         Assert.IsNotNull (names [0], "#C3");
1331                                         Assert.AreEqual ("name2", names [0], "#C4");
1332                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#C5");
1333                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#C6");
1334                                 }
1335                         } finally {
1336                                 try {
1337                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1338                                         if (createdKey != null) {
1339                                                 createdKey.Close ();
1340                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1341                                         }
1342                                 } catch {
1343                                 }
1344                         }
1345                 }
1346
1347                 [Test]
1348                 public void DeleteValue_Key_ReadOnly ()
1349                 {
1350                         string subKeyName = Guid.NewGuid ().ToString ();
1351
1352                         try {
1353                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1354                                         createdKey.SetValue ("name1", "value1");
1355                                 }
1356
1357                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1358                                         try {
1359                                                 // deleting value that exists
1360                                                 createdKey.DeleteValue ("name1");
1361                                                 Assert.Fail ("#A1");
1362                                         } catch (UnauthorizedAccessException ex) {
1363                                                 // Cannot write to the registry key
1364                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#A2");
1365                                                 Assert.IsNotNull (ex.Message, "#A3");
1366                                                 Assert.IsNull (ex.InnerException, "#A4");
1367                                         }
1368
1369                                         try {
1370                                                 // deleting value that exists
1371                                                 createdKey.DeleteValue ("name1", true);
1372                                                 Assert.Fail ("#B1");
1373                                         } catch (UnauthorizedAccessException ex) {
1374                                                 // Cannot write to the registry key
1375                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#B2");
1376                                                 Assert.IsNotNull (ex.Message, "#B3");
1377                                                 Assert.IsNull (ex.InnerException, "#B4");
1378                                         }
1379
1380                                         try {
1381                                                 // deleting value that exists
1382                                                 createdKey.DeleteValue ("name1", false);
1383                                                 Assert.Fail ("#C1");
1384                                         } catch (UnauthorizedAccessException ex) {
1385                                                 // Cannot write to the registry key
1386                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#C2");
1387                                                 Assert.IsNotNull (ex.Message, "#C3");
1388                                                 Assert.IsNull (ex.InnerException, "#C4");
1389                                         }
1390
1391                                         try {
1392                                                 // deleting value that does not exist
1393                                                 createdKey.DeleteValue ("name2");
1394                                                 Assert.Fail ("#D1");
1395                                         } catch (UnauthorizedAccessException ex) {
1396                                                 // Cannot write to the registry key
1397                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#D2");
1398                                                 Assert.IsNotNull (ex.Message, "#D3");
1399                                                 Assert.IsNull (ex.InnerException, "#D4");
1400                                         }
1401
1402                                         try {
1403                                                 // deleting value that does not exist
1404                                                 createdKey.DeleteValue ("name2", true);
1405                                                 Assert.Fail ("#E1");
1406                                         } catch (UnauthorizedAccessException ex) {
1407                                                 // Cannot write to the registry key
1408                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#E2");
1409                                                 Assert.IsNotNull (ex.Message, "#E3");
1410                                                 Assert.IsNull (ex.InnerException, "#E4");
1411                                         }
1412
1413                                         try {
1414                                                 // deleting value that does not exist
1415                                                 createdKey.DeleteValue ("name2", false);
1416                                                 Assert.Fail ("#F1");
1417                                         } catch (UnauthorizedAccessException ex) {
1418                                                 // Cannot write to the registry key
1419                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#F2");
1420                                                 Assert.IsNotNull (ex.Message, "#F3");
1421                                                 Assert.IsNull (ex.InnerException, "#F4");
1422                                         }
1423                                 }
1424                         } finally {
1425                                 try {
1426                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1427                                         if (createdKey != null) {
1428                                                 createdKey.Close ();
1429                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1430                                         }
1431                                 } catch {
1432                                 }
1433                         }
1434                 }
1435
1436                 [Test]
1437                 public void DeleteValue_Key_Removed ()
1438                 {
1439                         string subKeyName = Guid.NewGuid ().ToString ();
1440
1441                         try {
1442                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1443                                         // check if key was successfully created
1444                                         Assert.IsNotNull (createdKey, "#1");
1445                                         createdKey.SetValue ("name1", "value1");
1446                                 }
1447                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1448                                         Assert.IsNotNull (createdKey, "#2");
1449                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1450                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#3");
1451
1452                                         createdKey.DeleteValue ("name1");
1453                                         createdKey.DeleteValue ("name1", true);
1454                                         createdKey.DeleteValue ("name1", false);
1455                                 }
1456                         } finally {
1457                                 try {
1458                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1459                                         if (createdKey != null) {
1460                                                 createdKey.Close ();
1461                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1462                                         }
1463                                 } catch {
1464                                 }
1465                         }
1466                 }
1467
1468                 [Test]
1469                 public void DeleteValue_Value_DoesNotExist ()
1470                 {
1471                         string subKeyName = Guid.NewGuid ().ToString ();
1472
1473                         try {
1474                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1475                                         // check if key was successfully created
1476                                         Assert.IsNotNull (createdKey, "#A1");
1477                                         createdKey.SetValue ("name1", "value1");
1478
1479                                         try {
1480                                                 createdKey.DeleteValue ("name2");
1481                                                 Assert.Fail ("#B1");
1482                                         } catch (ArgumentException ex) {
1483                                                 // No value exists with that name
1484                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1485                                                 Assert.IsNull (ex.InnerException, "#B3");
1486                                                 Assert.IsNotNull (ex.Message, "#B4");
1487                                                 Assert.IsNull (ex.ParamName, "#B5");
1488                                         }
1489
1490                                         try {
1491                                                 createdKey.DeleteValue ("name2", true);
1492                                                 Assert.Fail ("#C1");
1493                                         } catch (ArgumentException ex) {
1494                                                 // No value exists with that name
1495                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1496                                                 Assert.IsNull (ex.InnerException, "#C3");
1497                                                 Assert.IsNotNull (ex.Message, "#C4");
1498                                                 Assert.IsNull (ex.ParamName, "#C5");
1499                                         }
1500
1501                                         createdKey.DeleteValue ("name2", false);
1502                                 }
1503                         } finally {
1504                                 try {
1505                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1506                                         if (createdKey != null) {
1507                                                 createdKey.Close ();
1508                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1509                                         }
1510                                 } catch {
1511                                 }
1512                         }
1513                 }
1514
1515                 [Test]
1516                 public void DeleteValue_Name_Empty ()
1517                 {
1518                         string subKeyName = Guid.NewGuid ().ToString ();
1519
1520                         try {
1521                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1522                                         createdKey.SetValue ("name1", "value1");
1523                                         createdKey.SetValue (string.Empty, "value2");
1524
1525                                         string [] names = createdKey.GetValueNames ();
1526                                         Assert.IsNotNull (names, "#A1");
1527                                         Assert.AreEqual (2, names.Length, "#A2");
1528                                         Assert.IsNotNull (names [0], "#A3");
1529                                         /*
1530                                         Assert.AreEqual ("name1", names [0], "#A4");
1531                                         */
1532                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1533                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1534                                         Assert.IsNotNull (names [1], "#A7");
1535                                         /*
1536                                         Assert.AreEqual (string.Empty, names [1], "#A8");
1537                                         */
1538                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A9");
1539                                         Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#A10");
1540
1541                                         createdKey.DeleteValue (string.Empty);
1542
1543                                         names = createdKey.GetValueNames ();
1544                                         Assert.IsNotNull (names, "#B1");
1545                                         Assert.AreEqual (1, names.Length, "#B2");
1546                                         Assert.IsNotNull (names [0], "#B3");
1547                                         Assert.AreEqual ("name1", names [0], "#B4");
1548                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#B5");
1549                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#B6");
1550
1551                                         try {
1552                                                 createdKey.DeleteValue (string.Empty);
1553                                                 Assert.Fail ("#C1");
1554                                         } catch (ArgumentException ex) {
1555                                                 // No value exists with that name
1556                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1557                                                 Assert.IsNull (ex.InnerException, "#C3");
1558                                                 Assert.IsNotNull (ex.Message, "#C4");
1559                                                 Assert.IsNull (ex.ParamName, "#C5");
1560                                         }
1561
1562                                         try {
1563                                                 createdKey.DeleteValue (string.Empty, true);
1564                                                 Assert.Fail ("#D1");
1565                                         } catch (ArgumentException ex) {
1566                                                 // No value exists with that name
1567                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
1568                                                 Assert.IsNull (ex.InnerException, "#D3");
1569                                                 Assert.IsNotNull (ex.Message, "#D4");
1570                                                 Assert.IsNull (ex.ParamName, "#D5");
1571                                         }
1572
1573                                         createdKey.DeleteValue (string.Empty, false);
1574
1575                                         names = createdKey.GetValueNames ();
1576                                         Assert.IsNotNull (names, "#E1");
1577                                         Assert.AreEqual (1, names.Length, "#E2");
1578                                         Assert.IsNotNull (names [0], "#E3");
1579                                         Assert.AreEqual ("name1", names [0], "#E4");
1580                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#E5");
1581                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#E6");
1582                                 }
1583                         } finally {
1584                                 try {
1585                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1586                                         if (createdKey != null) {
1587                                                 createdKey.Close ();
1588                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1589                                         }
1590                                 } catch {
1591                                 }
1592                         }
1593                 }
1594
1595                 [Test]
1596                 public void DeleteValue_Name_Null ()
1597                 {
1598                         string subKeyName = Guid.NewGuid ().ToString ();
1599
1600                         try {
1601                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1602                                         createdKey.SetValue ("name1", "value1");
1603                                         createdKey.SetValue (null, "value2");
1604
1605                                         string [] names = createdKey.GetValueNames ();
1606                                         Assert.IsNotNull (names, "#A1");
1607                                         Assert.AreEqual (2, names.Length, "#A2");
1608                                         Assert.IsNotNull (names [0], "#A3");
1609                                         /*
1610                                         Assert.AreEqual ("name1", names [0], "#A4");
1611                                         */
1612                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1613                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1614                                         Assert.IsNotNull (names [1], "#A7");
1615                                         /*
1616                                         Assert.AreEqual (string.Empty, names [1], "#A8");
1617                                         */
1618                                         Assert.IsNotNull (createdKey.GetValue (null), "#A9");
1619                                         Assert.AreEqual ("value2", createdKey.GetValue (null), "#A10");
1620
1621                                         try {
1622                                                 createdKey.DeleteValue (null);
1623                                                 Assert.Fail ("#B1");
1624                                         } catch (ArgumentNullException ex) {
1625                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1626                                                 Assert.IsNull (ex.InnerException, "#B3");
1627                                                 Assert.IsNotNull (ex.Message, "#B4");
1628                                                 Assert.AreEqual ("name", ex.ParamName, "#B5");
1629                                         }
1630
1631                                         try {
1632                                                 createdKey.DeleteValue (null, true);
1633                                                 Assert.Fail ("#C1");
1634                                         } catch (ArgumentNullException ex) {
1635                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
1636                                                 Assert.IsNull (ex.InnerException, "#C3");
1637                                                 Assert.IsNotNull (ex.Message, "#C4");
1638                                                 Assert.AreEqual ("name", ex.ParamName, "#C5");
1639                                         }
1640
1641                                         try {
1642                                                 createdKey.DeleteValue (null, false);
1643                                                 Assert.Fail ("#D1");
1644                                         } catch (ArgumentNullException ex) {
1645                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
1646                                                 Assert.IsNull (ex.InnerException, "#D3");
1647                                                 Assert.IsNotNull (ex.Message, "#D4");
1648                                                 Assert.AreEqual ("name", ex.ParamName, "#D5");
1649                                         }
1650                                 }
1651                         } finally {
1652                                 try {
1653                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1654                                         if (createdKey != null) {
1655                                                 createdKey.Close ();
1656                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1657                                         }
1658                                 } catch {
1659                                 }
1660                         }
1661                 }
1662
1663                 [Test]
1664                 public void GetValue ()
1665                 {
1666                         string subKeyName = Guid.NewGuid ().ToString ();
1667
1668                         try {
1669                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1670                                         createdKey.SetValue ("name1", "value1");
1671                                         createdKey.SetValue ("name2", "value2");
1672                                 }
1673
1674                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1675                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#1");
1676                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#2");
1677                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#3");
1678                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#4");
1679                                         Assert.IsNull (createdKey.GetValue ("name3"), "#5");
1680                                         Assert.AreEqual ("value3", createdKey.GetValue ("name3", "value3"), "#6");
1681                                         Assert.IsNull (createdKey.GetValue ("name3", null), "#7");
1682                                         Assert.IsNull (createdKey.GetValue (new string ('a', 400)), "#8");
1683                                 }
1684                         } finally {
1685                                 try {
1686                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1687                                         if (createdKey != null) {
1688                                                 createdKey.Close ();
1689                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1690                                         }
1691                                 } catch {
1692                                 }
1693                         }
1694                 }
1695
1696                 [Test]
1697                 public void GetValue_Key_Removed ()
1698                 {
1699                         string subKeyName = Guid.NewGuid ().ToString ();
1700
1701                         try {
1702                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1703                                         createdKey.SetValue ("name1", "value1");
1704                                         createdKey.SetValue ("name2", "value2");
1705                                 }
1706
1707                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1708                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1709
1710                                         Assert.IsNull (createdKey.GetValue ("name1"), "#1");
1711                                         Assert.IsNotNull (createdKey.GetValue ("name1", "default"), "#2");
1712                                         Assert.AreEqual ("default", createdKey.GetValue ("name1", "default"), "#3");
1713                                         Assert.IsNull (createdKey.GetValue ("name3"), "#3");
1714                                         Assert.IsNotNull (createdKey.GetValue ("name3", "default"), "#4");
1715                                         Assert.AreEqual ("default", createdKey.GetValue ("name3", "default"), "#5");
1716                                 }
1717                         } finally {
1718                                 try {
1719                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1720                                         if (createdKey != null) {
1721                                                 createdKey.Close ();
1722                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1723                                         }
1724                                 } catch {
1725                                 }
1726                         }
1727                 }
1728
1729                 [Test]
1730                 public void GetValue_Name_Empty ()
1731                 {
1732                         string subKeyName = Guid.NewGuid ().ToString ();
1733
1734                         try {
1735                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1736                                         createdKey.SetValue ("name1", "value1");
1737                                         createdKey.SetValue ("name2", "value2");
1738
1739                                         Assert.IsNull (createdKey.GetValue (string.Empty), "#A1");
1740                                         Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#A2");
1741                                         Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#A3");
1742                                         Assert.IsNull (createdKey.GetValue (string.Empty, null), "#A4");
1743                                 }
1744
1745                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1746                                         Assert.IsNull (createdKey.GetValue (string.Empty), "#B1");
1747                                         Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#B2");
1748                                         Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#B3");
1749                                         Assert.IsNull (createdKey.GetValue (string.Empty, null), "#B4");
1750                                 }
1751
1752                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1753                                         createdKey.SetValue (string.Empty, "value1");
1754                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#C1");
1755                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#C2");
1756                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#C3");
1757                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#C4");
1758                                 }
1759
1760                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1761                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#D1");
1762                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#D2");
1763                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#D3");
1764                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#D4");
1765                                 }
1766                         } finally {
1767                                 try {
1768                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1769                                         if (createdKey != null) {
1770                                                 createdKey.Close ();
1771                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1772                                         }
1773                                 } catch {
1774                                 }
1775                         }
1776                 }
1777
1778                 [Test]
1779                 public void GetValue_Name_Null ()
1780                 {
1781                         string subKeyName = Guid.NewGuid ().ToString ();
1782
1783                         try {
1784                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1785                                         createdKey.SetValue ("name1", "value1");
1786                                         createdKey.SetValue ("name2", "value2");
1787
1788                                         Assert.IsNull (createdKey.GetValue (null), "#A1");
1789                                         Assert.IsNotNull (createdKey.GetValue (null, "default"), "#A2");
1790                                         Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#A3");
1791                                         Assert.IsNull (createdKey.GetValue (null, null), "#A4");
1792                                 }
1793
1794                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1795                                         Assert.IsNull (createdKey.GetValue (null), "#B1");
1796                                         Assert.IsNotNull (createdKey.GetValue (null, "default"), "#B2");
1797                                         Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#B3");
1798                                         Assert.IsNull (createdKey.GetValue (null, null), "#B4");
1799                                 }
1800
1801                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1802                                         createdKey.SetValue (string.Empty, "value1");
1803                                         Assert.IsNotNull (createdKey.GetValue (null), "#C1");
1804                                         Assert.AreEqual ("value1", createdKey.GetValue (null), "#C2");
1805                                         Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#C3");
1806                                         Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#C4");
1807                                 }
1808
1809                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1810                                         Assert.IsNotNull (createdKey.GetValue (null), "#D1");
1811                                         Assert.AreEqual ("value1", createdKey.GetValue (null), "#D2");
1812                                         Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#D3");
1813                                         Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#D4");
1814                                 }
1815                         } finally {
1816                                 try {
1817                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1818                                         if (createdKey != null) {
1819                                                 createdKey.Close ();
1820                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1821                                         }
1822                                 } catch {
1823                                 }
1824                         }
1825                 }
1826
1827                 [Test]
1828                 public void GetValue_Expand ()
1829                 {
1830                         string subKeyName = Guid.NewGuid ().ToString ();
1831
1832                         try {
1833                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1834                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1835                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1836
1837                                         createdKey.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1838                                                 RegistryValueKind.ExpandString);
1839                                         createdKey.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1840                                         createdKey.SetValue ("name3", "just some text",
1841                                                 RegistryValueKind.ExpandString);
1842
1843                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#A1");
1844                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#A2");
1845                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#A3");
1846                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1847                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A4");
1848                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1849                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A5");
1850                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1851                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A6");
1852                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1853                                                 null, RegistryValueOptions.None), "#A7");
1854                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1855                                                 null, RegistryValueOptions.None), "#A8");
1856                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1857                                                 null, RegistryValueOptions.None), "#A9");
1858
1859                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "789");
1860                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "666");
1861
1862                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#B1");
1863                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#B2");
1864                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#B3");
1865                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1866                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B4");
1867                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1868                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B5");
1869                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1870                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B6");
1871                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1872                                                 null, RegistryValueOptions.None), "#B7");
1873                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1874                                                 null, RegistryValueOptions.None), "#B8");
1875                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1876                                                 null, RegistryValueOptions.None), "#B9");
1877                                 }
1878                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1879                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#C1");
1880                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#C2");
1881                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#C3");
1882                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1883                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C4");
1884                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1885                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C5");
1886                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1887                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C6");
1888                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1889                                                 null, RegistryValueOptions.None), "#C7");
1890                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1891                                                 null, RegistryValueOptions.None), "#C8");
1892                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1893                                                 null, RegistryValueOptions.None), "#C9");
1894
1895                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1896                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1897
1898                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#D1");
1899                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#D2");
1900                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#D3");
1901                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1902                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D4");
1903                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1904                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D5");
1905                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1906                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D6");
1907                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1908                                                 null, RegistryValueOptions.None), "#D7");
1909                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1910                                                 null, RegistryValueOptions.None), "#D8");
1911                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1912                                                 null, RegistryValueOptions.None), "#D9");
1913                                 }
1914                         } finally {
1915                                 try {
1916                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1917                                         if (createdKey != null) {
1918                                                 createdKey.Close ();
1919                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1920                                         }
1921                                 } catch {
1922                                 }
1923                         }
1924                 }
1925
1926                 [Test]
1927                 public void GetValueNames ()
1928                 {
1929                         string subKeyName = Guid.NewGuid ().ToString ();
1930
1931                         try {
1932                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1933                                         string [] names = createdKey.GetValueNames ();
1934                                         Assert.IsNotNull (names, "#A1");
1935                                         Assert.AreEqual (0, names.Length, "#A2");
1936
1937                                         createdKey.SetValue ("name1", "value1");
1938                                         createdKey.SetValue ("name2", "value2");
1939                                         createdKey.SetValue ("namelong", "value3");
1940                                         createdKey.SetValue ("name3", "value4");
1941
1942                                         Assert.AreEqual (4, createdKey.ValueCount, "#B1");
1943                                         names = createdKey.GetValueNames ();
1944                                         Assert.IsNotNull (names, "#B2");
1945                                         Assert.AreEqual (4, names.Length, "#B3");
1946                                 }
1947
1948                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1949                                         string [] names = createdKey.GetValueNames ();
1950                                         Assert.IsNotNull (names, "#C1");
1951                                         Assert.AreEqual (4, names.Length, "#C2");
1952
1953                                         // Mono's Unix registry API uses a hashtable to store the
1954                                         // values (and their names), so names are not returned in
1955                                         // order
1956                                         //
1957                                         // to test whether the names returned by GetValueNames
1958                                         // match what we expect, we use these names to remove the
1959                                         // the values from the created keys and such we should end
1960                                         // up with zero values
1961                                         for (int i = 0; i < names.Length; i++) {
1962                                                 string valueName = names [i];
1963                                                 createdKey.DeleteValue (valueName);
1964                                         }
1965
1966                                         // all values should be removed now
1967                                         Assert.AreEqual (0, createdKey.ValueCount, "#C3");
1968                                 }
1969                         } finally {
1970                                 try {
1971                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1972                                         if (createdKey != null) {
1973                                                 createdKey.Close ();
1974                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1975                                         }
1976                                 } catch {
1977                                 }
1978                         }
1979                 }
1980
1981                 [Test]
1982                 public void GetValueNames_Key_Removed ()
1983                 {
1984                         string subKeyName = Guid.NewGuid ().ToString ();
1985
1986                         try {
1987                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1988                                         createdKey.SetValue ("name1", "value1");
1989                                         createdKey.SetValue ("name2", "value2");
1990
1991                                         string [] names = createdKey.GetValueNames ();
1992                                         Assert.IsNotNull (names, "#A1");
1993                                         Assert.AreEqual (2, names.Length, "#A2");
1994                                 }
1995
1996                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1997                                         string [] names = createdKey.GetValueNames ();
1998                                         Assert.IsNotNull (names, "#B1");
1999                                         Assert.AreEqual (2, names.Length, "#B2");
2000
2001                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2002
2003                                         try {
2004                                                 createdKey.GetValueNames ();
2005                                                 Assert.Fail ("#C1");
2006                                         } catch (IOException ex) {
2007                                                 // Illegal operation attempted on a registry key that
2008                                                 // has been marked for deletion
2009                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
2010                                                 Assert.IsNotNull (ex.Message, "#C3");
2011                                                 Assert.IsNull (ex.InnerException, "#C4");
2012                                         }
2013                                 }
2014                         } finally {
2015                                 try {
2016                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2017                                         if (createdKey != null) {
2018                                                 createdKey.Close ();
2019                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2020                                         }
2021                                 } catch {
2022                                 }
2023                         }
2024                 }
2025
2026                 [Test] // bug #78519
2027                 public void GetSubKeyNamesTest ()
2028                 {
2029                         string subKeyName = Guid.NewGuid ().ToString ();
2030
2031                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2032                         try {
2033                                 // check if key was successfully created
2034                                 Assert.IsNotNull (createdKey, "#A");
2035
2036                                 RegistryKey subKey = createdKey.CreateSubKey ("foo");
2037                                 Assert.IsNotNull (subKey, "#B1");
2038                                 Assert.AreEqual (1, createdKey.SubKeyCount, "#B2");
2039                                 string[] subKeyNames = createdKey.GetSubKeyNames ();
2040                                 Assert.IsNotNull (subKeyNames, "#B3");
2041                                 Assert.AreEqual (1, subKeyNames.Length, "#B4");
2042                                 Assert.AreEqual ("foo", subKeyNames[0], "#B5");
2043
2044                                 subKey = createdKey.CreateSubKey ("longfoo");
2045                                 Assert.IsNotNull (subKey, "#C1");
2046                                 Assert.AreEqual (2, createdKey.SubKeyCount, "#C2");
2047                                 subKeyNames = createdKey.GetSubKeyNames ();
2048                                 Assert.IsNotNull (subKeyNames, "#C3");
2049                                 Assert.AreEqual (2, subKeyNames.Length, "#C4");
2050                                 Assert.AreEqual ("foo", subKeyNames [0], "#C5");
2051                                 Assert.AreEqual ("longfoo", subKeyNames [1], "#C6");
2052
2053                                 subKey = createdKey.CreateSubKey ("sfoo");
2054                                 Assert.IsNotNull (subKey, "#D1");
2055                                 Assert.AreEqual (3, createdKey.SubKeyCount, "#D2");
2056                                 subKeyNames = createdKey.GetSubKeyNames ();
2057                                 Assert.IsNotNull (subKeyNames, "#D3");
2058                                 Assert.AreEqual (3, subKeyNames.Length, "#D4");
2059                                 Assert.AreEqual ("foo", subKeyNames [0], "#D5");
2060                                 Assert.AreEqual ("longfoo", subKeyNames [1], "#D6");
2061                                 Assert.AreEqual ("sfoo", subKeyNames [2], "#D7");
2062
2063                                 foreach (string name in subKeyNames) {
2064                                         createdKey.DeleteSubKeyTree (name);
2065                                 }
2066                                 Assert.AreEqual (0, createdKey.SubKeyCount, "#E");
2067                         } finally {
2068                                 // clean-up
2069                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2070                         }
2071                 }
2072
2073                 [Test]
2074                 public void OpenRemoteBaseKey ()
2075                 {
2076                         // access to registry of remote machines is not implemented on unix
2077                         if (RunningOnUnix)
2078                                 return;
2079
2080                         RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
2081                                 RegistryHive.CurrentUser, Environment.MachineName);
2082                         Assert.IsNotNull (hive, "#1");
2083
2084                         RegistryKey key = hive.OpenSubKey ("SOFTWARE");
2085                         Assert.IsNotNull (key, "#2");
2086                         key.Close ();
2087
2088                         hive.Close ();
2089                 }
2090
2091                 [Test]
2092                 public void OpenRemoteBaseKey_MachineName_Null ()
2093                 {
2094                         try {
2095                                 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser, null);
2096                                 Assert.Fail ("#1");
2097                         } catch (ArgumentNullException ex) {
2098                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2099                                 Assert.IsNull (ex.InnerException, "#3");
2100                                 Assert.IsNotNull (ex.Message, "#4");
2101                                 Assert.AreEqual ("machineName", ex.ParamName, "#5");
2102                         }
2103                 }
2104
2105                 [Test]
2106                 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
2107                 {
2108                         // access to registry of remote machines is not implemented on unix
2109                         if (RunningOnUnix)
2110                                 return;
2111
2112                         try {
2113                                 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
2114                                         "DOESNOTEXIST");
2115                                 Assert.Fail ("#1");
2116                         } catch (IOException ex) {
2117                                 // The network path was not found
2118                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2119                                 Assert.IsNotNull (ex.Message, "#3");
2120                                 Assert.IsNull (ex.InnerException, "#4");
2121                         }
2122                 }
2123
2124                 [Test] // bug #322839
2125                 public void SetValue1_EntityReferences ()
2126                 {
2127                         string subKeyName = Guid.NewGuid ().ToString ();
2128
2129                         try {
2130                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2131                                         // we created a new subkey, so value should not exist
2132                                         Assert.IsNull (createdKey.GetValue ("FirstName&\"<LastName>\""), "#A1");
2133                                         // create value
2134                                         createdKey.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
2135                                         // get value
2136                                         object name = createdKey.GetValue ("FirstName&\"<LastName>\"");
2137                                         // value should exist
2138                                         Assert.IsNotNull (name, "#A2");
2139                                         // type of value should be string
2140                                         Assert.AreEqual (typeof (string), name.GetType (), "#A3");
2141                                         // ensure value matches
2142                                         Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#A4");
2143
2144                                         // we created a new subkey, so value should not exist
2145                                         Assert.IsNull (createdKey.GetValue ("Info"), "#B1");
2146                                         // create value
2147                                         createdKey.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" });
2148                                         // get value
2149                                         object info = createdKey.GetValue ("Info");
2150                                         // value should exist
2151                                         Assert.IsNotNull (info, "#B2");
2152                                         // type of value should be string
2153                                         Assert.AreEqual (typeof (string []), info.GetType (), "#B3");
2154                                         // ensure value matches
2155                                         Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#B4");
2156                                 }
2157
2158                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2159                                         object name = openedKey.GetValue ("FirstName&\"<LastName>\"");
2160                                         Assert.IsNotNull (name, "#C1");
2161                                         Assert.AreEqual (typeof (string), name.GetType (), "#C2");
2162                                         Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#C3");
2163
2164                                         object info = openedKey.GetValue ("Info");
2165                                         Assert.IsNotNull (info, "#D1");
2166                                         Assert.AreEqual (typeof (string []), info.GetType (), "#D2");
2167                                         Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#D3");
2168                                 }
2169                         } finally {
2170                                 // clean-up
2171                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2172                         }
2173                 }
2174
2175                 [Test] // SetValue (String, Object)
2176                 public void SetValue1_Name_Null ()
2177                 {
2178                         string subKeyName = Guid.NewGuid ().ToString ();
2179
2180                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2181                         try {
2182                                 createdKey.SetValue (null, "value1");
2183                                 string [] names = createdKey.GetValueNames ();
2184                                 Assert.IsNotNull (names, "#A1");
2185                                 Assert.AreEqual (1, names.Length, "#A2");
2186                                 Assert.IsNotNull (names [0], "#A3");
2187                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2188                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2189                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2190                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2191                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2192
2193                                 createdKey.SetValue (string.Empty, "value2");
2194                                 names = createdKey.GetValueNames ();
2195                                 Assert.IsNotNull (names, "#B1");
2196                                 Assert.AreEqual (1, names.Length, "#B2");
2197                                 Assert.IsNotNull (names [0], "#B3");
2198                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2199                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2200                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2201                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2202                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2203                         } finally {
2204                                 // clean-up
2205                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2206                         }
2207                 }
2208
2209                 [Test] // SetValue (String, Object)
2210                 public void SetValue1_Name_Empty ()
2211                 {
2212                         string subKeyName = Guid.NewGuid ().ToString ();
2213
2214                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2215                         try {
2216                                 createdKey.SetValue (string.Empty, "value1");
2217                                 string [] names = createdKey.GetValueNames ();
2218                                 Assert.IsNotNull (names, "#A1");
2219                                 Assert.AreEqual (1, names.Length, "#A2");
2220                                 Assert.IsNotNull (names [0], "#A3");
2221                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2222                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2223                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2224                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2225                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2226
2227                                 createdKey.SetValue (null, "value2");
2228                                 names = createdKey.GetValueNames ();
2229                                 Assert.IsNotNull (names, "#B1");
2230                                 Assert.AreEqual (1, names.Length, "#B2");
2231                                 Assert.IsNotNull (names [0], "#B3");
2232                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2233                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2234                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2235                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2236                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2237                         } finally {
2238                                 // clean-up
2239                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2240                         }
2241                 }
2242
2243                 [Test] // SetValue (String, Object)
2244                 public void SetValue1_Name_MaxLength ()
2245                 {
2246                         string subKeyName = Guid.NewGuid ().ToString ();
2247
2248                         try {
2249                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2250                                         string name = new string ('a', 254);
2251
2252                                         createdKey.SetValue (name, "value1");
2253                                         Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2254                                         createdKey.DeleteValue (name);
2255                                         Assert.IsNull (createdKey.GetValue (name), "#A2");
2256
2257                                         name = new string ('a', 255);
2258
2259                                         createdKey.SetValue (name, "value2");
2260                                         Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2261                                         createdKey.DeleteValue (name);
2262                                         Assert.IsNull (createdKey.GetValue (name), "#B2");
2263
2264                                         name = new string ('a', 256);
2265
2266                                         try {
2267                                                 createdKey.SetValue (name, "value2");
2268                                                 Assert.Fail ("#C1");
2269                                         } catch (ArgumentException ex) {
2270                                                 // 1.x: Registry subkeys should not be
2271                                                 // greater than or equal to 255 characters
2272                                                 //
2273                                                 // 2.x: Registry subkeys should not be
2274                                                 // greater than 255 characters
2275                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2276                                                 Assert.IsNull (ex.InnerException, "#C3");
2277                                                 Assert.IsNotNull (ex.Message, "#C4");
2278                                                 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2279                                                 Assert.IsNull (ex.ParamName, "#C6");
2280                                         }
2281                                 }
2282                         } finally {
2283                                 try {
2284                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2285                                         if (createdKey != null) {
2286                                                 createdKey.Close ();
2287                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2288                                         }
2289                                 } catch {
2290                                 }
2291                         }
2292                 }
2293
2294                 [Test] // SetValue (String, Object)
2295                 public void SetValue1_Value_Null ()
2296                 {
2297                         string subKeyName = Guid.NewGuid ().ToString ();
2298
2299                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2300                         try {
2301                                 try {
2302                                         createdKey.SetValue ("Name", null);
2303                                         Assert.Fail ("#1");
2304                                 } catch (ArgumentNullException ex) {
2305                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2306                                         Assert.IsNull (ex.InnerException, "#3");
2307                                         Assert.IsNotNull (ex.Message, "#4");
2308                                         Assert.AreEqual ("value", ex.ParamName, "#5");
2309                                 }
2310                         } finally {
2311                                 // clean-up
2312                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2313                         }
2314                 }
2315
2316                 [Test] // SetValue (String, Object)
2317                 public void SetValue1_Boolean ()
2318                 {
2319                         string subKeyName = Guid.NewGuid ().ToString ();
2320
2321                         try {
2322                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2323                                         // we created a new subkey, so value should not exist
2324                                         Assert.IsNull (createdKey.GetValue ("Installed"), "#A1");
2325                                         // create value
2326                                         createdKey.SetValue ("Installed", true);
2327                                         // get value
2328                                         object value = createdKey.GetValue ("Installed");
2329                                         // value should exist
2330                                         Assert.IsNotNull (value, "#A2");
2331                                         // type of value should be string
2332                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2333                                         // ensure value matches
2334                                         Assert.AreEqual (true.ToString (), value, "#A4");
2335                                 }
2336
2337                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2338                                         object value = openedKey.GetValue ("Installed");
2339                                         Assert.IsNotNull (value, "#B1");
2340                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2341                                         Assert.AreEqual (true.ToString (), value, "#B3");
2342                                 }
2343                         } finally {
2344                                 // clean-up
2345                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2346                         }
2347                 }
2348
2349                 [Test] // SetValue (String, Object)
2350                 public void SetValue1_Byte ()
2351                 {
2352                         string subKeyName = Guid.NewGuid ().ToString ();
2353
2354                         try {
2355                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2356                                         // we created a new subkey, so value should not exist
2357                                         Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2358                                         // create value
2359                                         createdKey.SetValue ("Flags", (byte) 5);
2360                                         // get value
2361                                         object value = createdKey.GetValue ("Flags");
2362                                         // value should exist
2363                                         Assert.IsNotNull (value, "#A2");
2364                                         // type of value should be string
2365                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2366                                         // ensure value matches
2367                                         Assert.AreEqual ("5", value, "#A4");
2368                                 }
2369
2370                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2371                                         object value = openedKey.GetValue ("Flags");
2372                                         Assert.IsNotNull (value, "#B1");
2373                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2374                                         Assert.AreEqual ("5", value, "#B3");
2375                                 }
2376                         } finally {
2377                                 // clean-up
2378                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2379                         }
2380                 }
2381
2382                 [Test] // SetValue (String, Object)
2383                 public void SetValue1_ByteArray ()
2384                 {
2385                         string subKeyName = Guid.NewGuid ().ToString ();
2386
2387                         try {
2388                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2389                                         // we created a new subkey, so value should not exist
2390                                         Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2391                                         // create value
2392                                         createdKey.SetValue ("Flags", new byte [] { 1, 5 });
2393                                         // get value
2394                                         object value = createdKey.GetValue ("Flags");
2395                                         // value should exist
2396                                         Assert.IsNotNull (value, "#A2");
2397                                         // type of value should be string
2398                                         Assert.AreEqual (typeof (byte []), value.GetType (), "#3");
2399                                         // ensure value matches
2400                                         Assert.AreEqual (new byte [] { 1, 5 }, value, "#4");
2401                                 }
2402
2403                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2404                                         object value = openedKey.GetValue ("Flags");
2405                                         Assert.IsNotNull (value, "#B1");
2406                                         Assert.AreEqual (typeof (byte []), value.GetType (), "#B2");
2407                                         Assert.AreEqual (new byte [] { 1, 5 }, value, "#B3");
2408                                 }
2409                         } finally {
2410                                 // clean-up
2411                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2412                         }
2413                 }
2414
2415                 [Test] // SetValue (String, Object)
2416                 public void SetValue1_DateTime ()
2417                 {
2418                         string subKeyName = Guid.NewGuid ().ToString ();
2419
2420                         try {
2421                                 object rawValue = DateTime.Now;
2422
2423                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2424                                         // we created a new subkey, so value should not exist
2425                                         Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2426                                         // create value
2427                                         createdKey.SetValue ("Path", rawValue);
2428                                         // get value
2429                                         object value = createdKey.GetValue ("Path");
2430                                         // value should exist
2431                                         Assert.IsNotNull (value, "#A2");
2432                                         // type of value should be string
2433                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2434                                         // ensure value matches
2435                                         Assert.AreEqual (rawValue.ToString (), value, "#A4");
2436                                 }
2437
2438                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2439                                         object value = openedKey.GetValue ("Path");
2440                                         Assert.IsNotNull (value, "#B1");
2441                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2442                                         Assert.AreEqual (rawValue.ToString (), value, "#B3");
2443                                 }
2444                         } finally {
2445                                 // clean-up
2446                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2447                         }
2448                 }
2449
2450                 [Test]
2451                 public void SetValue_Int32 ()
2452                 {
2453                         string subKeyName = Guid.NewGuid ().ToString ();
2454
2455                         try {
2456                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2457                                         // we created a new subkey, so value should not exist
2458                                         Assert.IsNull (createdKey.GetValue ("RefCount"), "#A1");
2459                                         // create value
2460                                         createdKey.SetValue ("RefCount", 5);
2461                                         // get value
2462                                         object value = createdKey.GetValue ("RefCount");
2463                                         // value should exist
2464                                         Assert.IsNotNull (value, "#A2");
2465                                         // type of value should be int
2466                                         Assert.AreEqual (typeof (int), value.GetType (), "#A3");
2467                                         // ensure value matches
2468                                         Assert.AreEqual (5, value, "#A4");
2469                                 }
2470
2471                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2472                                         object value = openedKey.GetValue ("RefCount");
2473                                         Assert.IsNotNull (value, "#B1");
2474                                         Assert.AreEqual (typeof (int), value.GetType (), "#B2");
2475                                         Assert.AreEqual (5, value, "#B3");
2476                                 }
2477                         } finally {
2478                                 // clean-up
2479                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2480                         }
2481                 }
2482
2483                 [Test] // SetValue (String, Object)
2484                 public void SetValue1_Int64 ()
2485                 {
2486                         string subKeyName = Guid.NewGuid ().ToString ();
2487
2488                         try {
2489                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2490                                         // we created a new subkey, so value should not exist
2491                                         Assert.IsNull (createdKey.GetValue ("Ticks"), "#A1");
2492                                         // create value
2493                                         createdKey.SetValue ("Ticks", 500L);
2494                                         // get value
2495                                         object value = createdKey.GetValue ("Ticks");
2496                                         // value should exist
2497                                         Assert.IsNotNull (value, "#A2");
2498                                         // type of value should be string
2499                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2500                                         // ensure value matches
2501                                         Assert.AreEqual ("500", value, "#A4");
2502                                 }
2503
2504                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2505                                         object value = openedKey.GetValue ("Ticks");
2506                                         Assert.IsNotNull (value, "#B1");
2507                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2508                                         Assert.AreEqual ("500", value, "#B3");
2509                                 }
2510                         } finally {
2511                                 // clean-up
2512                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2513                         }
2514                 }
2515
2516                 [Test] // SetValue (String, Object)
2517                 public void SetValue1_String ()
2518                 {
2519                         string subKeyName = Guid.NewGuid ().ToString ();
2520
2521                         try {
2522                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2523                                         // we created a new subkey, so value should not exist
2524                                         Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2525                                         // create value
2526                                         createdKey.SetValue ("Path", "/usr/lib/whatever");
2527                                         // get value
2528                                         object path = createdKey.GetValue ("Path");
2529                                         // value should exist
2530                                         Assert.IsNotNull (path, "#A2");
2531                                         // type of value should be string
2532                                         Assert.AreEqual (typeof (string), path.GetType (), "#A3");
2533                                         // ensure value matches
2534                                         Assert.AreEqual ("/usr/lib/whatever", path, "#A4");
2535                                 }
2536
2537                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2538                                         object path = openedKey.GetValue ("Path");
2539                                         Assert.IsNotNull (path, "#B1");
2540                                         Assert.AreEqual (typeof (string), path.GetType (), "#B2");
2541                                         Assert.AreEqual ("/usr/lib/whatever", path, "#B3");
2542                                 }
2543                         } finally {
2544                                 // clean-up
2545                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2546                         }
2547                 }
2548
2549                 [Test] // SetValue (String, Object)
2550                 public void SetValue1_StringArray ()
2551                 {
2552                         string subKeyName = Guid.NewGuid ().ToString ();
2553
2554                         try {
2555                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2556                                         // we created a new subkey, so value should not exist
2557                                         Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#A1");
2558                                         // create value
2559                                         createdKey.SetValue ("DependsOnGroup", new string [] { "A", "B" });
2560                                         // get value
2561                                         object value = createdKey.GetValue ("DependsOnGroup");
2562                                         // value should exist
2563                                         Assert.IsNotNull (value, "#A2");
2564                                         // type of value should be string
2565                                         Assert.AreEqual (typeof (string []), value.GetType (), "#A3");
2566                                         // ensure value matches
2567                                         Assert.AreEqual (new string [] { "A", "B" }, value, "#A4");
2568                                 }
2569
2570                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2571                                         object value = openedKey.GetValue ("DependsOnGroup");
2572                                         Assert.IsNotNull (value, "#B1");
2573                                         Assert.AreEqual (typeof (string []), value.GetType (), "#B2");
2574                                         Assert.AreEqual (new string [] { "A", "B" }, value, "#B3");
2575                                 }
2576                         } finally {
2577                                 // clean-up
2578                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2579                         }
2580                 }
2581
2582                 [Test] // SetValue (String, Object)
2583                 public void SetValue1_Key_ReadOnly ()
2584                 {
2585                         string subKeyName = Guid.NewGuid ().ToString ();
2586
2587                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2588                                 try {
2589                                         softwareKey.SetValue ("name1", "value1");
2590                                         Assert.Fail ("#1");
2591                                 } catch (UnauthorizedAccessException ex) {
2592                                         // Cannot write to the registry key
2593                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2594                                         Assert.IsNotNull (ex.Message, "#3");
2595                                         Assert.IsNull (ex.InnerException, "#4");
2596                                 }
2597                         }
2598
2599                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2600                                 try {
2601                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2602                                         }
2603
2604                                         using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2605                                                 try {
2606                                                         createdKey.SetValue ("name1", "value1");
2607                                                         Assert.Fail ("#1");
2608                                                 } catch (UnauthorizedAccessException ex) {
2609                                                         // Cannot write to the registry key
2610                                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2611                                                         Assert.IsNotNull (ex.Message, "#3");
2612                                                         Assert.IsNull (ex.InnerException, "#4");
2613                                                 }
2614                                         }
2615                                 } finally {
2616                                         try {
2617                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2618                                                 if (createdKey != null) {
2619                                                         createdKey.Close ();
2620                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2621                                                 }
2622                                         } catch {
2623                                         }
2624                                 }
2625                         }
2626                 }
2627
2628                 [Test] // SetValue (String, Object)
2629                 public void SetValue1_Key_Removed ()
2630                 {
2631                         string subKeyName = Guid.NewGuid ().ToString ();
2632
2633                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2634                                 try {
2635                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2636                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2637                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2638                                                 try {
2639                                                         createdKey.SetValue ("name1", "value1");
2640                                                         Assert.Fail ("#2");
2641                                                 } catch (IOException ex) {
2642                                                         // Illegal operation attempted on a registry key that
2643                                                         // has been marked for deletion
2644                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2645                                                         Assert.IsNotNull (ex.Message, "#4");
2646                                                         Assert.IsNull (ex.InnerException, "#5");
2647                                                 }
2648                                         }
2649                                 } finally {
2650                                         try {
2651                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2652                                                 if (createdKey != null) {
2653                                                         createdKey.Close ();
2654                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2655                                                 }
2656                                         } catch {
2657                                         }
2658                                 }
2659                         }
2660                 }
2661
2662                 [Test] // SetValue (String, Object, RegistryValueKind)
2663                 public void SetValue2_Key_ReadOnly ()
2664                 {
2665                         string subKeyName = Guid.NewGuid ().ToString ();
2666
2667                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2668                                 try {
2669                                         softwareKey.SetValue ("name1", "value1",
2670                                                 RegistryValueKind.String);
2671                                         Assert.Fail ("#1");
2672                                 } catch (UnauthorizedAccessException ex) {
2673                                         // Cannot write to the registry key
2674                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2675                                         Assert.IsNotNull (ex.Message, "#3");
2676                                         Assert.IsNull (ex.InnerException, "#4");
2677                                 }
2678                         }
2679
2680                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2681                                 try {
2682                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2683                                         }
2684
2685                                         using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2686                                                 try {
2687                                                         createdKey.SetValue ("name1", "value1",
2688                                                                 RegistryValueKind.String);
2689                                                         Assert.Fail ("#1");
2690                                                 } catch (UnauthorizedAccessException ex) {
2691                                                         // Cannot write to the registry key
2692                                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2693                                                         Assert.IsNotNull (ex.Message, "#3");
2694                                                         Assert.IsNull (ex.InnerException, "#4");
2695                                                 }
2696                                         }
2697                                 } finally {
2698                                         try {
2699                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2700                                                 if (createdKey != null) {
2701                                                         createdKey.Close ();
2702                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2703                                                 }
2704                                         } catch {
2705                                         }
2706                                 }
2707                         }
2708                 }
2709
2710                 [Test] // SetValue (String, Object, RegistryValueKind)
2711                 public void SetValue2_Key_Removed ()
2712                 {
2713                         string subKeyName = Guid.NewGuid ().ToString ();
2714
2715                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2716                                 try {
2717                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2718                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2719                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2720                                                 try {
2721                                                         createdKey.SetValue ("name1", "value1",
2722                                                                 RegistryValueKind.String);
2723                                                         Assert.Fail ("#2");
2724                                                 } catch (IOException ex) {
2725                                                         // Illegal operation attempted on a registry key that
2726                                                         // has been marked for deletion
2727                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2728                                                         Assert.IsNotNull (ex.Message, "#4");
2729                                                         Assert.IsNull (ex.InnerException, "#5");
2730                                                 }
2731                                         }
2732                                 } finally {
2733                                         try {
2734                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2735                                                 if (createdKey != null) {
2736                                                         createdKey.Close ();
2737                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2738                                                 }
2739                                         } catch {
2740                                         }
2741                                 }
2742                         }
2743                 }
2744
2745                 [Test] // SetValue (String, Object, RegistryValueKind)
2746                 public void SetValue2_Name_Empty ()
2747                 {
2748                         string subKeyName = Guid.NewGuid ().ToString ();
2749
2750                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2751                         try {
2752                                 createdKey.SetValue (string.Empty, "value1",
2753                                         RegistryValueKind.String);
2754                                 string [] names = createdKey.GetValueNames ();
2755                                 Assert.IsNotNull (names, "#A1");
2756                                 Assert.AreEqual (1, names.Length, "#A2");
2757                                 Assert.IsNotNull (names [0], "#A3");
2758                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2759                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2760                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2761                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2762                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2763
2764                                 createdKey.SetValue (null, "value2",
2765                                         RegistryValueKind.String);
2766                                 names = createdKey.GetValueNames ();
2767                                 Assert.IsNotNull (names, "#B1");
2768                                 Assert.AreEqual (1, names.Length, "#B2");
2769                                 Assert.IsNotNull (names [0], "#B3");
2770                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2771                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2772                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2773                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2774                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2775                         } finally {
2776                                 // clean-up
2777                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2778                         }
2779                 }
2780
2781                 [Test] // SetValue (String, Object, RegistryValueKind)
2782                 public void SetValue2_Name_MaxLength ()
2783                 {
2784                         string subKeyName = Guid.NewGuid ().ToString ();
2785
2786                         try {
2787                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2788                                         string name = new string ('a', 254);
2789
2790                                         createdKey.SetValue (name, "value1",
2791                                                 RegistryValueKind.String);
2792                                         Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2793                                         createdKey.DeleteValue (name);
2794                                         Assert.IsNull (createdKey.GetValue (name), "#A2");
2795
2796                                         name = new string ('a', 255);
2797
2798                                         createdKey.SetValue (name, "value2",
2799                                                 RegistryValueKind.String);
2800                                         Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2801                                         createdKey.DeleteValue (name);
2802                                         Assert.IsNull (createdKey.GetValue (name), "#B2");
2803
2804                                         name = new string ('a', 256);
2805
2806                                         try {
2807                                                 createdKey.SetValue (name, "value2",
2808                                                         RegistryValueKind.String);
2809                                                 Assert.Fail ("#C1");
2810                                         } catch (ArgumentException ex) {
2811                                                 // Registry subkeys should not be
2812                                                 // greater than 255 characters
2813                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2814                                                 Assert.IsNull (ex.InnerException, "#C3");
2815                                                 Assert.IsNotNull (ex.Message, "#C4");
2816                                                 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2817                                                 Assert.IsNull (ex.ParamName, "#C6");
2818                                         }
2819                                 }
2820                         } finally {
2821                                 try {
2822                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2823                                         if (createdKey != null) {
2824                                                 createdKey.Close ();
2825                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2826                                         }
2827                                 } catch {
2828                                 }
2829                         }
2830                 }
2831
2832                 [Test] // SetValue (String, Object, RegistryValueKind)
2833                 public void SetValue2_Name_Null ()
2834                 {
2835                         string subKeyName = Guid.NewGuid ().ToString ();
2836
2837                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2838                         try {
2839                                 createdKey.SetValue (null, "value1",
2840                                         RegistryValueKind.String);
2841                                 string [] names = createdKey.GetValueNames ();
2842                                 Assert.IsNotNull (names, "#A1");
2843                                 Assert.AreEqual (1, names.Length, "#A2");
2844                                 Assert.IsNotNull (names [0], "#A3");
2845                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2846                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2847                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2848                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2849                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2850
2851                                 createdKey.SetValue (string.Empty, "value2",
2852                                         RegistryValueKind.String);
2853                                 names = createdKey.GetValueNames ();
2854                                 Assert.IsNotNull (names, "#B1");
2855                                 Assert.AreEqual (1, names.Length, "#B2");
2856                                 Assert.IsNotNull (names [0], "#B3");
2857                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2858                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2859                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2860                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2861                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2862                         } finally {
2863                                 // clean-up
2864                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2865                         }
2866                 }
2867
2868                 [Test] // SetValue (String, Object, RegistryValueKind)
2869                 public void SetValue2_Value_Null ()
2870                 {
2871                         string subKeyName = Guid.NewGuid ().ToString ();
2872
2873                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2874                         try {
2875                                 try {
2876                                         createdKey.SetValue ("Name", null,
2877                                                 RegistryValueKind.String);
2878                                         Assert.Fail ("#1");
2879                                 } catch (ArgumentNullException ex) {
2880                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2881                                         Assert.IsNull (ex.InnerException, "#3");
2882                                         Assert.IsNotNull (ex.Message, "#4");
2883                                         Assert.AreEqual ("value", ex.ParamName, "#5");
2884                                 }
2885                         } finally {
2886                                 // clean-up
2887                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2888                         }
2889                 }
2890
2891                 [Test]
2892                 public void SubKeyCount ()
2893                 {
2894                         string subKeyName = Guid.NewGuid ().ToString ();
2895
2896                         try {
2897                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2898                                         // check if key was successfully created
2899                                         Assert.IsNotNull (createdKey, "#A1");
2900                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2901                                                 subKey.Close ();
2902                                         }
2903                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2904                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2905                                                 subKey.Close ();
2906                                         }
2907                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2908                                 }
2909                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2910                                         Assert.IsNotNull (createdKey, "#B1");
2911                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
2912
2913                                         using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2914                                                 Assert.IsNotNull (createdKey2, "#B3");
2915                                                 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
2916                                                 createdKey2.DeleteSubKey ("monotemp1");
2917                                                 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
2918                                         }
2919                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
2920                                 }
2921                         } finally {
2922                                 try {
2923                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2924                                         if (createdKey != null) {
2925                                                 createdKey.Close ();
2926                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2927                                         }
2928                                 } catch {
2929                                 }
2930                         }
2931                 }
2932
2933                 [Test]
2934                 public void SubKeyCount_Key_Removed ()
2935                 {
2936                         string subKeyName = Guid.NewGuid ().ToString ();
2937
2938                         try {
2939                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2940                                         // check if key was successfully created
2941                                         Assert.IsNotNull (createdKey, "#A1");
2942                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2943                                                 subKey.Close ();
2944                                         }
2945                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
2946                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
2947                                                 subKey.Close ();
2948                                         }
2949                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
2950                                 }
2951                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2952                                         Assert.IsNotNull (createdKey, "#B1");
2953                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
2954
2955                                         // remove created key
2956                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2957
2958                                         try {
2959                                                 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
2960                                         } catch (IOException ex) {
2961                                                 // Illegal operation attempted on a registry key that
2962                                                 // has been marked for deletion
2963                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
2964                                                 Assert.IsNotNull (ex.Message, "#15");
2965                                                 Assert.IsNull (ex.InnerException, "#16");
2966                                         }
2967                                 }
2968                         } finally {
2969                                 try {
2970                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2971                                         if (createdKey != null) {
2972                                                 createdKey.Close ();
2973                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2974                                         }
2975                                 } catch {
2976                                 }
2977                         }
2978                 }
2979
2980                 [Test]
2981                 public void ValueCount ()
2982                 {
2983                         string subKeyName = Guid.NewGuid ().ToString ();
2984
2985                         try {
2986                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2987                                         // check if key was successfully created
2988                                         Assert.IsNotNull (createdKey, "#A1");
2989                                         Assert.AreEqual (0, createdKey.ValueCount, "#A2");
2990                                         createdKey.SetValue ("name1", "value1");
2991                                         Assert.AreEqual (1, createdKey.ValueCount, "#A3");
2992                                         createdKey.SetValue ("name2", "value2");
2993                                         Assert.AreEqual (2, createdKey.ValueCount, "#A4");
2994                                         createdKey.SetValue ("name2", "value2b");
2995                                         Assert.AreEqual (2, createdKey.ValueCount, "#A5");
2996                                         createdKey.SetValue ("name3", "value3");
2997                                         Assert.AreEqual (3, createdKey.ValueCount, "#A6");
2998                                 }
2999                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3000                                         Assert.IsNotNull (createdKey, "#B1");
3001                                         Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3002
3003                                         using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3004                                                 Assert.IsNotNull (createdKey2, "#B3");
3005                                                 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
3006                                                 createdKey2.DeleteValue ("name2");
3007                                                 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
3008                                         }
3009                                         Assert.AreEqual (2, createdKey.ValueCount, "#B6");
3010                                 }
3011                         } finally {
3012                                 try {
3013                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3014                                         if (createdKey != null) {
3015                                                 createdKey.Close ();
3016                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3017                                         }
3018                                 } catch {
3019                                 }
3020                         }
3021                 }
3022
3023                 [Test]
3024                 public void ValueCount_Key_Removed ()
3025                 {
3026                         string subKeyName = Guid.NewGuid ().ToString ();
3027
3028                         try {
3029                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3030                                         // check if key was successfully created
3031                                         Assert.IsNotNull (createdKey, "#A1");
3032                                         Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3033                                         createdKey.SetValue ("name1", "value1");
3034                                         Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3035                                         createdKey.SetValue ("name2", "value2");
3036                                         Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3037                                         createdKey.SetValue ("name2", "value2b");
3038                                         Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3039                                         createdKey.SetValue ("name3", "value3");
3040                                         Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3041                                 }
3042                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3043                                         Assert.IsNotNull (createdKey, "#B1");
3044                                         Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3045
3046                                         // remove created key
3047                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3048
3049                                         try {
3050                                                 Assert.Fail ("#C1: " + createdKey.ValueCount);
3051                                         } catch (IOException ex) {
3052                                                 // Illegal operation attempted on a registry key that
3053                                                 // has been marked for deletion
3054                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3055                                                 Assert.IsNotNull (ex.Message, "#15");
3056                                                 Assert.IsNull (ex.InnerException, "#16");
3057                                         }
3058                                 }
3059                         } finally {
3060                                 try {
3061                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3062                                         if (createdKey != null) {
3063                                                 createdKey.Close ();
3064                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3065                                         }
3066                                 } catch {
3067                                 }
3068                         }
3069                 }
3070
3071                 [Test]
3072                 public void bug79051 ()
3073                 {
3074                         string subKeyName = Guid.NewGuid ().ToString ();
3075
3076                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3077                                 try {
3078                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3079                                                 createdKey.SetValue ("test", "whatever");
3080                                                 createdKey.Close ();
3081                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3082                                         }
3083                                 } finally {
3084                                         try {
3085                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3086                                                 if (createdKey != null) {
3087                                                         createdKey.Close ();
3088                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3089                                                 }
3090                                         } catch {
3091                                         }
3092                                 }
3093                         }
3094                 }
3095
3096                 [Test]
3097                 public void bug79059 ()
3098                 {
3099                         string subKeyName = Guid.NewGuid ().ToString ();
3100
3101                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3102                                 try {
3103                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3104                                                 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
3105                                                 }
3106                                                 createdKey.Close ();
3107                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3108                                         }
3109                                 } finally {
3110                                         try {
3111                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3112                                                 if (createdKey != null) {
3113                                                         createdKey.Close ();
3114                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3115                                                 }
3116                                         } catch {
3117                                         }
3118                                 }
3119                         }
3120                 }
3121
3122                 [Test]
3123                 public void bugnew1 ()
3124                 {
3125                         string subKeyName = Guid.NewGuid ().ToString ();
3126
3127                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3128                                 try {
3129                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3130                                                 createdKey.SetValue ("name1", "value1");
3131
3132                                                 RegistryKey testKey = null;
3133                                                 try {
3134                                                         testKey = createdKey.OpenSubKey ("test", true);
3135                                                         if (testKey == null)
3136                                                                 testKey = createdKey.CreateSubKey ("test");
3137                                                         testKey.SetValue ("another", "one");
3138                                                 } finally {
3139                                                         if (testKey != null)
3140                                                                 testKey.Close ();
3141                                                 }
3142
3143                                                 createdKey.SetValue ("name2", "value2");
3144                                                 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
3145                                                 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
3146                                                 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
3147                                                 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
3148
3149                                                 string [] names = createdKey.GetValueNames ();
3150                                                 Assert.IsNotNull (names, "#6");
3151                                                 Assert.AreEqual (2, names.Length, "#7");
3152                                                 Assert.AreEqual ("name1", names [0], "#8");
3153                                                 Assert.AreEqual ("name2", names [1], "#9");
3154
3155                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3156
3157                                                 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
3158                                                         Assert.IsNull (openedKey, "#10");
3159                                                 }
3160
3161                                                 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
3162                                                 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
3163
3164                                                 try {
3165                                                         createdKey.GetValueNames ();
3166                                                         Assert.Fail ("#13");
3167                                                 } catch (IOException ex) {
3168                                                         // Illegal operation attempted on a registry key that
3169                                                         // has been marked for deletion
3170                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3171                                                         Assert.IsNotNull (ex.Message, "#15");
3172                                                         Assert.IsNull (ex.InnerException, "#16");
3173                                                 }
3174
3175                                                 try {
3176                                                         createdKey.SetValue ("name1", "value1");
3177                                                         Assert.Fail ("#17");
3178                                                 } catch (IOException ex) {
3179                                                         // Illegal operation attempted on a registry key that
3180                                                         // has been marked for deletion
3181                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
3182                                                         Assert.IsNotNull (ex.Message, "#19");
3183                                                         Assert.IsNull (ex.InnerException, "#20");
3184                                                 }
3185
3186                                                 try {
3187                                                         createdKey.SetValue ("newname", "value1");
3188                                                         Assert.Fail ("#21");
3189                                                 } catch (IOException ex) {
3190                                                         // Illegal operation attempted on a registry key that
3191                                                         // has been marked for deletion
3192                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
3193                                                         Assert.IsNotNull (ex.Message, "#23");
3194                                                         Assert.IsNull (ex.InnerException, "#24");
3195                                                 }
3196
3197                                                 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
3198                                                 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
3199                                                 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
3200                                                 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
3201
3202                                                 try {
3203                                                         createdKey.CreateSubKey ("new");
3204                                                         Assert.Fail ("#29");
3205                                                 } catch (IOException ex) {
3206                                                         // Illegal operation attempted on a registry key that
3207                                                         // has been marked for deletion
3208                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
3209                                                         Assert.IsNotNull (ex.Message, "#31");
3210                                                         Assert.IsNull (ex.InnerException, "#32");
3211                                                 }
3212                                         }
3213                                 } finally {
3214                                         try {
3215                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3216                                                 if (createdKey != null) {
3217                                                         createdKey.Close ();
3218                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3219                                                 }
3220                                         } catch {
3221                                         }
3222                                 }
3223                         }
3224                 }
3225
3226                 [Test]
3227                 public void bugnew2 () // values cannot be written on registry root (hive)
3228                 {
3229                         string [] names = Registry.CurrentUser.GetValueNames ();
3230                         Assert.IsNotNull (names, "#1");
3231                         Registry.CurrentUser.SetValue ("name1", "value1");
3232                         Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
3233                         Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
3234                         string [] newNames = Registry.CurrentUser.GetValueNames ();
3235                         Assert.IsNotNull (newNames, "#4");
3236                         Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
3237                         Registry.CurrentUser.DeleteValue ("name1");
3238                 }
3239
3240                 [Test]
3241                 public void bugnew3 () // on Windows, key cannot be closed twice
3242                 {
3243                         string subKeyName = Guid.NewGuid ().ToString ();
3244
3245                         try {
3246                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3247                                         createdKey.Close ();
3248                                 }
3249
3250                                 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3251                                 openedKey.Close ();
3252                                 openedKey.Close ();
3253                         } finally {
3254                                 try {
3255                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3256                                         if (createdKey != null) {
3257                                                 createdKey.Close ();
3258                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3259                                         }
3260                                 } catch {
3261                                 }
3262                         }
3263                 }
3264
3265                 [Test]
3266                 public void bugnew4 () // Key cannot be flushed once it has been closed
3267                 {
3268                         string subKeyName = Guid.NewGuid ().ToString ();
3269
3270                         try {
3271                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3272                                         createdKey.Close ();
3273                                 }
3274
3275                                 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3276                                 openedKey.Close ();
3277                                 openedKey.Flush ();
3278                         } finally {
3279                                 try {
3280                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3281                                         if (createdKey != null) {
3282                                                 createdKey.Close ();
3283                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3284                                         }
3285                                 } catch {
3286                                 }
3287                         }
3288                 }
3289
3290                 private bool RunningOnUnix {
3291                         get {
3292                                 int p = (int) Environment.OSVersion.Platform;
3293                                 return ((p == 4) || (p == 128) || (p == 6));
3294                         }
3295                 }
3296         }
3297 }