开发者

How to Load & Save to val's in app.config file?

开发者 https://www.devze.com 2022-12-19 16:00 出处:网络
How to Load & Save to values in app.config file ? For example: to save & to 开发者_开发问答load Connection stringYou use the ConfigurationManager class, which has a Save method.That documenta

How to Load & Save to values in app.config file ?

For example: to save & to 开发者_开发问答load Connection string


You use the ConfigurationManager class, which has a Save method. That documentation page has a comprehensive example of loading custom configuration sections and saving values.


Try this

public static string ConnectionString
{
        get
        {
            try
            {
                return ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString;
            }
            catch
            {
                return "";
            }
        }
        set
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings["YourConnectionStringName "].ConnectionString = value;
            config.Save(ConfigurationSaveMode.Minimal, true);
            //refresh so that you can use the updates value directly without the need to restart the application
            System.Configuration.ConfigurationManager.RefreshSection("connectionStrings");
        }
    }
0

精彩评论

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