title: C# Windows registry GetValue Error
body: <p>I have a program which outputs the various registry values from "Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU".</p>
  
  <p>However the program outputs an error on Cannot implicity convert type'object' to 'string' at the s variable at the GetValue portion or the program! And the program outputs an error of "Cannot access a closed registry key too".</p>
  
  <p>Can someone please give advise on the codes? Thanks!</p>
  
  <p>The Code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Microsoft.Win32;
  
  namespace RegKeys
  {
  class ConsoleApplication1
  {
      static void Main(string[] args)
      {
          try
          {
              RegistryKey rk = Registry.CurrentUser;
  
              rk = rk.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU", false);
              PrintKeys(rk);
          }
  
          catch (Exception MyError)
          {
              Console.WriteLine("An error has occurred: " + MyError.Message);
          }
      }
  
      static void PrintKeys(RegistryKey rk)
      {
          if (rk == null)
          {
              Console.WriteLine("No specified registry key!");
              return;
          }
  
          String[] names = rk.GetValueNames();
  
          Console.WriteLine("Subkeys of " + rk.Name);
          Console.WriteLine("-----------------------------------------------");
  
          foreach (String s in names)
          {
              try
              {
                  if (s == "MRUList")
                  {
                      continue;
                  }
  
                  else
                  {
                      String val = rk.GetValue(s);
                      Console.WriteLine(s + " Contains the value of : " + val);
                  }
  
                  rk.Close();
              }
  
              catch (Exception MyError)
              {
                  Console.WriteLine("An error has occurred: " + MyError.Message);
              }
  
              Console.WriteLine("-----------------------------------------------");
              rk.Close();
          }
      }
  }
  }
  </code></pre>