mercoledì 20 marzo 2013

C# - Getting Value From App.Config



public string GetValueFromConfig(string chiave)
{
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = @"YourAppFile.exe.config";

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            AppSettingsSection app = config.AppSettings;
            return app.Settings[chiave].Value;
        }

Update App.Config Programmatically




ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"YourAppFile.exe.config";

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

AppSettingsSection app = config.AppSettings;

app.Settings.Remove("key ");
app.Settings.Add("key", "value");

config.Save(ConfigurationSaveMode.Modified);