如何使用 c# winform 从给定配置文件的 appSettings 获取值?
how to get value from appSettings for given config file using c# winform?
我正在提供 .config
文件路径,我想从给定的 .config
文件中检索 key=MYDATA
的 appSetting 值。
我尝试了以下代码,但没有达到预期效果。
//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//var classLibrary1AppSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
//config.AppSettings.File = "C:\mydemo\web.config";
want to get value for key=MYDATA
终于搞定了,发帖对大家有帮助
System.Configuration.ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "C:\mydemo\web.config";
System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
AppSettingsSection section = (AppSettingsSection)configuration.GetSection("appSettings");
if (section.Settings.AllKeys.Any(key => key == "MYDATA"))
{
section.Settings["MYDATA"].Value = updateConfigId;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
我正在提供 .config
文件路径,我想从给定的 .config
文件中检索 key=MYDATA
的 appSetting 值。
我尝试了以下代码,但没有达到预期效果。
//System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//var classLibrary1AppSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
//config.AppSettings.File = "C:\mydemo\web.config";
want to get value for
key=MYDATA
终于搞定了,发帖对大家有帮助
System.Configuration.ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "C:\mydemo\web.config";
System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
AppSettingsSection section = (AppSettingsSection)configuration.GetSection("appSettings");
if (section.Settings.AllKeys.Any(key => key == "MYDATA"))
{
section.Settings["MYDATA"].Value = updateConfigId;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}