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