Monday, May 11, 2009

C# - How to read or modify value of a key in app.Config


using System;
using System.Text;
using System.Configuration;
using System.Collections.Generic;

namespace myApp
{
class theAppCnfg
{
public static string GetKey( string keyName )
{
string retVal = string.Empty;
AppSettingsReader appCnfgRdr = new AppSettingsReader();
retVal = (string) appCnfgRdr.GetValue(keyName,typeof(string));

return retVal;
}

public static void ModifyKey(string keyName, string keyValue)
{
Configuration cnfg =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None );
cnfg.AppSettings.Settings[keyName].Value = keyValue;
cnfg.Save(ConfigurationSaveMode.Modified);
}

}
}

Share/Bookmark

No comments: