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

本文关键字:配置文件 获取 appSettings 何使用 winform | 更新日期: 2023-09-27 18:15:54

我给了.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的值

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

我终于可以管理它了,发帖可以帮助别人

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