[System] UriKind.RelativeOrAbsolute workaround.
[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                                 Assert.Ignore ("Running on Unix.");
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                                 Assert.Ignore ("Running on Unix.");
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                                 Assert.Ignore ("Running on Unix.");
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                                 subKeyName = new string ('a', 256);
1255
1256                                 try {
1257                                         softwareKey.DeleteSubKeyTree (subKeyName);
1258                                         Assert.Fail ("#B1");
1259                                 } catch (ArgumentException ex) {
1260                                         // 1.x: Registry subkeys should not be
1261                                         // greater than or equal to 255 characters
1262                                         //
1263                                         // 2.x: Registry subkeys should not be
1264                                         // greater than 255 characters
1265                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1266                                         Assert.IsNull (ex.InnerException, "#B3");
1267                                         Assert.IsNotNull (ex.Message, "#B4");
1268                                         Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#B5");
1269                                         Assert.IsNull (ex.ParamName, "#B6");
1270                                 }
1271                         }
1272                 }
1273
1274                 [Test]
1275                 public void DeleteSubKeyTree_Name_Null ()
1276                 {
1277                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
1278                                 try {
1279                                         softwareKey.DeleteSubKeyTree (null);
1280                                         Assert.Fail ("#1");
1281                                 } catch (ArgumentNullException ex) {
1282                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1283                                         Assert.IsNull (ex.InnerException, "#3");
1284                                         Assert.IsNotNull (ex.Message, "#4");
1285                                         Assert.AreEqual ("name", ex.ParamName, "#5");
1286                                 }
1287                         }
1288                 }
1289
1290                 [Test]
1291                 public void DeleteValue ()
1292                 {
1293                         string subKeyName = Guid.NewGuid ().ToString ();
1294
1295                         try {
1296                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1297                                         // check if key was successfully created
1298                                         Assert.IsNotNull (createdKey, "#A1");
1299                                         createdKey.SetValue ("name1", "value1");
1300                                         createdKey.SetValue ("name2", "value2");
1301                                         string [] names = createdKey.GetValueNames ();
1302                                         Assert.IsNotNull (names, "#A2");
1303                                         Assert.AreEqual (2, names.Length, "#A3");
1304                                         Assert.IsNotNull (names [0], "#A4");
1305                                         Assert.AreEqual ("name1", names [0], "#A5");
1306                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A6");
1307                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A7");
1308                                         Assert.AreEqual ("name2", names [1], "#A8");
1309                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#A9");
1310                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#A10");
1311                                 }
1312                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1313                                         Assert.IsNotNull (createdKey, "#B1");
1314                                         createdKey.DeleteValue ("name1");
1315                                         string [] names = createdKey.GetValueNames ();
1316                                         Assert.IsNotNull (names, "#B2");
1317                                         Assert.AreEqual (1, names.Length, "#B3");
1318                                         Assert.IsNotNull (names [0], "#B4");
1319                                         Assert.AreEqual ("name2", names [0], "#B5");
1320                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#B6");
1321                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#B7");
1322                                         createdKey.DeleteValue (new string ('a', 400), false);
1323                                 }
1324                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1325                                         string [] names = createdKey.GetValueNames ();
1326                                         Assert.IsNotNull (names, "#C1");
1327                                         Assert.AreEqual (1, names.Length, "#C2");
1328                                         Assert.IsNotNull (names [0], "#C3");
1329                                         Assert.AreEqual ("name2", names [0], "#C4");
1330                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#C5");
1331                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#C6");
1332                                 }
1333                         } finally {
1334                                 try {
1335                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1336                                         if (createdKey != null) {
1337                                                 createdKey.Close ();
1338                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1339                                         }
1340                                 } catch {
1341                                 }
1342                         }
1343                 }
1344
1345                 [Test]
1346                 public void DeleteValue_Key_ReadOnly ()
1347                 {
1348                         string subKeyName = Guid.NewGuid ().ToString ();
1349
1350                         try {
1351                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1352                                         createdKey.SetValue ("name1", "value1");
1353                                 }
1354
1355                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1356                                         try {
1357                                                 // deleting value that exists
1358                                                 createdKey.DeleteValue ("name1");
1359                                                 Assert.Fail ("#A1");
1360                                         } catch (UnauthorizedAccessException ex) {
1361                                                 // Cannot write to the registry key
1362                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#A2");
1363                                                 Assert.IsNotNull (ex.Message, "#A3");
1364                                                 Assert.IsNull (ex.InnerException, "#A4");
1365                                         }
1366
1367                                         try {
1368                                                 // deleting value that exists
1369                                                 createdKey.DeleteValue ("name1", true);
1370                                                 Assert.Fail ("#B1");
1371                                         } catch (UnauthorizedAccessException ex) {
1372                                                 // Cannot write to the registry key
1373                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#B2");
1374                                                 Assert.IsNotNull (ex.Message, "#B3");
1375                                                 Assert.IsNull (ex.InnerException, "#B4");
1376                                         }
1377
1378                                         try {
1379                                                 // deleting value that exists
1380                                                 createdKey.DeleteValue ("name1", false);
1381                                                 Assert.Fail ("#C1");
1382                                         } catch (UnauthorizedAccessException ex) {
1383                                                 // Cannot write to the registry key
1384                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#C2");
1385                                                 Assert.IsNotNull (ex.Message, "#C3");
1386                                                 Assert.IsNull (ex.InnerException, "#C4");
1387                                         }
1388
1389                                         try {
1390                                                 // deleting value that does not exist
1391                                                 createdKey.DeleteValue ("name2");
1392                                                 Assert.Fail ("#D1");
1393                                         } catch (UnauthorizedAccessException ex) {
1394                                                 // Cannot write to the registry key
1395                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#D2");
1396                                                 Assert.IsNotNull (ex.Message, "#D3");
1397                                                 Assert.IsNull (ex.InnerException, "#D4");
1398                                         }
1399
1400                                         try {
1401                                                 // deleting value that does not exist
1402                                                 createdKey.DeleteValue ("name2", true);
1403                                                 Assert.Fail ("#E1");
1404                                         } catch (UnauthorizedAccessException ex) {
1405                                                 // Cannot write to the registry key
1406                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#E2");
1407                                                 Assert.IsNotNull (ex.Message, "#E3");
1408                                                 Assert.IsNull (ex.InnerException, "#E4");
1409                                         }
1410
1411                                         try {
1412                                                 // deleting value that does not exist
1413                                                 createdKey.DeleteValue ("name2", false);
1414                                                 Assert.Fail ("#F1");
1415                                         } catch (UnauthorizedAccessException ex) {
1416                                                 // Cannot write to the registry key
1417                                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#F2");
1418                                                 Assert.IsNotNull (ex.Message, "#F3");
1419                                                 Assert.IsNull (ex.InnerException, "#F4");
1420                                         }
1421                                 }
1422                         } finally {
1423                                 try {
1424                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1425                                         if (createdKey != null) {
1426                                                 createdKey.Close ();
1427                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1428                                         }
1429                                 } catch {
1430                                 }
1431                         }
1432                 }
1433
1434                 [Test]
1435                 public void DeleteValue_Key_Removed ()
1436                 {
1437                         string subKeyName = Guid.NewGuid ().ToString ();
1438
1439                         try {
1440                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1441                                         // check if key was successfully created
1442                                         Assert.IsNotNull (createdKey, "#1");
1443                                         createdKey.SetValue ("name1", "value1");
1444                                 }
1445                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1446                                         Assert.IsNotNull (createdKey, "#2");
1447                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1448                                         Assert.IsNull (Registry.CurrentUser.OpenSubKey (subKeyName), "#3");
1449
1450                                         createdKey.DeleteValue ("name1");
1451                                         createdKey.DeleteValue ("name1", true);
1452                                         createdKey.DeleteValue ("name1", false);
1453                                 }
1454                         } finally {
1455                                 try {
1456                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1457                                         if (createdKey != null) {
1458                                                 createdKey.Close ();
1459                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1460                                         }
1461                                 } catch {
1462                                 }
1463                         }
1464                 }
1465
1466                 [Test]
1467                 public void DeleteValue_Value_DoesNotExist ()
1468                 {
1469                         string subKeyName = Guid.NewGuid ().ToString ();
1470
1471                         try {
1472                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1473                                         // check if key was successfully created
1474                                         Assert.IsNotNull (createdKey, "#A1");
1475                                         createdKey.SetValue ("name1", "value1");
1476
1477                                         try {
1478                                                 createdKey.DeleteValue ("name2");
1479                                                 Assert.Fail ("#B1");
1480                                         } catch (ArgumentException ex) {
1481                                                 // No value exists with that name
1482                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
1483                                                 Assert.IsNull (ex.InnerException, "#B3");
1484                                                 Assert.IsNotNull (ex.Message, "#B4");
1485                                                 Assert.IsNull (ex.ParamName, "#B5");
1486                                         }
1487
1488                                         try {
1489                                                 createdKey.DeleteValue ("name2", true);
1490                                                 Assert.Fail ("#C1");
1491                                         } catch (ArgumentException ex) {
1492                                                 // No value exists with that name
1493                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1494                                                 Assert.IsNull (ex.InnerException, "#C3");
1495                                                 Assert.IsNotNull (ex.Message, "#C4");
1496                                                 Assert.IsNull (ex.ParamName, "#C5");
1497                                         }
1498
1499                                         createdKey.DeleteValue ("name2", false);
1500                                 }
1501                         } finally {
1502                                 try {
1503                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1504                                         if (createdKey != null) {
1505                                                 createdKey.Close ();
1506                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1507                                         }
1508                                 } catch {
1509                                 }
1510                         }
1511                 }
1512
1513                 [Test]
1514                 public void DeleteValue_Name_Empty ()
1515                 {
1516                         string subKeyName = Guid.NewGuid ().ToString ();
1517
1518                         try {
1519                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1520                                         createdKey.SetValue ("name1", "value1");
1521                                         createdKey.SetValue (string.Empty, "value2");
1522
1523                                         string [] names = createdKey.GetValueNames ();
1524                                         Assert.IsNotNull (names, "#A1");
1525                                         Assert.AreEqual (2, names.Length, "#A2");
1526                                         Assert.IsNotNull (names [0], "#A3");
1527                                         /*
1528                                         Assert.AreEqual ("name1", names [0], "#A4");
1529                                         */
1530                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1531                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1532                                         Assert.IsNotNull (names [1], "#A7");
1533                                         /*
1534                                         Assert.AreEqual (string.Empty, names [1], "#A8");
1535                                         */
1536                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A9");
1537                                         Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#A10");
1538
1539                                         createdKey.DeleteValue (string.Empty);
1540
1541                                         names = createdKey.GetValueNames ();
1542                                         Assert.IsNotNull (names, "#B1");
1543                                         Assert.AreEqual (1, names.Length, "#B2");
1544                                         Assert.IsNotNull (names [0], "#B3");
1545                                         Assert.AreEqual ("name1", names [0], "#B4");
1546                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#B5");
1547                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#B6");
1548
1549                                         try {
1550                                                 createdKey.DeleteValue (string.Empty);
1551                                                 Assert.Fail ("#C1");
1552                                         } catch (ArgumentException ex) {
1553                                                 // No value exists with that name
1554                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
1555                                                 Assert.IsNull (ex.InnerException, "#C3");
1556                                                 Assert.IsNotNull (ex.Message, "#C4");
1557                                                 Assert.IsNull (ex.ParamName, "#C5");
1558                                         }
1559
1560                                         try {
1561                                                 createdKey.DeleteValue (string.Empty, true);
1562                                                 Assert.Fail ("#D1");
1563                                         } catch (ArgumentException ex) {
1564                                                 // No value exists with that name
1565                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
1566                                                 Assert.IsNull (ex.InnerException, "#D3");
1567                                                 Assert.IsNotNull (ex.Message, "#D4");
1568                                                 Assert.IsNull (ex.ParamName, "#D5");
1569                                         }
1570
1571                                         createdKey.DeleteValue (string.Empty, false);
1572
1573                                         names = createdKey.GetValueNames ();
1574                                         Assert.IsNotNull (names, "#E1");
1575                                         Assert.AreEqual (1, names.Length, "#E2");
1576                                         Assert.IsNotNull (names [0], "#E3");
1577                                         Assert.AreEqual ("name1", names [0], "#E4");
1578                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#E5");
1579                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#E6");
1580                                 }
1581                         } finally {
1582                                 try {
1583                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1584                                         if (createdKey != null) {
1585                                                 createdKey.Close ();
1586                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1587                                         }
1588                                 } catch {
1589                                 }
1590                         }
1591                 }
1592
1593                 [Test]
1594                 public void DeleteValue_Name_Null ()
1595                 {
1596                         string subKeyName = Guid.NewGuid ().ToString ();
1597
1598                         try {
1599                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1600                                         createdKey.SetValue ("name1", "value1");
1601                                         createdKey.SetValue (null, "value2");
1602
1603                                         string [] names = createdKey.GetValueNames ();
1604                                         Assert.IsNotNull (names, "#A1");
1605                                         Assert.AreEqual (2, names.Length, "#A2");
1606                                         Assert.IsNotNull (names [0], "#A3");
1607                                         /*
1608                                         Assert.AreEqual ("name1", names [0], "#A4");
1609                                         */
1610                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#A5");
1611                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#A6");
1612                                         Assert.IsNotNull (names [1], "#A7");
1613                                         /*
1614                                         Assert.AreEqual (string.Empty, names [1], "#A8");
1615                                         */
1616                                         Assert.IsNotNull (createdKey.GetValue (null), "#A9");
1617                                         Assert.AreEqual ("value2", createdKey.GetValue (null), "#A10");
1618
1619                                         try {
1620                                                 createdKey.DeleteValue (null);
1621                                                 Assert.Fail ("#B1");
1622                                         } catch (ArgumentNullException ex) {
1623                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
1624                                                 Assert.IsNull (ex.InnerException, "#B3");
1625                                                 Assert.IsNotNull (ex.Message, "#B4");
1626                                                 Assert.AreEqual ("name", ex.ParamName, "#B5");
1627                                         }
1628
1629                                         try {
1630                                                 createdKey.DeleteValue (null, true);
1631                                                 Assert.Fail ("#C1");
1632                                         } catch (ArgumentNullException ex) {
1633                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#C2");
1634                                                 Assert.IsNull (ex.InnerException, "#C3");
1635                                                 Assert.IsNotNull (ex.Message, "#C4");
1636                                                 Assert.AreEqual ("name", ex.ParamName, "#C5");
1637                                         }
1638
1639                                         try {
1640                                                 createdKey.DeleteValue (null, false);
1641                                                 Assert.Fail ("#D1");
1642                                         } catch (ArgumentNullException ex) {
1643                                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
1644                                                 Assert.IsNull (ex.InnerException, "#D3");
1645                                                 Assert.IsNotNull (ex.Message, "#D4");
1646                                                 Assert.AreEqual ("name", ex.ParamName, "#D5");
1647                                         }
1648                                 }
1649                         } finally {
1650                                 try {
1651                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1652                                         if (createdKey != null) {
1653                                                 createdKey.Close ();
1654                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1655                                         }
1656                                 } catch {
1657                                 }
1658                         }
1659                 }
1660
1661 #if NET_4_0
1662                 [DllImport ("advapi32.dll", CharSet = CharSet.Unicode)]
1663                 static extern int RegOpenKeyEx (IntPtr keyBase, string keyName, IntPtr reserved, int access, out IntPtr keyHandle);
1664         
1665                 const int RegCurrentUserHive = -2147483647;
1666                 const int RegAccessRead = 0x00020019;
1667
1668                 // FromHandle is specially designed to retrieve a RegistryKey instance based on a IntPtr
1669                 // retrieved using any unmanaged registry function, so we use directly RegOpenKeyEx to load
1670                 // our handle and then pass it to the new 4.0 method.
1671                 [Test]
1672                 public void FromHandle ()
1673                 {
1674                         // Not supported on Unix
1675                         if (RunningOnUnix)
1676                                 Assert.Ignore ("Running on Unix.");
1677
1678                         string subKeyName = Guid.NewGuid ().ToString ();
1679                         try {
1680                                 using (RegistryKey key = Registry.CurrentUser.CreateSubKey (subKeyName))
1681                                         key.SetValue ("Name", "Mono001");
1682
1683                                 IntPtr handle;
1684                                 int res = RegOpenKeyEx (new IntPtr (RegCurrentUserHive), subKeyName, IntPtr.Zero, RegAccessRead, out handle);
1685
1686                                 using (RegistryKey key = RegistryKey.FromHandle (new SafeRegistryHandle (handle, true))) {
1687                                         Assert.AreEqual (String.Empty, key.Name, "#A0");
1688                                         Assert.AreEqual ("Mono001", key.GetValue ("Name"), "#A1");
1689                                 }
1690                         } finally {
1691                                 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1692                                 if (createdKey != null) {
1693                                         createdKey.Close ();
1694                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1695                                 }
1696                         }
1697                 }
1698
1699                 [Test]
1700                 public void FromHandle_InvalidHandle ()
1701                 {
1702                         // Not supported on Unix
1703                         if (RunningOnUnix)
1704                                 Assert.Ignore ("Running on Unix.");
1705
1706                         string subKeyName = Guid.NewGuid ().ToString ();
1707                         try {
1708                                 using (RegistryKey key = RegistryKey.FromHandle (new SafeRegistryHandle (IntPtr.Zero, true))) {
1709                                         Assert.AreEqual (String.Empty, key.Name, "#A0");
1710
1711                                         // Any operation should throw a IOException, since even if we have a RegistryKey instance,
1712                                         // the handle is not valid.
1713                                         key.CreateSubKey ("ChildSubKey");
1714                                         Assert.Fail ("#Exc0");
1715                                 }
1716                         } catch (IOException) {
1717                         } finally {
1718                                 RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1719                                 if (createdKey != null) {
1720                                         createdKey.Close ();
1721                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1722                                 }
1723                         }
1724                 }
1725
1726                 [Test]
1727                 public void Handle ()
1728                 {
1729                         if (RunningOnUnix)
1730                                 Assert.Ignore ("Running on Unix.");
1731
1732                         string subKeyName = Guid.NewGuid ().ToString ();
1733                         RegistryKey subkey = null;
1734                         try {
1735                                 subkey = Registry.CurrentUser.CreateSubKey (subKeyName);
1736                                 Assert.AreEqual (true, subkey.Handle != null, "#A0");
1737                                 Assert.AreEqual (false, subkey.Handle.IsClosed, "#A1");
1738                                 Assert.AreEqual (false, subkey.Handle.IsInvalid, "#A2");
1739
1740                                 subkey.Close ();
1741                                 try {
1742                                         if (subkey.Handle != null)
1743                                                 Console.WriteLine (); // Avoids a warning at compile time
1744                                         Assert.Fail ("#Disposed");
1745                                 } catch (ObjectDisposedException) {
1746                                 }
1747
1748                         } finally {
1749                                 if (subkey != null) {
1750                                         subkey.Close ();
1751                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName, false);
1752                                 }
1753                         }
1754                 }
1755 #endif
1756
1757                 [Test]
1758                 public void GetValue ()
1759                 {
1760                         string subKeyName = Guid.NewGuid ().ToString ();
1761
1762                         try {
1763                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1764                                         createdKey.SetValue ("name1", "value1");
1765                                         createdKey.SetValue ("name2", "value2");
1766                                 }
1767
1768                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1769                                         Assert.IsNotNull (createdKey.GetValue ("name1"), "#1");
1770                                         Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#2");
1771                                         Assert.IsNotNull (createdKey.GetValue ("name2"), "#3");
1772                                         Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#4");
1773                                         Assert.IsNull (createdKey.GetValue ("name3"), "#5");
1774                                         Assert.AreEqual ("value3", createdKey.GetValue ("name3", "value3"), "#6");
1775                                         Assert.IsNull (createdKey.GetValue ("name3", null), "#7");
1776                                         Assert.IsNull (createdKey.GetValue (new string ('a', 400)), "#8");
1777                                 }
1778                         } finally {
1779                                 try {
1780                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1781                                         if (createdKey != null) {
1782                                                 createdKey.Close ();
1783                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1784                                         }
1785                                 } catch {
1786                                 }
1787                         }
1788                 }
1789
1790                 [Test]
1791                 public void GetValue_Key_Removed ()
1792                 {
1793                         string subKeyName = Guid.NewGuid ().ToString ();
1794
1795                         try {
1796                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1797                                         createdKey.SetValue ("name1", "value1");
1798                                         createdKey.SetValue ("name2", "value2");
1799                                 }
1800
1801                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1802                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1803
1804                                         Assert.IsNull (createdKey.GetValue ("name1"), "#1");
1805                                         Assert.IsNotNull (createdKey.GetValue ("name1", "default"), "#2");
1806                                         Assert.AreEqual ("default", createdKey.GetValue ("name1", "default"), "#3");
1807                                         Assert.IsNull (createdKey.GetValue ("name3"), "#3");
1808                                         Assert.IsNotNull (createdKey.GetValue ("name3", "default"), "#4");
1809                                         Assert.AreEqual ("default", createdKey.GetValue ("name3", "default"), "#5");
1810                                 }
1811                         } finally {
1812                                 try {
1813                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1814                                         if (createdKey != null) {
1815                                                 createdKey.Close ();
1816                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1817                                         }
1818                                 } catch {
1819                                 }
1820                         }
1821                 }
1822
1823                 [Test]
1824                 public void GetValue_Name_Empty ()
1825                 {
1826                         string subKeyName = Guid.NewGuid ().ToString ();
1827
1828                         try {
1829                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1830                                         createdKey.SetValue ("name1", "value1");
1831                                         createdKey.SetValue ("name2", "value2");
1832
1833                                         Assert.IsNull (createdKey.GetValue (string.Empty), "#A1");
1834                                         Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#A2");
1835                                         Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#A3");
1836                                         Assert.IsNull (createdKey.GetValue (string.Empty, null), "#A4");
1837                                 }
1838
1839                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1840                                         Assert.IsNull (createdKey.GetValue (string.Empty), "#B1");
1841                                         Assert.IsNotNull (createdKey.GetValue (string.Empty, "default"), "#B2");
1842                                         Assert.AreEqual ("default", createdKey.GetValue (string.Empty, "default"), "#B3");
1843                                         Assert.IsNull (createdKey.GetValue (string.Empty, null), "#B4");
1844                                 }
1845
1846                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1847                                         createdKey.SetValue (string.Empty, "value1");
1848                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#C1");
1849                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#C2");
1850                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#C3");
1851                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#C4");
1852                                 }
1853
1854                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1855                                         Assert.IsNotNull (createdKey.GetValue (string.Empty), "#D1");
1856                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#D2");
1857                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, "default"), "#D3");
1858                                         Assert.AreEqual ("value1", createdKey.GetValue (string.Empty, null), "#D4");
1859                                 }
1860                         } finally {
1861                                 try {
1862                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1863                                         if (createdKey != null) {
1864                                                 createdKey.Close ();
1865                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1866                                         }
1867                                 } catch {
1868                                 }
1869                         }
1870                 }
1871
1872                 [Test]
1873                 public void GetValue_Name_Null ()
1874                 {
1875                         string subKeyName = Guid.NewGuid ().ToString ();
1876
1877                         try {
1878                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1879                                         createdKey.SetValue ("name1", "value1");
1880                                         createdKey.SetValue ("name2", "value2");
1881
1882                                         Assert.IsNull (createdKey.GetValue (null), "#A1");
1883                                         Assert.IsNotNull (createdKey.GetValue (null, "default"), "#A2");
1884                                         Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#A3");
1885                                         Assert.IsNull (createdKey.GetValue (null, null), "#A4");
1886                                 }
1887
1888                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1889                                         Assert.IsNull (createdKey.GetValue (null), "#B1");
1890                                         Assert.IsNotNull (createdKey.GetValue (null, "default"), "#B2");
1891                                         Assert.AreEqual ("default", createdKey.GetValue (null, "default"), "#B3");
1892                                         Assert.IsNull (createdKey.GetValue (null, null), "#B4");
1893                                 }
1894
1895                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
1896                                         createdKey.SetValue (string.Empty, "value1");
1897                                         Assert.IsNotNull (createdKey.GetValue (null), "#C1");
1898                                         Assert.AreEqual ("value1", createdKey.GetValue (null), "#C2");
1899                                         Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#C3");
1900                                         Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#C4");
1901                                 }
1902
1903                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1904                                         Assert.IsNotNull (createdKey.GetValue (null), "#D1");
1905                                         Assert.AreEqual ("value1", createdKey.GetValue (null), "#D2");
1906                                         Assert.AreEqual ("value1", createdKey.GetValue (null, "default"), "#D3");
1907                                         Assert.AreEqual ("value1", createdKey.GetValue (null, null), "#D4");
1908                                 }
1909                         } finally {
1910                                 try {
1911                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
1912                                         if (createdKey != null) {
1913                                                 createdKey.Close ();
1914                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
1915                                         }
1916                                 } catch {
1917                                 }
1918                         }
1919                 }
1920
1921                 [Test]
1922                 public void GetValue_Expand ()
1923                 {
1924                         string subKeyName = Guid.NewGuid ().ToString ();
1925
1926                         try {
1927                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
1928                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1929                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1930
1931                                         createdKey.SetValue ("name1", "%MONO_TEST1%/%MONO_TEST2%",
1932                                                 RegistryValueKind.ExpandString);
1933                                         createdKey.SetValue ("name2", "%MONO_TEST1%/%MONO_TEST2%");
1934                                         createdKey.SetValue ("name3", "just some text",
1935                                                 RegistryValueKind.ExpandString);
1936
1937                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#A1");
1938                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#A2");
1939                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#A3");
1940                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1941                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A4");
1942                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1943                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A5");
1944                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1945                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#A6");
1946                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
1947                                                 null, RegistryValueOptions.None), "#A7");
1948                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1949                                                 null, RegistryValueOptions.None), "#A8");
1950                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1951                                                 null, RegistryValueOptions.None), "#A9");
1952
1953                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "789");
1954                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "666");
1955
1956                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#B1");
1957                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#B2");
1958                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#B3");
1959                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1960                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B4");
1961                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1962                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B5");
1963                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1964                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#B6");
1965                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1966                                                 null, RegistryValueOptions.None), "#B7");
1967                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1968                                                 null, RegistryValueOptions.None), "#B8");
1969                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1970                                                 null, RegistryValueOptions.None), "#B9");
1971                                 }
1972                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
1973                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1"), "#C1");
1974                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#C2");
1975                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#C3");
1976                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1977                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C4");
1978                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1979                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C5");
1980                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1981                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#C6");
1982                                         Assert.AreEqual ("789/666", createdKey.GetValue ("name1",
1983                                                 null, RegistryValueOptions.None), "#C7");
1984                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1985                                                 null, RegistryValueOptions.None), "#C8");
1986                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
1987                                                 null, RegistryValueOptions.None), "#C9");
1988
1989                                         Environment.SetEnvironmentVariable ("MONO_TEST1", "123");
1990                                         Environment.SetEnvironmentVariable ("MONO_TEST2", "456");
1991
1992                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1"), "#D1");
1993                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2"), "#D2");
1994                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3"), "#D3");
1995                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name1",
1996                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D4");
1997                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
1998                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D5");
1999                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
2000                                                 null, RegistryValueOptions.DoNotExpandEnvironmentNames), "#D6");
2001                                         Assert.AreEqual ("123/456", createdKey.GetValue ("name1",
2002                                                 null, RegistryValueOptions.None), "#D7");
2003                                         Assert.AreEqual ("%MONO_TEST1%/%MONO_TEST2%", createdKey.GetValue ("name2",
2004                                                 null, RegistryValueOptions.None), "#D8");
2005                                         Assert.AreEqual ("just some text", createdKey.GetValue ("name3",
2006                                                 null, RegistryValueOptions.None), "#D9");
2007                                 }
2008                         } finally {
2009                                 try {
2010                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2011                                         if (createdKey != null) {
2012                                                 createdKey.Close ();
2013                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2014                                         }
2015                                 } catch {
2016                                 }
2017                         }
2018                 }
2019
2020                 [Test]
2021                 public void GetValueNames ()
2022                 {
2023                         string subKeyName = Guid.NewGuid ().ToString ();
2024
2025                         try {
2026                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2027                                         string [] names = createdKey.GetValueNames ();
2028                                         Assert.IsNotNull (names, "#A1");
2029                                         Assert.AreEqual (0, names.Length, "#A2");
2030
2031                                         createdKey.SetValue ("name1", "value1");
2032                                         createdKey.SetValue ("name2", "value2");
2033                                         createdKey.SetValue ("namelong", "value3");
2034                                         createdKey.SetValue ("name3", "value4");
2035
2036                                         Assert.AreEqual (4, createdKey.ValueCount, "#B1");
2037                                         names = createdKey.GetValueNames ();
2038                                         Assert.IsNotNull (names, "#B2");
2039                                         Assert.AreEqual (4, names.Length, "#B3");
2040                                 }
2041
2042                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
2043                                         string [] names = createdKey.GetValueNames ();
2044                                         Assert.IsNotNull (names, "#C1");
2045                                         Assert.AreEqual (4, names.Length, "#C2");
2046
2047                                         // Mono's Unix registry API uses a hashtable to store the
2048                                         // values (and their names), so names are not returned in
2049                                         // order
2050                                         //
2051                                         // to test whether the names returned by GetValueNames
2052                                         // match what we expect, we use these names to remove the
2053                                         // the values from the created keys and such we should end
2054                                         // up with zero values
2055                                         for (int i = 0; i < names.Length; i++) {
2056                                                 string valueName = names [i];
2057                                                 createdKey.DeleteValue (valueName);
2058                                         }
2059
2060                                         // all values should be removed now
2061                                         Assert.AreEqual (0, createdKey.ValueCount, "#C3");
2062                                 }
2063                         } finally {
2064                                 try {
2065                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2066                                         if (createdKey != null) {
2067                                                 createdKey.Close ();
2068                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2069                                         }
2070                                 } catch {
2071                                 }
2072                         }
2073                 }
2074
2075                 [Test]
2076                 public void GetValueNames_Key_Removed ()
2077                 {
2078                         string subKeyName = Guid.NewGuid ().ToString ();
2079
2080                         try {
2081                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2082                                         createdKey.SetValue ("name1", "value1");
2083                                         createdKey.SetValue ("name2", "value2");
2084
2085                                         string [] names = createdKey.GetValueNames ();
2086                                         Assert.IsNotNull (names, "#A1");
2087                                         Assert.AreEqual (2, names.Length, "#A2");
2088                                 }
2089
2090                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2091                                         string [] names = createdKey.GetValueNames ();
2092                                         Assert.IsNotNull (names, "#B1");
2093                                         Assert.AreEqual (2, names.Length, "#B2");
2094
2095                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2096
2097                                         try {
2098                                                 createdKey.GetValueNames ();
2099                                                 Assert.Fail ("#C1");
2100                                         } catch (IOException ex) {
2101                                                 // Illegal operation attempted on a registry key that
2102                                                 // has been marked for deletion
2103                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
2104                                                 Assert.IsNotNull (ex.Message, "#C3");
2105                                                 Assert.IsNull (ex.InnerException, "#C4");
2106                                         }
2107                                 }
2108                         } finally {
2109                                 try {
2110                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2111                                         if (createdKey != null) {
2112                                                 createdKey.Close ();
2113                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2114                                         }
2115                                 } catch {
2116                                 }
2117                         }
2118                 }
2119
2120                 [Test] // bug #78519
2121                 public void GetSubKeyNamesTest ()
2122                 {
2123                         string subKeyName = Guid.NewGuid ().ToString ();
2124
2125                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2126                         try {
2127                                 // check if key was successfully created
2128                                 Assert.IsNotNull (createdKey, "#A");
2129
2130                                 RegistryKey subKey = createdKey.CreateSubKey ("foo");
2131                                 Assert.IsNotNull (subKey, "#B1");
2132                                 Assert.AreEqual (1, createdKey.SubKeyCount, "#B2");
2133                                 string[] subKeyNames = createdKey.GetSubKeyNames ();
2134                                 Assert.IsNotNull (subKeyNames, "#B3");
2135                                 Assert.AreEqual (1, subKeyNames.Length, "#B4");
2136                                 Assert.AreEqual ("foo", subKeyNames[0], "#B5");
2137
2138                                 subKey = createdKey.CreateSubKey ("longfoo");
2139                                 Assert.IsNotNull (subKey, "#C1");
2140                                 Assert.AreEqual (2, createdKey.SubKeyCount, "#C2");
2141                                 subKeyNames = createdKey.GetSubKeyNames ();
2142                                 Assert.IsNotNull (subKeyNames, "#C3");
2143                                 Assert.AreEqual (2, subKeyNames.Length, "#C4");
2144                                 Assert.AreEqual ("foo", subKeyNames [0], "#C5");
2145                                 Assert.AreEqual ("longfoo", subKeyNames [1], "#C6");
2146
2147                                 subKey = createdKey.CreateSubKey ("sfoo");
2148                                 Assert.IsNotNull (subKey, "#D1");
2149                                 Assert.AreEqual (3, createdKey.SubKeyCount, "#D2");
2150                                 subKeyNames = createdKey.GetSubKeyNames ();
2151                                 Assert.IsNotNull (subKeyNames, "#D3");
2152                                 Assert.AreEqual (3, subKeyNames.Length, "#D4");
2153                                 Assert.AreEqual ("foo", subKeyNames [0], "#D5");
2154                                 Assert.AreEqual ("longfoo", subKeyNames [1], "#D6");
2155                                 Assert.AreEqual ("sfoo", subKeyNames [2], "#D7");
2156
2157                                 foreach (string name in subKeyNames) {
2158                                         createdKey.DeleteSubKeyTree (name);
2159                                 }
2160                                 Assert.AreEqual (0, createdKey.SubKeyCount, "#E");
2161                         } finally {
2162                                 // clean-up
2163                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2164                         }
2165                 }
2166
2167                 [Test]
2168                 public void OpenRemoteBaseKey ()
2169                 {
2170                         // access to registry of remote machines is not implemented on unix
2171                         if (RunningOnUnix)
2172                                 Assert.Ignore ("Running on Unix.");
2173
2174                         RegistryKey hive = RegistryKey.OpenRemoteBaseKey (
2175                                 RegistryHive.CurrentUser, Environment.MachineName);
2176                         Assert.IsNotNull (hive, "#1");
2177
2178                         RegistryKey key = hive.OpenSubKey ("SOFTWARE");
2179                         Assert.IsNotNull (key, "#2");
2180                         key.Close ();
2181
2182                         hive.Close ();
2183                 }
2184
2185                 [Test]
2186                 public void OpenRemoteBaseKey_MachineName_Null ()
2187                 {
2188                         try {
2189                                 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser, null);
2190                                 Assert.Fail ("#1");
2191                         } catch (ArgumentNullException ex) {
2192                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2193                                 Assert.IsNull (ex.InnerException, "#3");
2194                                 Assert.IsNotNull (ex.Message, "#4");
2195                                 Assert.AreEqual ("machineName", ex.ParamName, "#5");
2196                         }
2197                 }
2198
2199                 [Test]
2200                 // This hangs on windows
2201                 [Category ("NotWorking")]
2202                 public void OpenRemoteBaseKey_MachineName_DoesNotExist ()
2203                 {
2204                         // access to registry of remote machines is not implemented on unix
2205                         if (RunningOnUnix)
2206                                 Assert.Ignore ("Running on Unix.");
2207
2208                         try {
2209                                 RegistryKey.OpenRemoteBaseKey (RegistryHive.CurrentUser,
2210                                         "DOESNOTEXIST");
2211                                 Assert.Fail ("#1");
2212                         } catch (IOException ex) {
2213                                 // The network path was not found
2214                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
2215                                 Assert.IsNotNull (ex.Message, "#3");
2216                                 Assert.IsNull (ex.InnerException, "#4");
2217                         }
2218                 }
2219
2220                 [Test] // bug #322839
2221                 public void SetValue1_EntityReferences ()
2222                 {
2223                         string subKeyName = Guid.NewGuid ().ToString ();
2224
2225                         try {
2226                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2227                                         // we created a new subkey, so value should not exist
2228                                         Assert.IsNull (createdKey.GetValue ("FirstName&\"<LastName>\""), "#A1");
2229                                         // create value
2230                                         createdKey.SetValue ("FirstName&\"<LastName>\"", "<'Miguel' & \"de Icaza\">!");
2231                                         // get value
2232                                         object name = createdKey.GetValue ("FirstName&\"<LastName>\"");
2233                                         // value should exist
2234                                         Assert.IsNotNull (name, "#A2");
2235                                         // type of value should be string
2236                                         Assert.AreEqual (typeof (string), name.GetType (), "#A3");
2237                                         // ensure value matches
2238                                         Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#A4");
2239
2240                                         // we created a new subkey, so value should not exist
2241                                         Assert.IsNull (createdKey.GetValue ("Info"), "#B1");
2242                                         // create value
2243                                         createdKey.SetValue ("Info", new string [] { "Mono&<Novell>!", "<CLR&BCL>" });
2244                                         // get value
2245                                         object info = createdKey.GetValue ("Info");
2246                                         // value should exist
2247                                         Assert.IsNotNull (info, "#B2");
2248                                         // type of value should be string
2249                                         Assert.AreEqual (typeof (string []), info.GetType (), "#B3");
2250                                         // ensure value matches
2251                                         Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#B4");
2252                                 }
2253
2254                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2255                                         object name = openedKey.GetValue ("FirstName&\"<LastName>\"");
2256                                         Assert.IsNotNull (name, "#C1");
2257                                         Assert.AreEqual (typeof (string), name.GetType (), "#C2");
2258                                         Assert.AreEqual ("<'Miguel' & \"de Icaza\">!", name, "#C3");
2259
2260                                         object info = openedKey.GetValue ("Info");
2261                                         Assert.IsNotNull (info, "#D1");
2262                                         Assert.AreEqual (typeof (string []), info.GetType (), "#D2");
2263                                         Assert.AreEqual (new string [] { "Mono&<Novell>!", "<CLR&BCL>" }, info, "#D3");
2264                                 }
2265                         } finally {
2266                                 // clean-up
2267                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2268                         }
2269                 }
2270
2271                 [Test] // SetValue (String, Object)
2272                 public void SetValue1_Name_Null ()
2273                 {
2274                         string subKeyName = Guid.NewGuid ().ToString ();
2275
2276                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2277                         try {
2278                                 createdKey.SetValue (null, "value1");
2279                                 string [] names = createdKey.GetValueNames ();
2280                                 Assert.IsNotNull (names, "#A1");
2281                                 Assert.AreEqual (1, names.Length, "#A2");
2282                                 Assert.IsNotNull (names [0], "#A3");
2283                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2284                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2285                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2286                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2287                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2288
2289                                 createdKey.SetValue (string.Empty, "value2");
2290                                 names = createdKey.GetValueNames ();
2291                                 Assert.IsNotNull (names, "#B1");
2292                                 Assert.AreEqual (1, names.Length, "#B2");
2293                                 Assert.IsNotNull (names [0], "#B3");
2294                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2295                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2296                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2297                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2298                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2299                         } finally {
2300                                 // clean-up
2301                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2302                         }
2303                 }
2304
2305                 [Test] // SetValue (String, Object)
2306                 public void SetValue1_Name_Empty ()
2307                 {
2308                         string subKeyName = Guid.NewGuid ().ToString ();
2309
2310                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2311                         try {
2312                                 createdKey.SetValue (string.Empty, "value1");
2313                                 string [] names = createdKey.GetValueNames ();
2314                                 Assert.IsNotNull (names, "#A1");
2315                                 Assert.AreEqual (1, names.Length, "#A2");
2316                                 Assert.IsNotNull (names [0], "#A3");
2317                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2318                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2319                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2320                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2321                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2322
2323                                 createdKey.SetValue (null, "value2");
2324                                 names = createdKey.GetValueNames ();
2325                                 Assert.IsNotNull (names, "#B1");
2326                                 Assert.AreEqual (1, names.Length, "#B2");
2327                                 Assert.IsNotNull (names [0], "#B3");
2328                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2329                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2330                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2331                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2332                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2333                         } finally {
2334                                 // clean-up
2335                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2336                         }
2337                 }
2338
2339                 [Test] // SetValue (String, Object)
2340                 public void SetValue1_Name_MaxLength ()
2341                 {
2342                         string subKeyName = Guid.NewGuid ().ToString ();
2343
2344                         try {
2345                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2346                                         string name = new string ('a', 254);
2347
2348                                         createdKey.SetValue (name, "value1");
2349                                         Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2350                                         createdKey.DeleteValue (name);
2351                                         Assert.IsNull (createdKey.GetValue (name), "#A2");
2352
2353                                         name = new string ('a', 255);
2354
2355                                         createdKey.SetValue (name, "value2");
2356                                         Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2357                                         createdKey.DeleteValue (name);
2358                                         Assert.IsNull (createdKey.GetValue (name), "#B2");
2359
2360                                         name = new string ('a', 256);
2361
2362                                         try {
2363                                                 createdKey.SetValue (name, "value2");
2364                                                 Assert.Fail ("#C1");
2365                                         } catch (ArgumentException ex) {
2366                                                 // 1.x: Registry subkeys should not be
2367                                                 // greater than or equal to 255 characters
2368                                                 //
2369                                                 // 2.x: Registry subkeys should not be
2370                                                 // greater than 255 characters
2371                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2372                                                 Assert.IsNull (ex.InnerException, "#C3");
2373                                                 Assert.IsNotNull (ex.Message, "#C4");
2374                                                 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2375                                                 Assert.IsNull (ex.ParamName, "#C6");
2376                                         }
2377                                 }
2378                         } finally {
2379                                 try {
2380                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2381                                         if (createdKey != null) {
2382                                                 createdKey.Close ();
2383                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2384                                         }
2385                                 } catch {
2386                                 }
2387                         }
2388                 }
2389
2390                 [Test] // SetValue (String, Object)
2391                 public void SetValue1_Value_Null ()
2392                 {
2393                         string subKeyName = Guid.NewGuid ().ToString ();
2394
2395                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2396                         try {
2397                                 try {
2398                                         createdKey.SetValue ("Name", null);
2399                                         Assert.Fail ("#1");
2400                                 } catch (ArgumentNullException ex) {
2401                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2402                                         Assert.IsNull (ex.InnerException, "#3");
2403                                         Assert.IsNotNull (ex.Message, "#4");
2404                                         Assert.AreEqual ("value", ex.ParamName, "#5");
2405                                 }
2406                         } finally {
2407                                 // clean-up
2408                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2409                         }
2410                 }
2411
2412                 [Test] // SetValue (String, Object)
2413                 public void SetValue1_Boolean ()
2414                 {
2415                         string subKeyName = Guid.NewGuid ().ToString ();
2416
2417                         try {
2418                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2419                                         // we created a new subkey, so value should not exist
2420                                         Assert.IsNull (createdKey.GetValue ("Installed"), "#A1");
2421                                         // create value
2422                                         createdKey.SetValue ("Installed", true);
2423                                         // get value
2424                                         object value = createdKey.GetValue ("Installed");
2425                                         // value should exist
2426                                         Assert.IsNotNull (value, "#A2");
2427                                         // type of value should be string
2428                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2429                                         // ensure value matches
2430                                         Assert.AreEqual (true.ToString (), value, "#A4");
2431                                 }
2432
2433                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2434                                         object value = openedKey.GetValue ("Installed");
2435                                         Assert.IsNotNull (value, "#B1");
2436                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2437                                         Assert.AreEqual (true.ToString (), value, "#B3");
2438                                 }
2439                         } finally {
2440                                 // clean-up
2441                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2442                         }
2443                 }
2444
2445                 [Test] // SetValue (String, Object)
2446                 public void SetValue1_Byte ()
2447                 {
2448                         string subKeyName = Guid.NewGuid ().ToString ();
2449
2450                         try {
2451                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2452                                         // we created a new subkey, so value should not exist
2453                                         Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2454                                         // create value
2455                                         createdKey.SetValue ("Flags", (byte) 5);
2456                                         // get value
2457                                         object value = createdKey.GetValue ("Flags");
2458                                         // value should exist
2459                                         Assert.IsNotNull (value, "#A2");
2460                                         // type of value should be string
2461                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2462                                         // ensure value matches
2463                                         Assert.AreEqual ("5", value, "#A4");
2464                                 }
2465
2466                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2467                                         object value = openedKey.GetValue ("Flags");
2468                                         Assert.IsNotNull (value, "#B1");
2469                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2470                                         Assert.AreEqual ("5", value, "#B3");
2471                                 }
2472                         } finally {
2473                                 // clean-up
2474                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2475                         }
2476                 }
2477
2478                 [Test] // SetValue (String, Object)
2479                 public void SetValue1_ByteArray ()
2480                 {
2481                         string subKeyName = Guid.NewGuid ().ToString ();
2482
2483                         try {
2484                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2485                                         // we created a new subkey, so value should not exist
2486                                         Assert.IsNull (createdKey.GetValue ("Flags"), "#A1");
2487                                         // create value
2488                                         createdKey.SetValue ("Flags", new byte [] { 1, 5 });
2489                                         // get value
2490                                         object value = createdKey.GetValue ("Flags");
2491                                         // value should exist
2492                                         Assert.IsNotNull (value, "#A2");
2493                                         // type of value should be string
2494                                         Assert.AreEqual (typeof (byte []), value.GetType (), "#3");
2495                                         // ensure value matches
2496                                         Assert.AreEqual (new byte [] { 1, 5 }, value, "#4");
2497                                 }
2498
2499                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2500                                         object value = openedKey.GetValue ("Flags");
2501                                         Assert.IsNotNull (value, "#B1");
2502                                         Assert.AreEqual (typeof (byte []), value.GetType (), "#B2");
2503                                         Assert.AreEqual (new byte [] { 1, 5 }, value, "#B3");
2504                                 }
2505                         } finally {
2506                                 // clean-up
2507                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2508                         }
2509                 }
2510
2511                 [Test] // SetValue (String, Object)
2512                 public void SetValue1_DateTime ()
2513                 {
2514                         string subKeyName = Guid.NewGuid ().ToString ();
2515
2516                         try {
2517                                 object rawValue = DateTime.Now;
2518
2519                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2520                                         // we created a new subkey, so value should not exist
2521                                         Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2522                                         // create value
2523                                         createdKey.SetValue ("Path", rawValue);
2524                                         // get value
2525                                         object value = createdKey.GetValue ("Path");
2526                                         // value should exist
2527                                         Assert.IsNotNull (value, "#A2");
2528                                         // type of value should be string
2529                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2530                                         // ensure value matches
2531                                         Assert.AreEqual (rawValue.ToString (), value, "#A4");
2532                                 }
2533
2534                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2535                                         object value = openedKey.GetValue ("Path");
2536                                         Assert.IsNotNull (value, "#B1");
2537                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2538                                         Assert.AreEqual (rawValue.ToString (), value, "#B3");
2539                                 }
2540                         } finally {
2541                                 // clean-up
2542                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2543                         }
2544                 }
2545
2546                 [Test]
2547                 public void SetValue_Int32 ()
2548                 {
2549                         string subKeyName = Guid.NewGuid ().ToString ();
2550
2551                         try {
2552                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2553                                         // we created a new subkey, so value should not exist
2554                                         Assert.IsNull (createdKey.GetValue ("RefCount"), "#A1");
2555                                         // create value
2556                                         createdKey.SetValue ("RefCount", 5);
2557                                         // get value
2558                                         object value = createdKey.GetValue ("RefCount");
2559                                         // value should exist
2560                                         Assert.IsNotNull (value, "#A2");
2561                                         // type of value should be int
2562                                         Assert.AreEqual (typeof (int), value.GetType (), "#A3");
2563                                         // ensure value matches
2564                                         Assert.AreEqual (5, value, "#A4");
2565                                 }
2566
2567                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2568                                         object value = openedKey.GetValue ("RefCount");
2569                                         Assert.IsNotNull (value, "#B1");
2570                                         Assert.AreEqual (typeof (int), value.GetType (), "#B2");
2571                                         Assert.AreEqual (5, value, "#B3");
2572                                 }
2573                         } finally {
2574                                 // clean-up
2575                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2576                         }
2577                 }
2578
2579                 [Test] // SetValue (String, Object)
2580                 public void SetValue1_Int64 ()
2581                 {
2582                         string subKeyName = Guid.NewGuid ().ToString ();
2583
2584                         try {
2585                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2586                                         // we created a new subkey, so value should not exist
2587                                         Assert.IsNull (createdKey.GetValue ("Ticks"), "#A1");
2588                                         // create value
2589                                         createdKey.SetValue ("Ticks", 500L);
2590                                         // get value
2591                                         object value = createdKey.GetValue ("Ticks");
2592                                         // value should exist
2593                                         Assert.IsNotNull (value, "#A2");
2594                                         // type of value should be string
2595                                         Assert.AreEqual (typeof (string), value.GetType (), "#A3");
2596                                         // ensure value matches
2597                                         Assert.AreEqual ("500", value, "#A4");
2598                                 }
2599
2600                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2601                                         object value = openedKey.GetValue ("Ticks");
2602                                         Assert.IsNotNull (value, "#B1");
2603                                         Assert.AreEqual (typeof (string), value.GetType (), "#B2");
2604                                         Assert.AreEqual ("500", value, "#B3");
2605                                 }
2606                         } finally {
2607                                 // clean-up
2608                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2609                         }
2610                 }
2611
2612                 [Test] // SetValue (String, Object)
2613                 public void SetValue1_String ()
2614                 {
2615                         string subKeyName = Guid.NewGuid ().ToString ();
2616
2617                         try {
2618                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2619                                         // we created a new subkey, so value should not exist
2620                                         Assert.IsNull (createdKey.GetValue ("Path"), "#A1");
2621                                         // create value
2622                                         createdKey.SetValue ("Path", "/usr/lib/whatever");
2623                                         // get value
2624                                         object path = createdKey.GetValue ("Path");
2625                                         // value should exist
2626                                         Assert.IsNotNull (path, "#A2");
2627                                         // type of value should be string
2628                                         Assert.AreEqual (typeof (string), path.GetType (), "#A3");
2629                                         // ensure value matches
2630                                         Assert.AreEqual ("/usr/lib/whatever", path, "#A4");
2631                                 }
2632
2633                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2634                                         object path = openedKey.GetValue ("Path");
2635                                         Assert.IsNotNull (path, "#B1");
2636                                         Assert.AreEqual (typeof (string), path.GetType (), "#B2");
2637                                         Assert.AreEqual ("/usr/lib/whatever", path, "#B3");
2638                                 }
2639                         } finally {
2640                                 // clean-up
2641                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2642                         }
2643                 }
2644
2645                 [Test] // SetValue (String, Object)
2646                 public void SetValue1_StringArray ()
2647                 {
2648                         string subKeyName = Guid.NewGuid ().ToString ();
2649
2650                         try {
2651                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2652                                         // we created a new subkey, so value should not exist
2653                                         Assert.IsNull (createdKey.GetValue ("DependsOnGroup"), "#A1");
2654                                         // create value
2655                                         createdKey.SetValue ("DependsOnGroup", new string [] { "A", "B" });
2656                                         // get value
2657                                         object value = createdKey.GetValue ("DependsOnGroup");
2658                                         // value should exist
2659                                         Assert.IsNotNull (value, "#A2");
2660                                         // type of value should be string
2661                                         Assert.AreEqual (typeof (string []), value.GetType (), "#A3");
2662                                         // ensure value matches
2663                                         Assert.AreEqual (new string [] { "A", "B" }, value, "#A4");
2664                                 }
2665
2666                                 using (RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
2667                                         object value = openedKey.GetValue ("DependsOnGroup");
2668                                         Assert.IsNotNull (value, "#B1");
2669                                         Assert.AreEqual (typeof (string []), value.GetType (), "#B2");
2670                                         Assert.AreEqual (new string [] { "A", "B" }, value, "#B3");
2671                                 }
2672                         } finally {
2673                                 // clean-up
2674                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2675                         }
2676                 }
2677
2678                 [Test] // SetValue (String, Object)
2679                 public void SetValue1_Key_ReadOnly ()
2680                 {
2681                         string subKeyName = Guid.NewGuid ().ToString ();
2682
2683                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2684                                 try {
2685                                         softwareKey.SetValue ("name1", "value1");
2686                                         Assert.Fail ("#1");
2687                                 } catch (UnauthorizedAccessException ex) {
2688                                         // Cannot write to the registry key
2689                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2690                                         Assert.IsNotNull (ex.Message, "#3");
2691                                         Assert.IsNull (ex.InnerException, "#4");
2692                                 }
2693                         }
2694
2695                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2696                                 try {
2697                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2698                                         }
2699
2700                                         using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2701                                                 try {
2702                                                         createdKey.SetValue ("name1", "value1");
2703                                                         Assert.Fail ("#1");
2704                                                 } catch (UnauthorizedAccessException ex) {
2705                                                         // Cannot write to the registry key
2706                                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2707                                                         Assert.IsNotNull (ex.Message, "#3");
2708                                                         Assert.IsNull (ex.InnerException, "#4");
2709                                                 }
2710                                         }
2711                                 } finally {
2712                                         try {
2713                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2714                                                 if (createdKey != null) {
2715                                                         createdKey.Close ();
2716                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2717                                                 }
2718                                         } catch {
2719                                         }
2720                                 }
2721                         }
2722                 }
2723
2724                 [Test] // SetValue (String, Object)
2725                 public void SetValue1_Key_Removed ()
2726                 {
2727                         string subKeyName = Guid.NewGuid ().ToString ();
2728
2729                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2730                                 try {
2731                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2732                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2733                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2734                                                 try {
2735                                                         createdKey.SetValue ("name1", "value1");
2736                                                         Assert.Fail ("#2");
2737                                                 } catch (IOException ex) {
2738                                                         // Illegal operation attempted on a registry key that
2739                                                         // has been marked for deletion
2740                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2741                                                         Assert.IsNotNull (ex.Message, "#4");
2742                                                         Assert.IsNull (ex.InnerException, "#5");
2743                                                 }
2744                                         }
2745                                 } finally {
2746                                         try {
2747                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2748                                                 if (createdKey != null) {
2749                                                         createdKey.Close ();
2750                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2751                                                 }
2752                                         } catch {
2753                                         }
2754                                 }
2755                         }
2756                 }
2757
2758                 [Test] // SetValue (String, Object, RegistryValueKind)
2759                 public void SetValue2_Key_ReadOnly ()
2760                 {
2761                         string subKeyName = Guid.NewGuid ().ToString ();
2762
2763                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software")) {
2764                                 try {
2765                                         softwareKey.SetValue ("name1", "value1",
2766                                                 RegistryValueKind.String);
2767                                         Assert.Fail ("#1");
2768                                 } catch (UnauthorizedAccessException ex) {
2769                                         // Cannot write to the registry key
2770                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2771                                         Assert.IsNotNull (ex.Message, "#3");
2772                                         Assert.IsNull (ex.InnerException, "#4");
2773                                 }
2774                         }
2775
2776                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2777                                 try {
2778                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2779                                         }
2780
2781                                         using (RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName)) {
2782                                                 try {
2783                                                         createdKey.SetValue ("name1", "value1",
2784                                                                 RegistryValueKind.String);
2785                                                         Assert.Fail ("#1");
2786                                                 } catch (UnauthorizedAccessException ex) {
2787                                                         // Cannot write to the registry key
2788                                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
2789                                                         Assert.IsNotNull (ex.Message, "#3");
2790                                                         Assert.IsNull (ex.InnerException, "#4");
2791                                                 }
2792                                         }
2793                                 } finally {
2794                                         try {
2795                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2796                                                 if (createdKey != null) {
2797                                                         createdKey.Close ();
2798                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2799                                                 }
2800                                         } catch {
2801                                         }
2802                                 }
2803                         }
2804                 }
2805
2806                 [Test] // SetValue (String, Object, RegistryValueKind)
2807                 public void SetValue2_Key_Removed ()
2808                 {
2809                         string subKeyName = Guid.NewGuid ().ToString ();
2810
2811                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
2812                                 try {
2813                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
2814                                                 softwareKey.DeleteSubKeyTree (subKeyName);
2815                                                 Assert.IsNull (softwareKey.OpenSubKey (subKeyName), "#1");
2816                                                 try {
2817                                                         createdKey.SetValue ("name1", "value1",
2818                                                                 RegistryValueKind.String);
2819                                                         Assert.Fail ("#2");
2820                                                 } catch (IOException ex) {
2821                                                         // Illegal operation attempted on a registry key that
2822                                                         // has been marked for deletion
2823                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#3");
2824                                                         Assert.IsNotNull (ex.Message, "#4");
2825                                                         Assert.IsNull (ex.InnerException, "#5");
2826                                                 }
2827                                         }
2828                                 } finally {
2829                                         try {
2830                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
2831                                                 if (createdKey != null) {
2832                                                         createdKey.Close ();
2833                                                         softwareKey.DeleteSubKeyTree (subKeyName);
2834                                                 }
2835                                         } catch {
2836                                         }
2837                                 }
2838                         }
2839                 }
2840
2841                 [Test] // SetValue (String, Object, RegistryValueKind)
2842                 public void SetValue2_Name_Empty ()
2843                 {
2844                         string subKeyName = Guid.NewGuid ().ToString ();
2845
2846                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2847                         try {
2848                                 createdKey.SetValue (string.Empty, "value1",
2849                                         RegistryValueKind.String);
2850                                 string [] names = createdKey.GetValueNames ();
2851                                 Assert.IsNotNull (names, "#A1");
2852                                 Assert.AreEqual (1, names.Length, "#A2");
2853                                 Assert.IsNotNull (names [0], "#A3");
2854                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2855                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2856                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2857                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2858                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2859
2860                                 createdKey.SetValue (null, "value2",
2861                                         RegistryValueKind.String);
2862                                 names = createdKey.GetValueNames ();
2863                                 Assert.IsNotNull (names, "#B1");
2864                                 Assert.AreEqual (1, names.Length, "#B2");
2865                                 Assert.IsNotNull (names [0], "#B3");
2866                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2867                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2868                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2869                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2870                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2871                         } finally {
2872                                 // clean-up
2873                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2874                         }
2875                 }
2876
2877                 [Test] // SetValue (String, Object, RegistryValueKind)
2878                 public void SetValue2_Name_MaxLength ()
2879                 {
2880                         string subKeyName = Guid.NewGuid ().ToString ();
2881
2882                         try {
2883                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2884                                         string name = new string ('a', 254);
2885
2886                                         createdKey.SetValue (name, "value1",
2887                                                 RegistryValueKind.String);
2888                                         Assert.IsNotNull (createdKey.GetValue (name), "#A1");
2889                                         createdKey.DeleteValue (name);
2890                                         Assert.IsNull (createdKey.GetValue (name), "#A2");
2891
2892                                         name = new string ('a', 255);
2893
2894                                         createdKey.SetValue (name, "value2",
2895                                                 RegistryValueKind.String);
2896                                         Assert.IsNotNull (createdKey.GetValue (name), "#B1");
2897                                         createdKey.DeleteValue (name);
2898                                         Assert.IsNull (createdKey.GetValue (name), "#B2");
2899
2900                                         name = new string ('a', 256);
2901
2902                                         try {
2903                                                 createdKey.SetValue (name, "value2",
2904                                                         RegistryValueKind.String);
2905                                                 Assert.Fail ("#C1");
2906                                         } catch (ArgumentException ex) {
2907                                                 // Registry subkeys should not be
2908                                                 // greater than 255 characters
2909                                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
2910                                                 Assert.IsNull (ex.InnerException, "#C3");
2911                                                 Assert.IsNotNull (ex.Message, "#C4");
2912                                                 Assert.IsTrue (ex.Message.IndexOf ("255") != -1, "#C5");
2913                                                 Assert.IsNull (ex.ParamName, "#C6");
2914                                         }
2915                                 }
2916                         } finally {
2917                                 try {
2918                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
2919                                         if (createdKey != null) {
2920                                                 createdKey.Close ();
2921                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2922                                         }
2923                                 } catch {
2924                                 }
2925                         }
2926                 }
2927
2928                 [Test] // SetValue (String, Object, RegistryValueKind)
2929                 public void SetValue2_Name_Null ()
2930                 {
2931                         string subKeyName = Guid.NewGuid ().ToString ();
2932
2933                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2934                         try {
2935                                 createdKey.SetValue (null, "value1",
2936                                         RegistryValueKind.String);
2937                                 string [] names = createdKey.GetValueNames ();
2938                                 Assert.IsNotNull (names, "#A1");
2939                                 Assert.AreEqual (1, names.Length, "#A2");
2940                                 Assert.IsNotNull (names [0], "#A3");
2941                                 Assert.AreEqual (string.Empty, names [0], "#A4");
2942                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#A5");
2943                                 Assert.AreEqual ("value1", createdKey.GetValue (string.Empty), "#A6");
2944                                 Assert.IsNotNull (createdKey.GetValue (null), "#A7");
2945                                 Assert.AreEqual ("value1", createdKey.GetValue (null), "#A8");
2946
2947                                 createdKey.SetValue (string.Empty, "value2",
2948                                         RegistryValueKind.String);
2949                                 names = createdKey.GetValueNames ();
2950                                 Assert.IsNotNull (names, "#B1");
2951                                 Assert.AreEqual (1, names.Length, "#B2");
2952                                 Assert.IsNotNull (names [0], "#B3");
2953                                 Assert.AreEqual (string.Empty, names [0], "#B4");
2954                                 Assert.IsNotNull (createdKey.GetValue (string.Empty), "#B5");
2955                                 Assert.AreEqual ("value2", createdKey.GetValue (string.Empty), "#B6");
2956                                 Assert.IsNotNull (createdKey.GetValue (null), "#B7");
2957                                 Assert.AreEqual ("value2", createdKey.GetValue (null), "#B8");
2958                         } finally {
2959                                 // clean-up
2960                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2961                         }
2962                 }
2963
2964                 [Test] // SetValue (String, Object, RegistryValueKind)
2965                 public void SetValue2_Value_Null ()
2966                 {
2967                         string subKeyName = Guid.NewGuid ().ToString ();
2968
2969                         RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName);
2970                         try {
2971                                 try {
2972                                         createdKey.SetValue ("Name", null,
2973                                                 RegistryValueKind.String);
2974                                         Assert.Fail ("#1");
2975                                 } catch (ArgumentNullException ex) {
2976                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2977                                         Assert.IsNull (ex.InnerException, "#3");
2978                                         Assert.IsNotNull (ex.Message, "#4");
2979                                         Assert.AreEqual ("value", ex.ParamName, "#5");
2980                                 }
2981                         } finally {
2982                                 // clean-up
2983                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
2984                         }
2985                 }
2986
2987                 [Test]
2988                 public void SubKeyCount ()
2989                 {
2990                         string subKeyName = Guid.NewGuid ().ToString ();
2991
2992                         try {
2993                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
2994                                         // check if key was successfully created
2995                                         Assert.IsNotNull (createdKey, "#A1");
2996                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
2997                                                 subKey.Close ();
2998                                         }
2999                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3000                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3001                                                 subKey.Close ();
3002                                         }
3003                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3004                                 }
3005                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3006                                         Assert.IsNotNull (createdKey, "#B1");
3007                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3008
3009                                         using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3010                                                 Assert.IsNotNull (createdKey2, "#B3");
3011                                                 Assert.AreEqual (2, createdKey2.SubKeyCount, "#B4");
3012                                                 createdKey2.DeleteSubKey ("monotemp1");
3013                                                 Assert.AreEqual (1, createdKey2.SubKeyCount, "#B5");
3014                                         }
3015                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#B6");
3016                                 }
3017                         } finally {
3018                                 try {
3019                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3020                                         if (createdKey != null) {
3021                                                 createdKey.Close ();
3022                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3023                                         }
3024                                 } catch {
3025                                 }
3026                         }
3027                 }
3028
3029                 [Test]
3030                 public void SubKeyCount_Key_Removed ()
3031                 {
3032                         string subKeyName = Guid.NewGuid ().ToString ();
3033
3034                         try {
3035                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3036                                         // check if key was successfully created
3037                                         Assert.IsNotNull (createdKey, "#A1");
3038                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp1")) {
3039                                                 subKey.Close ();
3040                                         }
3041                                         Assert.AreEqual (1, createdKey.SubKeyCount, "#A2");
3042                                         using (RegistryKey subKey = createdKey.CreateSubKey ("monotemp2")) {
3043                                                 subKey.Close ();
3044                                         }
3045                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#A3");
3046                                 }
3047                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3048                                         Assert.IsNotNull (createdKey, "#B1");
3049                                         Assert.AreEqual (2, createdKey.SubKeyCount, "#B2");
3050
3051                                         // remove created key
3052                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3053
3054                                         try {
3055                                                 Assert.Fail ("#C1: " + createdKey.SubKeyCount);
3056                                         } catch (IOException ex) {
3057                                                 // Illegal operation attempted on a registry key that
3058                                                 // has been marked for deletion
3059                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3060                                                 Assert.IsNotNull (ex.Message, "#15");
3061                                                 Assert.IsNull (ex.InnerException, "#16");
3062                                         }
3063                                 }
3064                         } finally {
3065                                 try {
3066                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3067                                         if (createdKey != null) {
3068                                                 createdKey.Close ();
3069                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3070                                         }
3071                                 } catch {
3072                                 }
3073                         }
3074                 }
3075
3076                 [Test]
3077                 public void ValueCount ()
3078                 {
3079                         string subKeyName = Guid.NewGuid ().ToString ();
3080
3081                         try {
3082                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3083                                         // check if key was successfully created
3084                                         Assert.IsNotNull (createdKey, "#A1");
3085                                         Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3086                                         createdKey.SetValue ("name1", "value1");
3087                                         Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3088                                         createdKey.SetValue ("name2", "value2");
3089                                         Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3090                                         createdKey.SetValue ("name2", "value2b");
3091                                         Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3092                                         createdKey.SetValue ("name3", "value3");
3093                                         Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3094                                 }
3095                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3096                                         Assert.IsNotNull (createdKey, "#B1");
3097                                         Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3098
3099                                         using (RegistryKey createdKey2 = Registry.CurrentUser.OpenSubKey (subKeyName, true)) {
3100                                                 Assert.IsNotNull (createdKey2, "#B3");
3101                                                 Assert.AreEqual (3, createdKey2.ValueCount, "#B4");
3102                                                 createdKey2.DeleteValue ("name2");
3103                                                 Assert.AreEqual (2, createdKey2.ValueCount, "#B5");
3104                                         }
3105                                         Assert.AreEqual (2, createdKey.ValueCount, "#B6");
3106                                 }
3107                         } finally {
3108                                 try {
3109                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3110                                         if (createdKey != null) {
3111                                                 createdKey.Close ();
3112                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3113                                         }
3114                                 } catch {
3115                                 }
3116                         }
3117                 }
3118
3119                 [Test]
3120                 public void ValueCount_Key_Removed ()
3121                 {
3122                         string subKeyName = Guid.NewGuid ().ToString ();
3123
3124                         try {
3125                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3126                                         // check if key was successfully created
3127                                         Assert.IsNotNull (createdKey, "#A1");
3128                                         Assert.AreEqual (0, createdKey.ValueCount, "#A2");
3129                                         createdKey.SetValue ("name1", "value1");
3130                                         Assert.AreEqual (1, createdKey.ValueCount, "#A3");
3131                                         createdKey.SetValue ("name2", "value2");
3132                                         Assert.AreEqual (2, createdKey.ValueCount, "#A4");
3133                                         createdKey.SetValue ("name2", "value2b");
3134                                         Assert.AreEqual (2, createdKey.ValueCount, "#A5");
3135                                         createdKey.SetValue ("name3", "value3");
3136                                         Assert.AreEqual (3, createdKey.ValueCount, "#A6");
3137                                 }
3138                                 using (RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName)) {
3139                                         Assert.IsNotNull (createdKey, "#B1");
3140                                         Assert.AreEqual (3, createdKey.ValueCount, "#B2");
3141
3142                                         // remove created key
3143                                         Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3144
3145                                         try {
3146                                                 Assert.Fail ("#C1: " + createdKey.ValueCount);
3147                                         } catch (IOException ex) {
3148                                                 // Illegal operation attempted on a registry key that
3149                                                 // has been marked for deletion
3150                                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3151                                                 Assert.IsNotNull (ex.Message, "#15");
3152                                                 Assert.IsNull (ex.InnerException, "#16");
3153                                         }
3154                                 }
3155                         } finally {
3156                                 try {
3157                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3158                                         if (createdKey != null) {
3159                                                 createdKey.Close ();
3160                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3161                                         }
3162                                 } catch {
3163                                 }
3164                         }
3165                 }
3166
3167                 [Test]
3168                 public void bug79051 ()
3169                 {
3170                         string subKeyName = Guid.NewGuid ().ToString ();
3171
3172                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3173                                 try {
3174                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3175                                                 createdKey.SetValue ("test", "whatever");
3176                                                 createdKey.Close ();
3177                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3178                                         }
3179                                 } finally {
3180                                         try {
3181                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3182                                                 if (createdKey != null) {
3183                                                         createdKey.Close ();
3184                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3185                                                 }
3186                                         } catch {
3187                                         }
3188                                 }
3189                         }
3190                 }
3191
3192                 // Bug Xamarin 3632
3193                 [Test]
3194                 public void TypeCastTests ()
3195                 {
3196                         string subKeyName = Guid.NewGuid ().ToString ();
3197                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3198                                 try {
3199                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3200                                                 createdKey.SetValue ("test-int", (int) 1, RegistryValueKind.DWord);
3201                                                 createdKey.SetValue ("test-uint", (uint) 1, RegistryValueKind.DWord);
3202                                                 createdKey.SetValue ("test-byte", (byte) 1, RegistryValueKind.DWord);
3203                                                 createdKey.SetValue ("test-sbyte", (sbyte) 1, RegistryValueKind.DWord);
3204                                                 createdKey.SetValue ("test-short", (short) 1, RegistryValueKind.DWord);
3205                                                 createdKey.SetValue ("test-ushort", (ushort) 1, RegistryValueKind.DWord);
3206                                                 createdKey.SetValue ("test-long", (long) 1, RegistryValueKind.DWord);
3207                                                 createdKey.SetValue ("test-ulong", (ulong) 1, RegistryValueKind.DWord);
3208                                                 createdKey.SetValue ("test-decimal", (decimal) 1, RegistryValueKind.DWord);
3209                                                 createdKey.SetValue ("test-float", (float) 1, RegistryValueKind.DWord);
3210                                                 createdKey.SetValue ("test-bool", true, RegistryValueKind.DWord);
3211
3212                                                 createdKey.SetValue ("dtest-int", (int) 1, RegistryValueKind.QWord);
3213                                                 createdKey.SetValue ("dtest-uint", (uint) 1, RegistryValueKind.QWord);
3214                                                 createdKey.SetValue ("dtest-byte", (byte) 1, RegistryValueKind.QWord);
3215                                                 createdKey.SetValue ("dtest-sbyte", (sbyte) 1, RegistryValueKind.QWord);
3216                                                 createdKey.SetValue ("dtest-short", (short) 1, RegistryValueKind.QWord);
3217                                                 createdKey.SetValue ("dtest-ushort", (ushort) 1, RegistryValueKind.QWord);
3218                                                 createdKey.SetValue ("dtest-long", (long) 1, RegistryValueKind.QWord);
3219                                                 createdKey.SetValue ("dtest-ulong", (ulong) 1, RegistryValueKind.QWord);
3220                                                 createdKey.SetValue ("dtest-decimal", (decimal) 1, RegistryValueKind.QWord);
3221                                                 createdKey.SetValue ("dtest-float", (float) 1, RegistryValueKind.QWord);
3222                                                 createdKey.SetValue ("dtest-bool", true, RegistryValueKind.QWord);
3223
3224                                                 object r = createdKey.GetValue ("test-int");
3225                                                 Assert.AreEqual (r is int, true);
3226                                                 Assert.AreEqual ((int) r, 1);
3227                                                 
3228                                                 r = createdKey.GetValue ("test-uint");
3229                                                 Assert.AreEqual (r is int, true);
3230                                                 Assert.AreEqual ((int) r, 1);
3231                                                 r = createdKey.GetValue ("test-byte");
3232                                                 Assert.AreEqual (r is int, true);
3233                                                 Assert.AreEqual ((int) r, 1);
3234                                                 r = createdKey.GetValue ("test-sbyte");
3235                                                 Assert.AreEqual (r is int, true);
3236                                                 Assert.AreEqual ((int) r, 1);
3237                                                 r = createdKey.GetValue ("test-short");
3238                                                 Assert.AreEqual (r is int, true);
3239                                                 Assert.AreEqual ((int) r, 1);
3240                                                 r = createdKey.GetValue ("test-ushort");
3241                                                 Assert.AreEqual (r is int, true);
3242                                                 Assert.AreEqual ((int) r, 1);
3243                                                 r = createdKey.GetValue ("test-long");
3244                                                 Assert.AreEqual (r is int, true);
3245                                                 Assert.AreEqual ((int) r, 1);
3246                                                 r = createdKey.GetValue ("test-ulong");
3247                                                 Assert.AreEqual (r is int, true);
3248                                                 Assert.AreEqual ((int) r, 1);
3249
3250                                                 r = createdKey.GetValue ("dtest-int");
3251                                                 Assert.AreEqual (r is long, true);
3252                                                 Assert.AreEqual ((long) r, 1);
3253                                                 r = createdKey.GetValue ("dtest-uint");
3254                                                 Assert.AreEqual (r is long, true);
3255                                                 Assert.AreEqual ((long) r, 1);
3256                                                 r = createdKey.GetValue ("dtest-byte");
3257                                                 Assert.AreEqual (r is long, true);
3258                                                 Assert.AreEqual ((long) r, 1);
3259                                                 r = createdKey.GetValue ("dtest-sbyte");
3260                                                 Assert.AreEqual (r is long, true);
3261                                                 Assert.AreEqual ((long) r, 1);
3262                                                 r = createdKey.GetValue ("dtest-short");
3263                                                 Assert.AreEqual (r is long, true);
3264                                                 Assert.AreEqual ((long) r, 1);
3265                                                 r = createdKey.GetValue ("dtest-ushort");
3266                                                 Assert.AreEqual (r is long, true);
3267                                                 Assert.AreEqual ((long) r, 1);
3268                                                 r = createdKey.GetValue ("dtest-long");
3269                                                 Assert.AreEqual (r is long, true);
3270                                                 Assert.AreEqual ((long) r, 1);
3271                                                 r = createdKey.GetValue ("dtest-ulong");
3272                                                 Assert.AreEqual (r is long, true);
3273                                                 Assert.AreEqual ((long) r, 1);
3274                                                 r = createdKey.GetValue ("dtest-decimal");
3275                                                 Assert.IsTrue (r is long);
3276                                                 Assert.AreEqual ((long) r, 1);
3277                                                 r = createdKey.GetValue ("dtest-float");
3278                                                 Assert.IsTrue (r is long);
3279                                                 Assert.AreEqual ((long) r, 1);
3280                                                 r = createdKey.GetValue ("dtest-bool");
3281                                                 Assert.AreEqual (typeof (long), r.GetType ());
3282
3283                                                 try {
3284                                                         createdKey.SetValue ("test-int", uint.MaxValue, RegistryValueKind.DWord);
3285                                                         Assert.Fail ("#100");
3286
3287                                                         createdKey.SetValue ("test-int", ulong.MaxValue, RegistryValueKind.QWord);
3288                                                         Assert.Fail ("#101");
3289                                                 } catch (ArgumentException) {
3290                                                 }
3291                                                 
3292                                                 createdKey.Close ();
3293                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3294                                         }
3295                                 } finally {
3296                                         try {
3297                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3298                                                 if (createdKey != null) {
3299                                                         createdKey.Close ();
3300                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3301                                                 }
3302                                         } catch {
3303                                         }
3304                                 }
3305                         }
3306                 }
3307                 
3308                 [Test]
3309                 public void bug79059 ()
3310                 {
3311                         string subKeyName = Guid.NewGuid ().ToString ();
3312
3313                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3314                                 try {
3315                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3316                                                 using (RegistryKey softwareKey2 = Registry.CurrentUser.OpenSubKey ("software")) {
3317                                                 }
3318                                                 createdKey.Close ();
3319                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3320                                         }
3321                                 } finally {
3322                                         try {
3323                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3324                                                 if (createdKey != null) {
3325                                                         createdKey.Close ();
3326                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3327                                                 }
3328                                         } catch {
3329                                         }
3330                                 }
3331                         }
3332                 }
3333
3334                 [Test]
3335                 public void bugnew1 ()
3336                 {
3337                         string subKeyName = Guid.NewGuid ().ToString ();
3338
3339                         using (RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey ("software", true)) {
3340                                 try {
3341                                         using (RegistryKey createdKey = softwareKey.CreateSubKey (subKeyName)) {
3342                                                 createdKey.SetValue ("name1", "value1");
3343
3344                                                 RegistryKey testKey = null;
3345                                                 try {
3346                                                         testKey = createdKey.OpenSubKey ("test", true);
3347                                                         if (testKey == null)
3348                                                                 testKey = createdKey.CreateSubKey ("test");
3349                                                         testKey.SetValue ("another", "one");
3350                                                 } finally {
3351                                                         if (testKey != null)
3352                                                                 testKey.Close ();
3353                                                 }
3354
3355                                                 createdKey.SetValue ("name2", "value2");
3356                                                 Assert.IsNotNull (createdKey.GetValue ("name1"), "#2");
3357                                                 Assert.AreEqual ("value1", createdKey.GetValue ("name1"), "#3");
3358                                                 Assert.IsNotNull (createdKey.GetValue ("name2"), "#4");
3359                                                 Assert.AreEqual ("value2", createdKey.GetValue ("name2"), "#5");
3360
3361                                                 string [] names = createdKey.GetValueNames ();
3362                                                 Assert.IsNotNull (names, "#6");
3363                                                 Assert.AreEqual (2, names.Length, "#7");
3364                                                 Assert.AreEqual ("name1", names [0], "#8");
3365                                                 Assert.AreEqual ("name2", names [1], "#9");
3366
3367                                                 softwareKey.DeleteSubKeyTree (subKeyName);
3368
3369                                                 using (RegistryKey openedKey = softwareKey.OpenSubKey (subKeyName, true)) {
3370                                                         Assert.IsNull (openedKey, "#10");
3371                                                 }
3372
3373                                                 Assert.IsNull (createdKey.GetValue ("name1"), "#11");
3374                                                 Assert.IsNull (createdKey.GetValue ("name2"), "#12");
3375
3376                                                 try {
3377                                                         createdKey.GetValueNames ();
3378                                                         Assert.Fail ("#13");
3379                                                 } catch (IOException ex) {
3380                                                         // Illegal operation attempted on a registry key that
3381                                                         // has been marked for deletion
3382                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#14");
3383                                                         Assert.IsNotNull (ex.Message, "#15");
3384                                                         Assert.IsNull (ex.InnerException, "#16");
3385                                                 }
3386
3387                                                 try {
3388                                                         createdKey.SetValue ("name1", "value1");
3389                                                         Assert.Fail ("#17");
3390                                                 } catch (IOException ex) {
3391                                                         // Illegal operation attempted on a registry key that
3392                                                         // has been marked for deletion
3393                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#18");
3394                                                         Assert.IsNotNull (ex.Message, "#19");
3395                                                         Assert.IsNull (ex.InnerException, "#20");
3396                                                 }
3397
3398                                                 try {
3399                                                         createdKey.SetValue ("newname", "value1");
3400                                                         Assert.Fail ("#21");
3401                                                 } catch (IOException ex) {
3402                                                         // Illegal operation attempted on a registry key that
3403                                                         // has been marked for deletion
3404                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#22");
3405                                                         Assert.IsNotNull (ex.Message, "#23");
3406                                                         Assert.IsNull (ex.InnerException, "#24");
3407                                                 }
3408
3409                                                 Assert.IsNull (createdKey.OpenSubKey ("test"), "#25");
3410                                                 Assert.IsNull (createdKey.OpenSubKey ("test", true), "#26");
3411                                                 Assert.IsNull (createdKey.OpenSubKey ("new"), "#27");
3412                                                 Assert.IsNull (createdKey.OpenSubKey ("new", true), "#28");
3413
3414                                                 try {
3415                                                         createdKey.CreateSubKey ("new");
3416                                                         Assert.Fail ("#29");
3417                                                 } catch (IOException ex) {
3418                                                         // Illegal operation attempted on a registry key that
3419                                                         // has been marked for deletion
3420                                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#30");
3421                                                         Assert.IsNotNull (ex.Message, "#31");
3422                                                         Assert.IsNull (ex.InnerException, "#32");
3423                                                 }
3424                                         }
3425                                 } finally {
3426                                         try {
3427                                                 RegistryKey createdKey = softwareKey.OpenSubKey (subKeyName);
3428                                                 if (createdKey != null) {
3429                                                         createdKey.Close ();
3430                                                         softwareKey.DeleteSubKeyTree (subKeyName);
3431                                                 }
3432                                         } catch {
3433                                         }
3434                                 }
3435                         }
3436                 }
3437
3438                 [Test]
3439                 public void bugnew2 () // values cannot be written on registry root (hive)
3440                 {
3441                         string [] names = Registry.CurrentUser.GetValueNames ();
3442                         Assert.IsNotNull (names, "#1");
3443                         Registry.CurrentUser.SetValue ("name1", "value1");
3444                         Assert.IsNotNull (Registry.CurrentUser.GetValue ("name1"), "#2");
3445                         Assert.AreEqual ("value1", Registry.CurrentUser.GetValue ("name1"), "#3");
3446                         string [] newNames = Registry.CurrentUser.GetValueNames ();
3447                         Assert.IsNotNull (newNames, "#4");
3448                         Assert.AreEqual (names.Length + 1, newNames.Length, "#5");
3449                         Registry.CurrentUser.DeleteValue ("name1");
3450                 }
3451
3452                 [Test]
3453                 public void bugnew3 () // on Windows, key cannot be closed twice
3454                 {
3455                         string subKeyName = Guid.NewGuid ().ToString ();
3456
3457                         try {
3458                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3459                                         createdKey.Close ();
3460                                 }
3461
3462                                 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3463                                 openedKey.Close ();
3464                                 openedKey.Close ();
3465                         } finally {
3466                                 try {
3467                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3468                                         if (createdKey != null) {
3469                                                 createdKey.Close ();
3470                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3471                                         }
3472                                 } catch {
3473                                 }
3474                         }
3475                 }
3476
3477                 [Test]
3478                 public void bugnew4 () // Key cannot be flushed once it has been closed
3479                 {
3480                         string subKeyName = Guid.NewGuid ().ToString ();
3481
3482                         try {
3483                                 using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
3484                                         createdKey.Close ();
3485                                 }
3486
3487                                 RegistryKey openedKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3488                                 openedKey.Close ();
3489                                 openedKey.Flush ();
3490                         } finally {
3491                                 try {
3492                                         RegistryKey createdKey = Registry.CurrentUser.OpenSubKey (subKeyName);
3493                                         if (createdKey != null) {
3494                                                 createdKey.Close ();
3495                                                 Registry.CurrentUser.DeleteSubKeyTree (subKeyName);
3496                                         }
3497                                 } catch {
3498                                 }
3499                         }
3500                 }
3501
3502                 private bool RunningOnUnix {
3503                         get {
3504                                 int p = (int) Environment.OSVersion.Platform;
3505                                 return ((p == 4) || (p == 128) || (p == 6));
3506                         }
3507                 }
3508         }
3509 }