C# 读取,删除,增加,修改app.config配置文件信息

您所在的位置:网站首页 怎么取消config C# 读取,删除,增加,修改app.config配置文件信息

C# 读取,删除,增加,修改app.config配置文件信息

2024-07-13 20:28| 来源: 网络整理| 查看: 265

///

/// 修改和添加AppSettings中配置 如果相应的Key存在则修改 如不存在则添加

///

/// key值

/// 相应值

public static bool SetConfigValue(string key, string value){

    try {

        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        if (config.AppSettings.Settings[key] != null){

            config.AppSettings.Settings[key].Value = value;

        }else{

            config.AppSettings.Settings.Add(key, value);

        }

 

        config.Save(ConfigurationSaveMode.Modified);

        ConfigurationManager.RefreshSection("appSettings");

        return true;

    }catch{

        return false;

    }

}

 

///

/// 删除

///

///

public void removeItem(string keyName){

    //删除配置文件键为keyName的项 

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    config.AppSettings.Settings.Remove(keyName);

    config.Save(ConfigurationSaveMode.Modified);

    ConfigurationManager.RefreshSection("appSettings");

}

 

///

/// 查看相应Key的Value

///

///

///

public string valueItem(string keyName){

    //返回配置文件中键为keyName的项的值 

    return ConfigurationManager.AppSettings[keyName];

}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3