简体   繁体   English

如何使用c#winform从appSettings中获取给定配置文件的值?

[英]how to get value from appSettings for given config file using c# winform?

I'm giving .config file path and i want to retrieve appSetting value for key=MYDATA from that given .config file. 我正在提供.config文件路径,我想从给定的.config文件中检索key=MYDATA appSetting值。

I tried following code but not getting expected. 我尝试了以下代码,但并没有得到期望。

//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 想要获取key=MYDATA

Finally I able to manage it , posting will help others 最后,我能够管理它,发布将对他人有帮助

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");
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM