title: Need changable App.config file in WPF
body: <p>I have a desktop WPF application which uses Entity Framework 4.1 Code First approach to manage data. </p>
  
  <p>EF adds a connection string to the App.config file and I wan't to be able to change this connection string at runtime.</p>
  
  <p>Scenario is like this: </p>
  
  <p>When I deploy the application it has a default connection string in the App.config file. The user runs the application and since the connection string will probably be invalid for the user (because of server name, user id and password) I will display a server configuration window.</p>
  
  <p>Here user will enter the valid information about his connection and press OK. I will then be able to change the App.config file and save the user's new valid information to the file.</p>
  
  <p>Problems: </p>
  
  <p>If I change it using ConfigurationManager, the changes will be temporary meaning that the file is not saved, changes are made in memory.</p>
  
  <p>If I read the App.config file into a stream, make required changes in the memory, delete physical file and save the in memory stream as App.config again, Windows will not let me make changes to files under ProgramFiles folder.</p>
  
  <p>What is would be the best approach here?</p>
  
  <p><strong>EDIT: Problem Again!</strong></p>
  
  <p>After I modify the App.config file with this method:</p>
  
  <pre><code>private void ApplyChangesToConnectionString()
  {
      var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
      connectionStringsSection.ConnectionStrings["SomeUniqueName"].ConnectionString = GetChangesAppliedConnectionString(connectionStringsSection.ConnectionStrings["SomeUniqueName"].ConnectionString);
      config.Save(); // This line throws an exception
      ConfigurationManager.RefreshSection("connectionStrings");   
  }
  </code></pre>
  
  <p><code>config.Save();</code> method call throws an error saying </p>
  
  <blockquote>
    <p>"Access to "C:\Program Files (x86)\MyCompany\MyApp\MyApp.exe.config"
    is denied."</p>
  </blockquote>
  
  <p>I know that files under "Program files" are immutable, so how can I handle this?</p>