title: How To Create Registry Keys Using Microsoft.Win32.Registry C#
body: <p>I am wanting to write directly to the registry using <code>Microsoft.Win32.Registry</code>. I can do this is a reg file like so:</p>
  
  <pre><code>swreg = File.AppendText(strRegPath); //Opens the file:
  
  swreg.WriteLine(@"[HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\OMI Account Manager\Accounts\user@domain.com");
  swreg.WriteLine("\"DCEmail\"=dword:00000002");
  swreg.WriteLine("\"POP3 Server\"=\"10.0.0.200\""); 
  swreg.WriteLine("\"POP3 Port\"=dword:0000006e");
  </code></pre>
  
  <p>This creates a reg file, and I can execute the file which create the reg keys. I have tried to something similar using <code>Microsoft.Win32.Registry</code> like so:</p>
  
  <pre><code>var RKOutlook = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\Outlook\OMI Account Manager\Accounts");
  
  if (RKOutlook.OpenSubKey("user@domain.com") == null)
  {
      RKOutlook.CreateSubKey("user@domain.com");
      RKOutlook = RKOutlook.OpenSubKey("user@domain.com", true);
  }
  </code></pre>
  
  <p>However I receive an <code>System.NullReferenceException was unhandled</code> error. How can I achieve writing directly to the registry without using a reg file?</p>