Editing app.config in execution time using the same App

ぃ、小莉子 提交于 2019-12-24 09:49:15

问题


I have an Windows Forms application VS 2008 - C#, that uses app.config.

In execution time, in Menu option of my application, I want editing values of app.config, save it and restart application.

any sample source code, any good patterns and practices ??

edit:

in MSDN Forums, Jean Paul VA:

  1. Create an test windows forms application and add an app.config into it.

  2. Add reference to System.confguration

  3. Add a key named "font" in appSettings with value "Verdana"

  4. Place a button on form and on click of it add the modification code.

        System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
        configuration.AppSettings.Settings.Remove("font");
        configuration.AppSettings.Settings.Add("font", "Calibri");
    
        configuration.Save(ConfigurationSaveMode.Modified);
    
        ConfigurationManager.RefreshSection("appSettings");
    

what you think about it ?


回答1:


I don't think you can actually write to the configuration file at runtime - it may well be read-only, however, there may be a way around this by re-writing the file (with proper alterations as required) and essentially replacing the existing one, then, further, restarting the application to load the new values (I highly doubt this is desirable and personally would not try to instrument this malarkey).

You may also just consider storing application settings in the settings file which are easily manipulated in this way.

To use settings, first let's assume you have a Settings.settings file (if not then create one: Add Item->Settings File), then we have a setting configured named MyUnicornsName; In order to make a change and persist it you can simply do as follows:

Settings.Default.MyUnicornsName = "Lucifers Spawn";
Settings.Default.Save();

Similarly, to read a setting:

ATextDisplayControl.Text = Settings.Default.MyUnicornsName

In case you don't know, Visual Studio will open the settings editor when you open/double click the settings file in the IDE; using this interface you can add and edit your initial settings and their values, and string is not the only supported value, all primitives can be used, and, as far as I know, any serializable value too.




回答2:


Is there any reason you can't use the usual auto-gen'd Properties.Settings to store the changing data in a settings file instead? One great thing is that you know what you're changing so you don't even have to restart the application!

Using Settings in C#

Runtime access of settings is as easy as:

this.BackColor = Properties.Settings.Default.myColor;

(There is no good pattern for modifing app.config itself simply b/c it's designed to be readonly in that context, with expert-user settings.)




回答3:


Use the Project->Properties->Settings for these kinds of things.




回答4:


well actually the Properties in the App.config are ReadOnly so u cant do it.

But there's a trick............................

In the Settings.cs file create a Function or method that is public so that it can be available with Properties.settings

and write the following code..

    public void ChangeProperty(string propertyname, string value)
    {
        this[propertyname] = value;
    }

remember to pass the exact string of the property name to the method. or better create a Writeonly Property for the setting.

update

here is a code for setting as a property, i am taking a connection string as an example, bet it can be anything. remember the property stored is of Object type so you can create property specific to that..

    public string MyCustomConnectionstring
    {
        set
        {
            //replace the string with your connection string otr what ever setting you want to change
            this["myConnectionString"] = value;
        }
    }

Now you can easily use this Property to change the ConnectionString at run time...



来源:https://stackoverflow.com/questions/4679967/editing-app-config-in-execution-time-using-the-same-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!