title: How to change an application's exe.config file when it is being installed using Windows Installer
body: <p>I have created a Windows Service application.</p>
  
  <p>I have also created a Windows Installer for the Service Application.</p>
  
  <p>On the installer project, I have added a custom screen that presents users with a screen to enter values (right click setup project -> View -> User Interface -> Right click the 'Start' section -> Add dialog -> Text boxes -> OK)</p>
  
  <p>I have added the property as an argument to the custom action.</p>
  
  <p>I have added code to:</p>
  
  <pre><code>private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
  </code></pre>
  
  <p>and was able to write the custom value entered using the installation process to a text file.</p>
  
  <pre><code>private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
  {
      string path = @"c:\test\123.txt";
  
      if (!File.Exists(path))
      {
          File.Create(path).Dispose();
          using (TextWriter tw = new StreamWriter(path))
          {
              tw.WriteLine("The setting is: " + Context.Parameters["PathValue"]);
              tw.Close();
          }
  
      }
  
      else if (File.Exists(path))
      {
          using (TextWriter tw = new StreamWriter(path))
          {
              tw.WriteLine("The setting is: " + Context.Parameters["PathValue"]);
              tw.Close();
          }
      }
  }
  </code></pre>
  
  <p><strong>Now, I only want to write the value into the application's applicationame.exe.config file.</strong> </p>
  
  <p>I have created a config file for the service using the Project Properties window: <a href="https://i.stack.imgur.com/JkIDf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JkIDf.png" alt="enter image description here"></a></p>
  
  <p>Note: After the screenshot was taken, I also tried changing the <code>Access Modifier</code> on the top right of the screen from <code>Internal</code> to <code>Public</code> and still the same result.</p>
  
  <p>The applicationname.exe.config file generated is as below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
      &lt;configSections&gt;
          &lt;sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" &gt;
              &lt;section name="WindowsService2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
          &lt;/sectionGroup&gt;
      &lt;/configSections&gt;
      &lt;startup&gt; 
          &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /&gt;
      &lt;/startup&gt;
      &lt;applicationSettings&gt;
          &lt;WindowsService2.Properties.Settings&gt;
              &lt;setting name="setting1" serializeAs="String"&gt;
                  &lt;value&gt;default111&lt;/value&gt;
              &lt;/setting&gt;
          &lt;/WindowsService2.Properties.Settings&gt;
      &lt;/applicationSettings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>How can I write to this without manually parsing the XML?</strong></p>
  
  <p>Note that my applicationname.exe.config file is <strong>not</strong> in the format: <code>&lt;add key="setting1" value="default111" /&gt;</code> for some reason. </p>
  
  <p>Also, I don't have a app.config file in the bin folder. I only have applicationname.exe.config.</p>
  
  <p><a href="https://i.stack.imgur.com/JWuF1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JWuF1.png" alt="enter image description here"></a></p>
  
  <p>I've tried this. But this doesn't update the application.exe.config file nor does it throw any errors.</p>
  
  <pre><code>private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
  {
  
      Configuration config = ConfigurationManager.OpenExeConfiguration(@System.Reflection.Assembly.GetExecutingAssembly().Location);
      string value = System.Configuration.ConfigurationManager.AppSettings["setting1"];
      System.Configuration.ConfigurationManager.AppSettings["setting1"] = Context.Parameters["PathValue"];
      config.Save(ConfigurationSaveMode.Modified);
  
  }
  </code></pre>
  
  <p>I've also tried the following and it didn't work (no error messages though):</p>
  
  <pre><code>System.Configuration.ConfigurationManager.AppSettings.Set("setting1", Context.Parameters["PathValue"]);
  </code></pre>
  
  <p>Please be kind enough to help me figure out how I can write the value to the applicationame.exe.config file without parsing the XML file.</p>