开发者

Editing app.config in execution time using the same App

开发者 https://www.devze.com 2023-02-05 00:28 出处:网络
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.

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.Configurat开发者_JS百科ion 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 ?


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.


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.)


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


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号