C# applicationSettings: how to update app.config?

后端 未结 4 986
滥情空心
滥情空心 2020-12-10 19:13

I am using .NET 4.0 and I would like to use the app.config file to store same parameter settings. I do the following. I use the Settings tab in the

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 19:39

    You should use Properties.Settings to perform this action. You can look at this documentation for more information.

    //modify
    Properties.Settings.Default.param1 = false;
    //save the setting
    Properties.Settings.Default.Save();
    

    Note that if your settings have the Scope of User, which they must be to be writeable, then the properties are saved somewhere else and not in your local config file. See here for the details.

    EDIT after discussion in comment and further searches:

    My suggestion to achieve the desired result would be to switch to AppSettings. That is because, after some searches, i found out that appsettings changes the .config file in the Application Data folder (running some tests on my machine confirm that).

    Look at comment in the answer to this question .

    I am not sure if there is some work around, but if you want your application to have a portable app.config file, i think the only way is to switch to AppSettings which i'm sure can save changes in the app.config found in the program folder.

    EDIT 2: Possible solution

    I found out a possible solution to make your app portable! You can change the Provider used by Settings to save the application's settings creating a custom Provider.

    The answer to this question provide a link to a code to make applicationsettings portable. I think you give it a try

提交回复
热议问题