如何装载&保存到app.config文件中的val?

时间:2010-02-10 18:58:55

标签: c# .net

如何装载&保存到app.config文件中的值?

例如:保存&加载连接字符串

2 个答案:

答案 0 :(得分:1)

您使用具有Save方法的ConfigurationManager类。该文档页面提供了加载自定义配置节和保存值的综合示例。

答案 1 :(得分:-1)

试试这个

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