public class AppSettingManager
{
public static bool Update(string key, string value)
{ try
{
var config = Create();
if (config == null)
{
return false;
}
var isModified = !string.IsNullOrEmpty(Get(key));
if (isModified)
{
config.AppSettings.Settings.Remove(key);
}
// Add an Application Setting.
config.AppSettings.Settings.Add(key, value);
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch (Exception)
{
return false;
} } public static bool Add(string key, string value)
{
try
{
var config = Create();
if (config == null)
{
return false;
}
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch (Exception)
{
return false; } } public static string Get(string key)
{
var config = Create();
if (config == null)
{
return null;
}
return config.AppSettings.Settings[key].Value;
} private static Configuration Create()
{
try
{
return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
catch (Exception)
{
return null;
}
}
}
04-26 10:43