I don’t have anything to post , but I think this might be useful for you. Want to write and delete appSettings in App.Config or Web.Config? Sure you do know how to read it.
Hope this little code helps
Code Snippet
- namespace Rdz.Utility
- {
- public static class ConfigUtility
- {
- /// <summary>
- /// Writes to configuration file (App.Config) to specified key, or creates a new Key if doesn't exist
- /// </summary>
- /// <param name="KeyName">The Application Settings KeyName</param>
- /// <param name="Value">The Application Settings Value</param>
- public static void WriteToAppConfig(string KeyName, string Value)
- {
- Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (cfg.AppSettings.Settings.AllKeys.Contains(KeyName))
- cfg.AppSettings.Settings[KeyName].Value = Value;
- else
- cfg.AppSettings.Settings.Add(KeyName, Value);
- cfg.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");
- }
- /// <summary>
- /// Deletes specified key in app.config
- /// </summary>
- /// <param name="KeyName"></param>
- public static void DeleteAppConfig(string KeyName)
- {
- Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (cfg.AppSettings.Settings.AllKeys.Contains(KeyName))
- {
- cfg.AppSettings.Settings.Remove(KeyName);
- cfg.Save(ConfigurationSaveMode.Modified);
- }
- ConfigurationManager.RefreshSection("appSettings");
- }
- }
- }
Cheers… and happy coding!
Tidak ada komentar:
Posting Komentar
[give me your ideas, hopes, comments and compliments below...]
[aaand...... +1 if you need to....]