Merge pull request #823 from DavidKarlas/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                 // This hangs on windows
2205                 [Category ("NotWorking")]
2206                 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
2207                 {
2208                         // access to registry of remote machines is not implemented on unix
2209                         if (RunningOnUnix)
2210                                 return;
2211
2212                         try {
2213                                 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
2214                                         "DOESNOTEXIST");
2215                                 Assert.Fail ("#1");
2216                         } catch (IOException ex) {
2217                                 // The network path was not found
2218                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2219                                 Assert.IsNotNull (ex.Message, "#3");
2220                                 Assert.IsNull (ex.InnerException, "#4");
2221                         }
2222                 }
2223
2224                 [Test] // bug #322839
2225                 public void SetValue1_EntityReferences ()
2226                 {
2227                         string subKeyName = Guid.NewGuid ().ToString ();
2228
2229                         try {
2230                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2231                                         // we created a new subkey, so value should not exist
2232                                         Assert.IsNull (createdKey.GetValue ("FirstName&\"<LastName>\""), "#A1");
2233                                         // create value
2234                                         createdKey.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
2235                                         // get value
2236                                         object name = createdKey.GetValue ("FirstName&\"<LastName>\"");
2237                                         // value should exist
2238                                         Assert.IsNotNull (name, "#A2");
2239                                         // type of value should be string
2240                                         Assert.AreEqual (typeof (string), name.GetType (), "#A3");
2241                                         // ensure value matches
2242                                         Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#A4");
2243
2244                                         // we created a new subkey, so value should not exist
2245                                         Assert.IsNull (createdKey.GetValue ("Info"), "#B1");
2246                                         // create value
2247                                         createdKey.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" });
2248                                         // get value
2249                                         object info = createdKey.GetValue ("Info");
2250                                         // value should exist
2251                                         Assert.IsNotNull (info, "#B2");
2252                                         // type of value should be string
2253                                         Assert.AreEqual (typeof (string []), info.GetType (), "#B3");
2254                                         // ensure value matches
2255                                         Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#B4");
2256                                 }
2257
2258                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2259                                         object name = openedKey.GetValue ("FirstName&\"<LastName>\"");
2260                                         Assert.IsNotNull (name, "#C1");
2261                                         Assert.AreEqual (typeof (string), name.GetType (), "#C2");
2262                                         Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#C3");
2263
2264                                         object info = openedKey.GetValue ("Info");
2265                                         Assert.IsNotNull (info, "#D1");
2266                                         Assert.AreEqual (typeof (string []), info.GetType (), "#D2");
2267                                         Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#D3");
2268                                 }
2269                         } finally {
2270                                 // clean-up
2271                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2272                         }
2273                 }
2274
2275                 [Test] // SetValue (String, Object)
2276                 public void SetValue1_Name_Null ()
2277                 {
2278                         string subKeyName = Guid.NewGuid ().ToString ();
2279
2280                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2281                         try {
2282                                 createdKey.SetValue (null, "value1");
2283                                 string [] names = createdKey.GetValueNames ();
2284                                 Assert.IsNotNull (names, "#A1");
2285                                 Assert.AreEqual (1, names.Length, "#A2");
2286                                 Assert.IsNotNull (names [0], "#A3");
2287                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2288                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2289                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2290                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2291                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2292
2293                                 createdKey.SetValue (string.Empty, "value2");
2294                                 names = createdKey.GetValueNames ();
2295                                 Assert.IsNotNull (names, "#B1");
2296                                 Assert.AreEqual (1, names.Length, "#B2");
2297                                 Assert.IsNotNull (names [0], "#B3");
2298                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2299                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2300                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2301                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2302                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2303                         } finally {
2304                                 // clean-up
2305                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2306                         }
2307                 }
2308
2309                 [Test] // SetValue (String, Object)
2310                 public void SetValue1_Name_Empty ()
2311                 {
2312                         string subKeyName = Guid.NewGuid ().ToString ();
2313
2314                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2315                         try {
2316                                 createdKey.SetValue (string.Empty, "value1");
2317                                 string [] names = createdKey.GetValueNames ();
2318                                 Assert.IsNotNull (names, "#A1");
2319                                 Assert.AreEqual (1, names.Length, "#A2");
2320                                 Assert.IsNotNull (names [0], "#A3");
2321                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2322                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2323                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2324                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2325                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2326
2327                                 createdKey.SetValue (null, "value2");
2328                                 names = createdKey.GetValueNames ();
2329                                 Assert.IsNotNull (names, "#B1");
2330                                 Assert.AreEqual (1, names.Length, "#B2");
2331                                 Assert.IsNotNull (names [0], "#B3");
2332                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2333                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2334                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2335                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2336                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2337                         } finally {
2338                                 // clean-up
2339                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2340                         }
2341                 }
2342
2343                 [Test] // SetValue (String, Object)
2344                 public void SetValue1_Name_MaxLength ()
2345                 {
2346                         string subKeyName = Guid.NewGuid ().ToString ();
2347
2348                         try {
2349                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2350                                         string name = new string ('a', 254);
2351
2352                                         createdKey.SetValue (name, "value1");
2353                                         Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2354                                         createdKey.DeleteValue (name);
2355                                         Assert.IsNull (createdKey.GetValue (name), "#A2");
2356
2357                                         name = new string ('a', 255);
2358
2359                                         createdKey.SetValue (name, "value2");
2360                                         Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2361                                         createdKey.DeleteValue (name);
2362                                         Assert.IsNull (createdKey.GetValue (name), "#B2");
2363
2364                                         name = new string ('a', 256);
2365
2366                                         try {
2367                                                 createdKey.SetValue (name, "value2");
2368                                                 Assert.Fail ("#C1");
2369                                         } catch (ArgumentException ex) {
2370                                                 // 1.x: Registry subkeys should not be
2371                                                 // greater than or equal to 255 characters
2372                                                 //
2373                                                 // 2.x: Registry subkeys should not be
2374                                                 // greater than 255 characters
2375                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2376                                                 Assert.IsNull (ex.InnerException, "#C3");
2377                                                 Assert.IsNotNull (ex.Message, "#C4");
2378                                                 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2379                                                 Assert.IsNull (ex.ParamName, "#C6");
2380                                         }
2381                                 }
2382                         } finally {
2383                                 try {
2384                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2385                                         if (createdKey != null) {
2386                                                 createdKey.Close ();
2387                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2388                                         }
2389                                 } catch {
2390                                 }
2391                         }
2392                 }
2393
2394                 [Test] // SetValue (String, Object)
2395                 public void SetValue1_Value_Null ()
2396                 {
2397                         string subKeyName = Guid.NewGuid ().ToString ();
2398
2399                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2400                         try {
2401                                 try {
2402                                         createdKey.SetValue ("Name", null);
2403                                         Assert.Fail ("#1");
2404                                 } catch (ArgumentNullException ex) {
2405                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2406                                         Assert.IsNull (ex.InnerException, "#3");
2407                                         Assert.IsNotNull (ex.Message, "#4");
2408                                         Assert.AreEqual ("value", ex.ParamName, "#5");
2409                                 }
2410                         } finally {
2411                                 // clean-up
2412                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2413                         }
2414                 }
2415
2416                 [Test] // SetValue (String, Object)
2417                 public void SetValue1_Boolean ()
2418                 {
2419                         string subKeyName = Guid.NewGuid ().ToString ();
2420
2421                         try {
2422                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2423                                         // we created a new subkey, so value should not exist
2424                                         Assert.IsNull (createdKey.GetValue ("Installed"), "#A1");
2425                                         // create value
2426                                         createdKey.SetValue ("Installed", true);
2427                                         // get value
2428                                         object value = createdKey.GetValue ("Installed");
2429                                         // value should exist
2430                                         Assert.IsNotNull (value, "#A2");
2431                                         // type of value should be string
2432                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2433                                         // ensure value matches
2434                                         Assert.AreEqual (true.ToString (), value, "#A4");
2435                                 }
2436
2437                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2438                                         object value = openedKey.GetValue ("Installed");
2439                                         Assert.IsNotNull (value, "#B1");
2440                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2441                                         Assert.AreEqual (true.ToString (), value, "#B3");
2442                                 }
2443                         } finally {
2444                                 // clean-up
2445                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2446                         }
2447                 }
2448
2449                 [Test] // SetValue (String, Object)
2450                 public void SetValue1_Byte ()
2451                 {
2452                         string subKeyName = Guid.NewGuid ().ToString ();
2453
2454                         try {
2455                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2456                                         // we created a new subkey, so value should not exist
2457                                         Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2458                                         // create value
2459                                         createdKey.SetValue ("Flags", (byte) 5);
2460                                         // get value
2461                                         object value = createdKey.GetValue ("Flags");
2462                                         // value should exist
2463                                         Assert.IsNotNull (value, "#A2");
2464                                         // type of value should be string
2465                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2466                                         // ensure value matches
2467                                         Assert.AreEqual ("5", value, "#A4");
2468                                 }
2469
2470                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2471                                         object value = openedKey.GetValue ("Flags");
2472                                         Assert.IsNotNull (value, "#B1");
2473                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2474                                         Assert.AreEqual ("5", value, "#B3");
2475                                 }
2476                         } finally {
2477                                 // clean-up
2478                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2479                         }
2480                 }
2481
2482                 [Test] // SetValue (String, Object)
2483                 public void SetValue1_ByteArray ()
2484                 {
2485                         string subKeyName = Guid.NewGuid ().ToString ();
2486
2487                         try {
2488                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2489                                         // we created a new subkey, so value should not exist
2490                                         Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2491                                         // create value
2492                                         createdKey.SetValue ("Flags", new byte [] { 1, 5 });
2493                                         // get value
2494                                         object value = createdKey.GetValue ("Flags");
2495                                         // value should exist
2496                                         Assert.IsNotNull (value, "#A2");
2497                                         // type of value should be string
2498                                         Assert.AreEqual (typeof (byte []), value.GetType (), "#3");
2499                                         // ensure value matches
2500                                         Assert.AreEqual (new byte [] { 1, 5 }, value, "#4");
2501                                 }
2502
2503                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2504                                         object value = openedKey.GetValue ("Flags");
2505                                         Assert.IsNotNull (value, "#B1");
2506                                         Assert.AreEqual (typeof (byte []), value.GetType (), "#B2");
2507                                         Assert.AreEqual (new byte [] { 1, 5 }, value, "#B3");
2508                                 }
2509                         } finally {
2510                                 // clean-up
2511                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2512                         }
2513                 }
2514
2515                 [Test] // SetValue (String, Object)
2516                 public void SetValue1_DateTime ()
2517                 {
2518                         string subKeyName = Guid.NewGuid ().ToString ();
2519
2520                         try {
2521                                 object rawValue = DateTime.Now;
2522
2523                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2524                                         // we created a new subkey, so value should not exist
2525                                         Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2526                                         // create value
2527                                         createdKey.SetValue ("Path", rawValue);
2528                                         // get value
2529                                         object value = createdKey.GetValue ("Path");
2530                                         // value should exist
2531                                         Assert.IsNotNull (value, "#A2");
2532                                         // type of value should be string
2533                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2534                                         // ensure value matches
2535                                         Assert.AreEqual (rawValue.ToString (), value, "#A4");
2536                                 }
2537
2538                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2539                                         object value = openedKey.GetValue ("Path");
2540                                         Assert.IsNotNull (value, "#B1");
2541                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2542                                         Assert.AreEqual (rawValue.ToString (), value, "#B3");
2543                                 }
2544                         } finally {
2545                                 // clean-up
2546                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2547                         }
2548                 }
2549
2550                 [Test]
2551                 public void SetValue_Int32 ()
2552                 {
2553                         string subKeyName = Guid.NewGuid ().ToString ();
2554
2555                         try {
2556                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2557                                         // we created a new subkey, so value should not exist
2558                                         Assert.IsNull (createdKey.GetValue ("RefCount"), "#A1");
2559                                         // create value
2560                                         createdKey.SetValue ("RefCount", 5);
2561                                         // get value
2562                                         object value = createdKey.GetValue ("RefCount");
2563                                         // value should exist
2564                                         Assert.IsNotNull (value, "#A2");
2565                                         // type of value should be int
2566                                         Assert.AreEqual (typeof (int), value.GetType (), "#A3");
2567                                         // ensure value matches
2568                                         Assert.AreEqual (5, value, "#A4");
2569                                 }
2570
2571                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2572                                         object value = openedKey.GetValue ("RefCount");
2573                                         Assert.IsNotNull (value, "#B1");
2574                                         Assert.AreEqual (typeof (int), value.GetType (), "#B2");
2575                                         Assert.AreEqual (5, value, "#B3");
2576                                 }
2577                         } finally {
2578                                 // clean-up
2579                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2580                         }
2581                 }
2582
2583                 [Test] // SetValue (String, Object)
2584                 public void SetValue1_Int64 ()
2585                 {
2586                         string subKeyName = Guid.NewGuid ().ToString ();
2587
2588                         try {
2589                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2590                                         // we created a new subkey, so value should not exist
2591                                         Assert.IsNull (createdKey.GetValue ("Ticks"), "#A1");
2592                                         // create value
2593                                         createdKey.SetValue ("Ticks", 500L);
2594                                         // get value
2595                                         object value = createdKey.GetValue ("Ticks");
2596                                         // value should exist
2597                                         Assert.IsNotNull (value, "#A2");
2598                                         // type of value should be string
2599                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2600                                         // ensure value matches
2601                                         Assert.AreEqual ("500", value, "#A4");
2602                                 }
2603
2604                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2605                                         object value = openedKey.GetValue ("Ticks");
2606                                         Assert.IsNotNull (value, "#B1");
2607                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2608                                         Assert.AreEqual ("500", value, "#B3");
2609                                 }
2610                         } finally {
2611                                 // clean-up
2612                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2613                         }
2614                 }
2615
2616                 [Test] // SetValue (String, Object)
2617                 public void SetValue1_String ()
2618                 {
2619                         string subKeyName = Guid.NewGuid ().ToString ();
2620
2621                         try {
2622                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2623                                         // we created a new subkey, so value should not exist
2624                                         Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2625                                         // create value
2626                                         createdKey.SetValue ("Path", "/usr/lib/whatever");
2627                                         // get value
2628                                         object path = createdKey.GetValue ("Path");
2629                                         // value should exist
2630                                         Assert.IsNotNull (path, "#A2");
2631                                         // type of value should be string
2632                                         Assert.AreEqual (typeof (string), path.GetType (), "#A3");
2633                                         // ensure value matches
2634                                         Assert.AreEqual ("/usr/lib/whatever", path, "#A4");
2635                                 }
2636
2637                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2638                                         object path = openedKey.GetValue ("Path");
2639                                         Assert.IsNotNull (path, "#B1");
2640                                         Assert.AreEqual (typeof (string), path.GetType (), "#B2");
2641                                         Assert.AreEqual ("/usr/lib/whatever", path, "#B3");
2642                                 }
2643                         } finally {
2644                                 // clean-up
2645                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2646                         }
2647                 }
2648
2649                 [Test] // SetValue (String, Object)
2650                 public void SetValue1_StringArray ()
2651                 {
2652                         string subKeyName = Guid.NewGuid ().ToString ();
2653
2654                         try {
2655                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2656                                         // we created a new subkey, so value should not exist
2657                                         Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#A1");
2658                                         // create value
2659                                         createdKey.SetValue ("DependsOnGroup", new string [] { "A", "B" });
2660                                         // get value
2661                                         object value = createdKey.GetValue ("DependsOnGroup");
2662                                         // value should exist
2663                                         Assert.IsNotNull (value, "#A2");
2664                                         // type of value should be string
2665                                         Assert.AreEqual (typeof (string []), value.GetType (), "#A3");
2666                                         // ensure value matches
2667                                         Assert.AreEqual (new string [] { "A", "B" }, value, "#A4");
2668                                 }
2669
2670                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2671                                         object value = openedKey.GetValue ("DependsOnGroup");
2672                                         Assert.IsNotNull (value, "#B1");
2673                                         Assert.AreEqual (typeof (string []), value.GetType (), "#B2");
2674                                         Assert.AreEqual (new string [] { "A", "B" }, value, "#B3");
2675                                 }
2676                         } finally {
2677                                 // clean-up
2678                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2679                         }
2680                 }
2681
2682                 [Test] // SetValue (String, Object)
2683                 public void SetValue1_Key_ReadOnly ()
2684                 {
2685                         string subKeyName = Guid.NewGuid ().ToString ();
2686
2687                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2688                                 try {
2689                                         softwareKey.SetValue ("name1", "value1");
2690                                         Assert.Fail ("#1");
2691                                 } catch (UnauthorizedAccessException ex) {
2692                                         // Cannot write to the registry key
2693                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2694                                         Assert.IsNotNull (ex.Message, "#3");
2695                                         Assert.IsNull (ex.InnerException, "#4");
2696                                 }
2697                         }
2698
2699                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2700                                 try {
2701                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2702                                         }
2703
2704                                         using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2705                                                 try {
2706                                                         createdKey.SetValue ("name1", "value1");
2707                                                         Assert.Fail ("#1");
2708                                                 } catch (UnauthorizedAccessException ex) {
2709                                                         // Cannot write to the registry key
2710                                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2711                                                         Assert.IsNotNull (ex.Message, "#3");
2712                                                         Assert.IsNull (ex.InnerException, "#4");
2713                                                 }
2714                                         }
2715                                 } finally {
2716                                         try {
2717                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2718                                                 if (createdKey != null) {
2719                                                         createdKey.Close ();
2720                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2721                                                 }
2722                                         } catch {
2723                                         }
2724                                 }
2725                         }
2726                 }
2727
2728                 [Test] // SetValue (String, Object)
2729                 public void SetValue1_Key_Removed ()
2730                 {
2731                         string subKeyName = Guid.NewGuid ().ToString ();
2732
2733                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2734                                 try {
2735                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2736                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2737                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2738                                                 try {
2739                                                         createdKey.SetValue ("name1", "value1");
2740                                                         Assert.Fail ("#2");
2741                                                 } catch (IOException ex) {
2742                                                         // Illegal operation attempted on a registry key that
2743                                                         // has been marked for deletion
2744                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2745                                                         Assert.IsNotNull (ex.Message, "#4");
2746                                                         Assert.IsNull (ex.InnerException, "#5");
2747                                                 }
2748                                         }
2749                                 } finally {
2750                                         try {
2751                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2752                                                 if (createdKey != null) {
2753                                                         createdKey.Close ();
2754                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2755                                                 }
2756                                         } catch {
2757                                         }
2758                                 }
2759                         }
2760                 }
2761
2762                 [Test] // SetValue (String, Object, RegistryValueKind)
2763                 public void SetValue2_Key_ReadOnly ()
2764                 {
2765                         string subKeyName = Guid.NewGuid ().ToString ();
2766
2767                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2768                                 try {
2769                                         softwareKey.SetValue ("name1", "value1",
2770                                                 RegistryValueKind.String);
2771                                         Assert.Fail ("#1");
2772                                 } catch (UnauthorizedAccessException ex) {
2773                                         // Cannot write to the registry key
2774                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2775                                         Assert.IsNotNull (ex.Message, "#3");
2776                                         Assert.IsNull (ex.InnerException, "#4");
2777                                 }
2778                         }
2779
2780                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2781                                 try {
2782                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2783                                         }
2784
2785                                         using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2786                                                 try {
2787                                                         createdKey.SetValue ("name1", "value1",
2788                                                                 RegistryValueKind.String);
2789                                                         Assert.Fail ("#1");
2790                                                 } catch (UnauthorizedAccessException ex) {
2791                                                         // Cannot write to the registry key
2792                                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2793                                                         Assert.IsNotNull (ex.Message, "#3");
2794                                                         Assert.IsNull (ex.InnerException, "#4");
2795                                                 }
2796                                         }
2797                                 } finally {
2798                                         try {
2799                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2800                                                 if (createdKey != null) {
2801                                                         createdKey.Close ();
2802                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2803                                                 }
2804                                         } catch {
2805                                         }
2806                                 }
2807                         }
2808                 }
2809
2810                 [Test] // SetValue (String, Object, RegistryValueKind)
2811                 public void SetValue2_Key_Removed ()
2812                 {
2813                         string subKeyName = Guid.NewGuid ().ToString ();
2814
2815                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2816                                 try {
2817                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2818                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2819                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2820                                                 try {
2821                                                         createdKey.SetValue ("name1", "value1",
2822                                                                 RegistryValueKind.String);
2823                                                         Assert.Fail ("#2");
2824                                                 } catch (IOException ex) {
2825                                                         // Illegal operation attempted on a registry key that
2826                                                         // has been marked for deletion
2827                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2828                                                         Assert.IsNotNull (ex.Message, "#4");
2829                                                         Assert.IsNull (ex.InnerException, "#5");
2830                                                 }
2831                                         }
2832                                 } finally {
2833                                         try {
2834                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2835                                                 if (createdKey != null) {
2836                                                         createdKey.Close ();
2837                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2838                                                 }
2839                                         } catch {
2840                                         }
2841                                 }
2842                         }
2843                 }
2844
2845                 [Test] // SetValue (String, Object, RegistryValueKind)
2846                 public void SetValue2_Name_Empty ()
2847                 {
2848                         string subKeyName = Guid.NewGuid ().ToString ();
2849
2850                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2851                         try {
2852                                 createdKey.SetValue (string.Empty, "value1",
2853                                         RegistryValueKind.String);
2854                                 string [] names = createdKey.GetValueNames ();
2855                                 Assert.IsNotNull (names, "#A1");
2856                                 Assert.AreEqual (1, names.Length, "#A2");
2857                                 Assert.IsNotNull (names [0], "#A3");
2858                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2859                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2860                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2861                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2862                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2863
2864                                 createdKey.SetValue (null, "value2",
2865                                         RegistryValueKind.String);
2866                                 names = createdKey.GetValueNames ();
2867                                 Assert.IsNotNull (names, "#B1");
2868                                 Assert.AreEqual (1, names.Length, "#B2");
2869                                 Assert.IsNotNull (names [0], "#B3");
2870                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2871                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2872                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2873                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2874                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2875                         } finally {
2876                                 // clean-up
2877                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2878                         }
2879                 }
2880
2881                 [Test] // SetValue (String, Object, RegistryValueKind)
2882                 public void SetValue2_Name_MaxLength ()
2883                 {
2884                         string subKeyName = Guid.NewGuid ().ToString ();
2885
2886                         try {
2887                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2888                                         string name = new string ('a', 254);
2889
2890                                         createdKey.SetValue (name, "value1",
2891                                                 RegistryValueKind.String);
2892                                         Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2893                                         createdKey.DeleteValue (name);
2894                                         Assert.IsNull (createdKey.GetValue (name), "#A2");
2895
2896                                         name = new string ('a', 255);
2897
2898                                         createdKey.SetValue (name, "value2",
2899                                                 RegistryValueKind.String);
2900                                         Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2901                                         createdKey.DeleteValue (name);
2902                                         Assert.IsNull (createdKey.GetValue (name), "#B2");
2903
2904                                         name = new string ('a', 256);
2905
2906                                         try {
2907                                                 createdKey.SetValue (name, "value2",
2908                                                         RegistryValueKind.String);
2909                                                 Assert.Fail ("#C1");
2910                                         } catch (ArgumentException ex) {
2911                                                 // Registry subkeys should not be
2912                                                 // greater than 255 characters
2913                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2914                                                 Assert.IsNull (ex.InnerException, "#C3");
2915                                                 Assert.IsNotNull (ex.Message, "#C4");
2916                                                 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2917                                                 Assert.IsNull (ex.ParamName, "#C6");
2918                                         }
2919                                 }
2920                         } finally {
2921                                 try {
2922                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2923                                         if (createdKey != null) {
2924                                                 createdKey.Close ();
2925                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2926                                         }
2927                                 } catch {
2928                                 }
2929                         }
2930                 }
2931
2932                 [Test] // SetValue (String, Object, RegistryValueKind)
2933                 public void SetValue2_Name_Null ()
2934                 {
2935                         string subKeyName = Guid.NewGuid ().ToString ();
2936
2937                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2938                         try {
2939                                 createdKey.SetValue (null, "value1",
2940                                         RegistryValueKind.String);
2941                                 string [] names = createdKey.GetValueNames ();
2942                                 Assert.IsNotNull (names, "#A1");
2943                                 Assert.AreEqual (1, names.Length, "#A2");
2944                                 Assert.IsNotNull (names [0], "#A3");
2945                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2946                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2947                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2948                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2949                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2950
2951                                 createdKey.SetValue (string.Empty, "value2",
2952                                         RegistryValueKind.String);
2953                                 names = createdKey.GetValueNames ();
2954                                 Assert.IsNotNull (names, "#B1");
2955                                 Assert.AreEqual (1, names.Length, "#B2");
2956                                 Assert.IsNotNull (names [0], "#B3");
2957                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2958                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2959                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2960                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2961                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2962                         } finally {
2963                                 // clean-up
2964                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2965                         }
2966                 }
2967
2968                 [Test] // SetValue (String, Object, RegistryValueKind)
2969                 public void SetValue2_Value_Null ()
2970                 {
2971                         string subKeyName = Guid.NewGuid ().ToString ();
2972
2973                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2974                         try {
2975                                 try {
2976                                         createdKey.SetValue ("Name", null,
2977                                                 RegistryValueKind.String);
2978                                         Assert.Fail ("#1");
2979                                 } catch (ArgumentNullException ex) {
2980                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2981                                         Assert.IsNull (ex.InnerException, "#3");
2982                                         Assert.IsNotNull (ex.Message, "#4");
2983                                         Assert.AreEqual ("value", ex.ParamName, "#5");
2984                                 }
2985                         } finally {
2986                                 // clean-up
2987                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2988                         }
2989                 }
2990
2991                 [Test]
2992                 public void SubKeyCount ()
2993                 {
2994                         string subKeyName = Guid.NewGuid ().ToString ();
2995
2996                         try {
2997                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2998                                         // check if key was successfully created
2999                                         Assert.IsNotNull (createdKey, "#A1");
3000                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3001                                                 subKey.Close ();
3002                                         }
3003                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3004                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3005                                                 subKey.Close ();
3006                                         }
3007                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3008                                 }
3009                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3010                                         Assert.IsNotNull (createdKey, "#B1");
3011                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3012
3013                                         using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3014                                                 Assert.IsNotNull (createdKey2, "#B3");
3015                                                 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
3016                                                 createdKey2.DeleteSubKey ("monotemp1");
3017                                                 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
3018                                         }
3019                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
3020                                 }
3021                         } finally {
3022                                 try {
3023                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3024                                         if (createdKey != null) {
3025                                                 createdKey.Close ();
3026                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3027                                         }
3028                                 } catch {
3029                                 }
3030                         }
3031                 }
3032
3033                 [Test]
3034                 public void SubKeyCount_Key_Removed ()
3035                 {
3036                         string subKeyName = Guid.NewGuid ().ToString ();
3037
3038                         try {
3039                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3040                                         // check if key was successfully created
3041                                         Assert.IsNotNull (createdKey, "#A1");
3042                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3043                                                 subKey.Close ();
3044                                         }
3045                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3046                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3047                                                 subKey.Close ();
3048                                         }
3049                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3050                                 }
3051                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3052                                         Assert.IsNotNull (createdKey, "#B1");
3053                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3054
3055                                         // remove created key
3056                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3057
3058                                         try {
3059                                                 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
3060                                         } catch (IOException ex) {
3061                                                 // Illegal operation attempted on a registry key that
3062                                                 // has been marked for deletion
3063                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3064                                                 Assert.IsNotNull (ex.Message, "#15");
3065                                                 Assert.IsNull (ex.InnerException, "#16");
3066                                         }
3067                                 }
3068                         } finally {
3069                                 try {
3070                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3071                                         if (createdKey != null) {
3072                                                 createdKey.Close ();
3073                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3074                                         }
3075                                 } catch {
3076                                 }
3077                         }
3078                 }
3079
3080                 [Test]
3081                 public void ValueCount ()
3082                 {
3083                         string subKeyName = Guid.NewGuid ().ToString ();
3084
3085                         try {
3086                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3087                                         // check if key was successfully created
3088                                         Assert.IsNotNull (createdKey, "#A1");
3089                                         Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3090                                         createdKey.SetValue ("name1", "value1");
3091                                         Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3092                                         createdKey.SetValue ("name2", "value2");
3093                                         Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3094                                         createdKey.SetValue ("name2", "value2b");
3095                                         Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3096                                         createdKey.SetValue ("name3", "value3");
3097                                         Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3098                                 }
3099                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3100                                         Assert.IsNotNull (createdKey, "#B1");
3101                                         Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3102
3103                                         using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3104                                                 Assert.IsNotNull (createdKey2, "#B3");
3105                                                 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
3106                                                 createdKey2.DeleteValue ("name2");
3107                                                 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
3108                                         }
3109                                         Assert.AreEqual (2, createdKey.ValueCount, "#B6");
3110                                 }
3111                         } finally {
3112                                 try {
3113                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3114                                         if (createdKey != null) {
3115                                                 createdKey.Close ();
3116                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3117                                         }
3118                                 } catch {
3119                                 }
3120                         }
3121                 }
3122
3123                 [Test]
3124                 public void ValueCount_Key_Removed ()
3125                 {
3126                         string subKeyName = Guid.NewGuid ().ToString ();
3127
3128                         try {
3129                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3130                                         // check if key was successfully created
3131                                         Assert.IsNotNull (createdKey, "#A1");
3132                                         Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3133                                         createdKey.SetValue ("name1", "value1");
3134                                         Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3135                                         createdKey.SetValue ("name2", "value2");
3136                                         Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3137                                         createdKey.SetValue ("name2", "value2b");
3138                                         Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3139                                         createdKey.SetValue ("name3", "value3");
3140                                         Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3141                                 }
3142                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3143                                         Assert.IsNotNull (createdKey, "#B1");
3144                                         Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3145
3146                                         // remove created key
3147                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3148
3149                                         try {
3150                                                 Assert.Fail ("#C1: " + createdKey.ValueCount);
3151                                         } catch (IOException ex) {
3152                                                 // Illegal operation attempted on a registry key that
3153                                                 // has been marked for deletion
3154                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3155                                                 Assert.IsNotNull (ex.Message, "#15");
3156                                                 Assert.IsNull (ex.InnerException, "#16");
3157                                         }
3158                                 }
3159                         } finally {
3160                                 try {
3161                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3162                                         if (createdKey != null) {
3163                                                 createdKey.Close ();
3164                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3165                                         }
3166                                 } catch {
3167                                 }
3168                         }
3169                 }
3170
3171                 [Test]
3172                 public void bug79051 ()
3173                 {
3174                         string subKeyName = Guid.NewGuid ().ToString ();
3175
3176                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3177                                 try {
3178                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3179                                                 createdKey.SetValue ("test", "whatever");
3180                                                 createdKey.Close ();
3181                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3182                                         }
3183                                 } finally {
3184                                         try {
3185                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3186                                                 if (createdKey != null) {
3187                                                         createdKey.Close ();
3188                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3189                                                 }
3190                                         } catch {
3191                                         }
3192                                 }
3193                         }
3194                 }
3195
3196                 // Bug Xamarin 3632
3197                 [Test]
3198                 public void TypeCastTests ()
3199                 {
3200                         string subKeyName = Guid.NewGuid ().ToString ();
3201                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3202                                 try {
3203                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3204                                                 createdKey.SetValue ("test-int", (int) 1, RegistryValueKind.DWord);
3205                                                 createdKey.SetValue ("test-uint", (uint) 1, RegistryValueKind.DWord);
3206                                                 createdKey.SetValue ("test-byte", (byte) 1, RegistryValueKind.DWord);
3207                                                 createdKey.SetValue ("test-sbyte", (sbyte) 1, RegistryValueKind.DWord);
3208                                                 createdKey.SetValue ("test-short", (short) 1, RegistryValueKind.DWord);
3209                                                 createdKey.SetValue ("test-ushort", (ushort) 1, RegistryValueKind.DWord);
3210                                                 createdKey.SetValue ("test-long", (long) 1, RegistryValueKind.DWord);
3211                                                 createdKey.SetValue ("test-ulong", (ulong) 1, RegistryValueKind.DWord);
3212                                                 createdKey.SetValue ("test-decimal", (decimal) 1, RegistryValueKind.DWord);
3213                                                 createdKey.SetValue ("test-float", (float) 1, RegistryValueKind.DWord);
3214                                                 createdKey.SetValue ("test-bool", true, RegistryValueKind.DWord);
3215
3216                                                 createdKey.SetValue ("dtest-int", (int) 1, RegistryValueKind.QWord);
3217                                                 createdKey.SetValue ("dtest-uint", (uint) 1, RegistryValueKind.QWord);
3218                                                 createdKey.SetValue ("dtest-byte", (byte) 1, RegistryValueKind.QWord);
3219                                                 createdKey.SetValue ("dtest-sbyte", (sbyte) 1, RegistryValueKind.QWord);
3220                                                 createdKey.SetValue ("dtest-short", (short) 1, RegistryValueKind.QWord);
3221                                                 createdKey.SetValue ("dtest-ushort", (ushort) 1, RegistryValueKind.QWord);
3222                                                 createdKey.SetValue ("dtest-long", (long) 1, RegistryValueKind.QWord);
3223                                                 createdKey.SetValue ("dtest-ulong", (ulong) 1, RegistryValueKind.QWord);
3224                                                 createdKey.SetValue ("dtest-decimal", (decimal) 1, RegistryValueKind.QWord);
3225                                                 createdKey.SetValue ("dtest-float", (float) 1, RegistryValueKind.QWord);
3226                                                 createdKey.SetValue ("dtest-bool", true, RegistryValueKind.QWord);
3227
3228                                                 object r = createdKey.GetValue ("test-int");
3229                                                 Assert.AreEqual (r is int, true);
3230                                                 Assert.AreEqual ((int) r, 1);
3231                                                 
3232                                                 r = createdKey.GetValue ("test-uint");
3233                                                 Assert.AreEqual (r is int, true);
3234                                                 Assert.AreEqual ((int) r, 1);
3235                                                 r = createdKey.GetValue ("test-byte");
3236                                                 Assert.AreEqual (r is int, true);
3237                                                 Assert.AreEqual ((int) r, 1);
3238                                                 r = createdKey.GetValue ("test-sbyte");
3239                                                 Assert.AreEqual (r is int, true);
3240                                                 Assert.AreEqual ((int) r, 1);
3241                                                 r = createdKey.GetValue ("test-short");
3242                                                 Assert.AreEqual (r is int, true);
3243                                                 Assert.AreEqual ((int) r, 1);
3244                                                 r = createdKey.GetValue ("test-ushort");
3245                                                 Assert.AreEqual (r is int, true);
3246                                                 Assert.AreEqual ((int) r, 1);
3247                                                 r = createdKey.GetValue ("test-long");
3248                                                 Assert.AreEqual (r is int, true);
3249                                                 Assert.AreEqual ((int) r, 1);
3250                                                 r = createdKey.GetValue ("test-ulong");
3251                                                 Assert.AreEqual (r is int, true);
3252                                                 Assert.AreEqual ((int) r, 1);
3253
3254                                                 r = createdKey.GetValue ("dtest-int");
3255                                                 Assert.AreEqual (r is long, true);
3256                                                 Assert.AreEqual ((long) r, 1);
3257                                                 r = createdKey.GetValue ("dtest-uint");
3258                                                 Assert.AreEqual (r is long, true);
3259                                                 Assert.AreEqual ((long) r, 1);
3260                                                 r = createdKey.GetValue ("dtest-byte");
3261                                                 Assert.AreEqual (r is long, true);
3262                                                 Assert.AreEqual ((long) r, 1);
3263                                                 r = createdKey.GetValue ("dtest-sbyte");
3264                                                 Assert.AreEqual (r is long, true);
3265                                                 Assert.AreEqual ((long) r, 1);
3266                                                 r = createdKey.GetValue ("dtest-short");
3267                                                 Assert.AreEqual (r is long, true);
3268                                                 Assert.AreEqual ((long) r, 1);
3269                                                 r = createdKey.GetValue ("dtest-ushort");
3270                                                 Assert.AreEqual (r is long, true);
3271                                                 Assert.AreEqual ((long) r, 1);
3272                                                 r = createdKey.GetValue ("dtest-long");
3273                                                 Assert.AreEqual (r is long, true);
3274                                                 Assert.AreEqual ((long) r, 1);
3275                                                 r = createdKey.GetValue ("dtest-ulong");
3276                                                 Assert.AreEqual (r is long, true);
3277                                                 Assert.AreEqual ((long) r, 1);
3278                                                 r = createdKey.GetValue ("dtest-decimal");
3279                                                 Assert.IsTrue (r is long);
3280                                                 Assert.AreEqual ((long) r, 1);
3281                                                 r = createdKey.GetValue ("dtest-float");
3282                                                 Assert.IsTrue (r is long);
3283                                                 Assert.AreEqual ((long) r, 1);
3284                                                 r = createdKey.GetValue ("dtest-bool");
3285                                                 Assert.AreEqual (typeof (long), r.GetType ());
3286
3287                                                 try {
3288                                                         createdKey.SetValue ("test-int", uint.MaxValue, RegistryValueKind.DWord);
3289                                                         Assert.Fail ("#100");
3290
3291                                                         createdKey.SetValue ("test-int", ulong.MaxValue, RegistryValueKind.QWord);
3292                                                         Assert.Fail ("#101");
3293                                                 } catch (ArgumentException) {
3294                                                 }
3295                                                 
3296                                                 createdKey.Close ();
3297                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3298                                         }
3299                                 } finally {
3300                                         try {
3301                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3302                                                 if (createdKey != null) {
3303                                                         createdKey.Close ();
3304                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3305                                                 }
3306                                         } catch {
3307                                         }
3308                                 }
3309                         }
3310                 }
3311                 
3312                 [Test]
3313                 public void bug79059 ()
3314                 {
3315                         string subKeyName = Guid.NewGuid ().ToString ();
3316
3317                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3318                                 try {
3319                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3320                                                 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
3321                                                 }
3322                                                 createdKey.Close ();
3323                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3324                                         }
3325                                 } finally {
3326                                         try {
3327                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3328                                                 if (createdKey != null) {
3329                                                         createdKey.Close ();
3330                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3331                                                 }
3332                                         } catch {
3333                                         }
3334                                 }
3335                         }
3336                 }
3337
3338                 [Test]
3339                 public void bugnew1 ()
3340                 {
3341                         string subKeyName = Guid.NewGuid ().ToString ();
3342
3343                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3344                                 try {
3345                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3346                                                 createdKey.SetValue ("name1", "value1");
3347
3348                                                 RegistryKey testKey = null;
3349                                                 try {
3350                                                         testKey = createdKey.OpenSubKey ("test", true);
3351                                                         if (testKey == null)
3352                                                                 testKey = createdKey.CreateSubKey ("test");
3353                                                         testKey.SetValue ("another", "one");
3354                                                 } finally {
3355                                                         if (testKey != null)
3356                                                                 testKey.Close ();
3357                                                 }
3358
3359                                                 createdKey.SetValue ("name2", "value2");
3360                                                 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
3361                                                 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
3362                                                 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
3363                                                 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
3364
3365                                                 string [] names = createdKey.GetValueNames ();
3366                                                 Assert.IsNotNull (names, "#6");
3367                                                 Assert.AreEqual (2, names.Length, "#7");
3368                                                 Assert.AreEqual ("name1", names [0], "#8");
3369                                                 Assert.AreEqual ("name2", names [1], "#9");
3370
3371                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3372
3373                                                 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
3374                                                         Assert.IsNull (openedKey, "#10");
3375                                                 }
3376
3377                                                 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
3378                                                 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
3379
3380                                                 try {
3381                                                         createdKey.GetValueNames ();
3382                                                         Assert.Fail ("#13");
3383                                                 } catch (IOException ex) {
3384                                                         // Illegal operation attempted on a registry key that
3385                                                         // has been marked for deletion
3386                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3387                                                         Assert.IsNotNull (ex.Message, "#15");
3388                                                         Assert.IsNull (ex.InnerException, "#16");
3389                                                 }
3390
3391                                                 try {
3392                                                         createdKey.SetValue ("name1", "value1");
3393                                                         Assert.Fail ("#17");
3394                                                 } catch (IOException ex) {
3395                                                         // Illegal operation attempted on a registry key that
3396                                                         // has been marked for deletion
3397                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
3398                                                         Assert.IsNotNull (ex.Message, "#19");
3399                                                         Assert.IsNull (ex.InnerException, "#20");
3400                                                 }
3401
3402                                                 try {
3403                                                         createdKey.SetValue ("newname", "value1");
3404                                                         Assert.Fail ("#21");
3405                                                 } catch (IOException ex) {
3406                                                         // Illegal operation attempted on a registry key that
3407                                                         // has been marked for deletion
3408                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
3409                                                         Assert.IsNotNull (ex.Message, "#23");
3410                                                         Assert.IsNull (ex.InnerException, "#24");
3411                                                 }
3412
3413                                                 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
3414                                                 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
3415                                                 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
3416                                                 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
3417
3418                                                 try {
3419                                                         createdKey.CreateSubKey ("new");
3420                                                         Assert.Fail ("#29");
3421                                                 } catch (IOException ex) {
3422                                                         // Illegal operation attempted on a registry key that
3423                                                         // has been marked for deletion
3424                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
3425                                                         Assert.IsNotNull (ex.Message, "#31");
3426                                                         Assert.IsNull (ex.InnerException, "#32");
3427                                                 }
3428                                         }
3429                                 } finally {
3430                                         try {
3431                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3432                                                 if (createdKey != null) {
3433                                                         createdKey.Close ();
3434                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3435                                                 }
3436                                         } catch {
3437                                         }
3438                                 }
3439                         }
3440                 }
3441
3442                 [Test]
3443                 public void bugnew2 () // values cannot be written on registry root (hive)
3444                 {
3445                         string [] names = Registry.CurrentUser.GetValueNames ();
3446                         Assert.IsNotNull (names, "#1");
3447                         Registry.CurrentUser.SetValue ("name1", "value1");
3448                         Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
3449                         Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
3450                         string [] newNames = Registry.CurrentUser.GetValueNames ();
3451                         Assert.IsNotNull (newNames, "#4");
3452                         Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
3453                         Registry.CurrentUser.DeleteValue ("name1");
3454                 }
3455
3456                 [Test]
3457                 public void bugnew3 () // on Windows, key cannot be closed twice
3458                 {
3459                         string subKeyName = Guid.NewGuid ().ToString ();
3460
3461                         try {
3462                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3463                                         createdKey.Close ();
3464                                 }
3465
3466                                 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3467                                 openedKey.Close ();
3468                                 openedKey.Close ();
3469                         } finally {
3470                                 try {
3471                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3472                                         if (createdKey != null) {
3473                                                 createdKey.Close ();
3474                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3475                                         }
3476                                 } catch {
3477                                 }
3478                         }
3479                 }
3480
3481                 [Test]
3482                 public void bugnew4 () // Key cannot be flushed once it has been closed
3483                 {
3484                         string subKeyName = Guid.NewGuid ().ToString ();
3485
3486                         try {
3487                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3488                                         createdKey.Close ();
3489                                 }
3490
3491                                 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3492                                 openedKey.Close ();
3493                                 openedKey.Flush ();
3494                         } finally {
3495                                 try {
3496                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3497                                         if (createdKey != null) {
3498                                                 createdKey.Close ();
3499                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3500                                         }
3501                                 } catch {
3502                                 }
3503                         }
3504                 }
3505
3506                 private bool RunningOnUnix {
3507                         get {
3508                                 int p = (int) Environment.OSVersion.Platform;
3509                                 return ((p == 4) || (p == 128) || (p == 6));
3510                         }
3511                 }
3512         }
3513 }