public class ConfigTools
{
public static string GetKey(string key, string defaultvalue)
{
if (HasKey(key))
{
string value = WebConfigurationManager.AppSettings[key];
return value;
}
return defaultvalue;
}
public static bool SetKey(string key, string value)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
if (!HasKey(key))
{
config.AppSettings.Settings.Add(key, value);
}
else
{
config.AppSettings.Settings[key].Value = value;
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
public static bool HasKey(string key)
{
foreach (var item in WebConfigurationManager.AppSettings.Keys)
{
string itemkey = ConvertTools.ToString(item);
if (itemkey == key)
{
return true;
}
}
return false;
}
}