Mert Akcakaya
Mert Akcakaya

Reputation: 3129

Need changable App.config file in WPF

I have a desktop WPF application which uses Entity Framework 4.1 Code First approach to manage data.

EF adds a connection string to the App.config file and I wan't to be able to change this connection string at runtime.

Scenario is like this:

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.

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.

Problems:

If I change it using ConfigurationManager, the changes will be temporary meaning that the file is not saved, changes are made in memory.

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.

What is would be the best approach here?

EDIT: Problem Again!

After I modify the App.config file with this method:

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");   
}

config.Save(); method call throws an error saying

"Access to "C:\Program Files (x86)\MyCompany\MyApp\MyApp.exe.config" is denied."

I know that files under "Program files" are immutable, so how can I handle this?

Upvotes: 0

Views: 6780

Answers (3)

Mert Akcakaya
Mert Akcakaya

Reputation: 3129

I couldn't modify ConfigurationManager.ConnectionStrings["key"] object, because it is readonly.

So I decided to add a new connection string to my App.config file so it looks like this:

<connectionStrings>
<add name="SomeUniqueName" connectionString="Data Source=(local)\SQLExpress;Initial Catalog=MyDb;User Id=sa;Password=password; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

and then changed my DbContext constructor to take newly added connection string like this:

public MyContext()
        : base("name=SomeUniqueName")
    {

    }

Here, the value of name attribute at connection string and constructor must match.

Then to change this newly added connection string at runtime I used a method like this:

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();
        ConfigurationManager.RefreshSection("connectionStrings");   
    }

Upvotes: 2

Nick
Nick

Reputation: 2325

App.config is the correct approach in my opinion, however I wouldn't rely on writing the file physically yourself. Instead, allow the framework to do the heavy lifting for you.

Check out the sample here.

EDIT: Glad Sandeep's comment above has helped you. Feel free to check out that link too if you want a bit more information!

Upvotes: 0

lstern
lstern

Reputation: 1629

App.config is not the proper place to do this since it's a global configuration used by the application.

I recommend you to save the settings per user. See this related question : c# - approach for saving user settings in a WPF application?

Upvotes: 0

Related Questions