简体   繁体   English

在WPF中需要可更改的App.config文件

[英]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. 我有一个桌面WPF应用程序,它使用Entity Framework 4.1 Code First方法来管理数据。

EF adds a connection string to the App.config file and I wan't to be able to change this connection string at runtime. EF将连接字符串添加到App.config文件中,而我将无法在运行时更改此连接字符串。

Scenario is like this: 场景是这样的:

When I deploy the application it has a default connection string in the App.config file. 部署应用程序时,它在App.config文件中具有默认的连接字符串。 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. 用户运行该应用程序,并且由于该连接字符串可能对该用户无效(由于服务器名称,用户ID和密码),因此我将显示一个服务器配置窗口。

Here user will enter the valid information about his connection and press OK. 用户将在此处输入有关其连接的有效信息,然后按OK。 I will then be able to change the App.config file and save the user's new valid information to the file. 然后,我将能够更改App.config文件并将用户的新有效信息保存到该文件中。

Problems: 问题:

If I change it using ConfigurationManager, the changes will be temporary meaning that the file is not saved, changes are made in memory. 如果使用ConfigurationManager进行更改,则更改将是暂时的,这意味着不会保存文件,而是在内存中进行更改。

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. 如果我将App.config文件读入流中,在内存中进行必要的更改,删除物理文件,然后再次将内存流另存为App.config,Windows将不允许我对ProgramFiles文件夹下的文件进行更改。

What is would be the best approach here? 最好的方法是什么?

EDIT: Problem Again! 编辑:再次问题!

After I modify the App.config file with this method: 用此方法修改App.config文件后:

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." “拒绝访问“ C:\\ Program Files(x86)\\ MyCompany \\ MyApp \\ MyApp.exe.config”。

I know that files under "Program files" are immutable, so how can I handle this? 我知道“程序文件”下的文件是不可变的,那么我该如何处理?

I couldn't modify ConfigurationManager.ConnectionStrings["key"] object, because it is readonly. 我无法修改ConfigurationManager.ConnectionStrings["key"]对象,因为它是只读的。

So I decided to add a new connection string to my App.config file so it looks like this: 因此,我决定将一个新的连接字符串添加到我的App.config文件中,如下所示:

<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: 然后将我的DbContext构造函数更改为采用新添加的连接字符串,如下所示:

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

    }

Here, the value of name attribute at connection string and constructor must match. 在此,连接字符串和构造函数处的name属性值必须匹配。

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

App.config is not the proper place to do this since it's a global configuration used by the application. App.config不是执行此操作的适当位置,因为它是应用程序使用的全局配置。

I recommend you to save the settings per user. 我建议您保存每个用户的设置。 See this related question : c# - approach for saving user settings in a WPF application? 看到这个相关的问题: c#-在WPF应用程序中保存用户设置的方法?

App.config is the correct approach in my opinion, however I wouldn't rely on writing the file physically yourself. 我认为App.config 正确的方法,但是我不会自己亲自写文件。 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. 编辑:Glad Sandeep的上述评论对您有所帮助。 Feel free to check out that link too if you want a bit more information! 如果您需要更多信息,也可以随时查看该链接!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM