类库项目中的 .NET 设置文件

本文关键字:设置 文件 NET 项目 类库 | 更新日期: 2023-09-27 18:37:15

我想在外部dll库中使用.NET设置文件。这篇文章准确地解释了我正在尝试做什么。

partial class LastState
{
    public LastState() : base(new ConfigurationFileApplicationSettings("LastState.config", typeof(LastState))) { }
}

遗憾的是,使用此实现,无法将设置保存回配置文件。如果我尝试使用 Save() ,SetPropertyValues 会抛出一个 NotSupportedException。有没有办法从外部dll库中保存.NET设置文件?

类库项目中的 .NET 设置文件

我会使用自定义配置文件。

ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"d:'test'justAConfigFile.config.whateverYouLikeExtension";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel)

请在此处查看更多内容。

您可以通过以下方式保存

config.AppSettings.Settings["YourThing"].Value = "New Value";
config.Save();