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