title: C# Configuration Files
body: <p>Okay, so a while ahead I posted how to read other config files of other programs (here is the link <a href="https://stackoverflow.com/questions/938585/open-other-programs-configuration-files">Previous Post</a>. I managed to do it. But now there is another problem. The scenario is like this, I have two programs. Program <strong>A</strong> reads its configuration from a config file and program <strong>B</strong> is only used to modify the contents of the config file which <strong>A</strong> reads. The name of the config file is <em>email.config</em>. It is in the same directory in which program <strong>A</strong> &amp; <strong>B</strong> resides.</p>
  
  <p>The problem is that I get path of a file for attachment using open file dialog. If the path points to a file in the same directory, the program works perfect! But if it points to a file outside the directory it throws an exception of type <em>System.NullReferenceException</em>.</p>
  
  <p>Here is the code</p>
  
  <pre><code>private void saveBtn_Click(object sender, EventArgs e)
  {
      try
      {
          // save everything and close
          string attachment = attachTxtBox.Text;
  
          var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFileName };
          // it throws exception here when
          // the path points to a file outside the exes directory
          Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
  
          externalConfig.AppSettings.Settings["ServerAddress"].Value = serverAddr;
          externalConfig.AppSettings.Settings["Port"].Value = port;
          externalConfig.AppSettings.Settings["SSL"].Value = ssl.ToString();
          externalConfig.AppSettings.Settings["Sender"].Value = senderAddr;
          externalConfig.AppSettings.Settings["SenderPassword"].Value = password;
          externalConfig.AppSettings.Settings["Subject"].Value = subject;
          externalConfig.AppSettings.Settings["AttachmentPath"].Value = attachment;
          externalConfig.AppSettings.Settings["Body"].Value = messageBody;
  
          // Save values in config
          externalConfig.Save(ConfigurationSaveMode.Full);
          Application.Exit();
      }
      catch (System.Exception ex)
      {
          MessageBox.Show("Error: " + ex.Message);
          Application.Exit();
      }
  }
  </code></pre>
  
  <p>The content of <em>email.config</em> is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;appSettings file=""&gt;
      &lt;clear /&gt;
      &lt;add key="ServerAddress" value="" /&gt;
      &lt;add key="Port" value="" /&gt;
      &lt;add key="Sender" value="" /&gt;
      &lt;add key="SenderPassword" value="" /&gt;
      &lt;add key="Subject" value="" /&gt;
      &lt;add key="AttachmentPath" value="" /&gt;
      &lt;add key="Body" value="" /&gt;
    &lt;/appSettings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>What am I doing wrong here?</p>
  
  <p><strong>EDIT:</strong> The value of <em>configFileName</em> is <em>"email.config"</em></p>