簡體   English   中英

如何使用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";

想要獲取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");
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM