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