使用 ConfigurationManager 将用户值存储在与应用程序配置分开的文件中

发布于 2024-12-06 05:15:40 字数 1832 浏览 3 评论 0原文

好吧,我已经快要放弃这个了。

我希望能够使用用户配置文件记录用户首选项,该文件将从应用程序配置文件中引用。我正在尝试使用 ConfigurationManager 和应用程序配置文件来执行此操作。我可以很好地读取用户设置,但设置它们是一个完全不同的问题。我想将应用程序设置与用户设置分开保存在两个不同的文件中。



当我使用这个:

<appSettings file="user.config">
  </appSettings>

并且 user.config 看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="localSetting" value="localVal"/>
</appSettings>

我可以使用 ConfigurationManager 读取本地设置,但不能保存到文件中。

var oldLocVal = ConfigurationManager.AppSettings["localSetting"]; 
ConfigurationManager.AppSettings.Set("localSetting", "newLocalValue"); // Doesn't save to file.



If instead my user.config file is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="localSetting" value="localVal"/>
  </appSettings>
</configuration>

然后我想以这种方式调用并保存 AppSettings:

var uMap = new ConfigurationFileMap("user.config");
var uConfig = ConfigurationManager.OpenMappedMachineConfiguration(uMap);
var oldLocalVarFromConfig = uConfig.AppSettings.Settings["localSetting"]; // NO
uConfig.AppSettings.Settings.Remove("localSetting");                      // NO
uConfig.AppSettings.Settings.Add("localSetting", "newValue");
uConfig.Save();

但它不会让我访问配置的应用程序设置。 (将某些内容转换为 AppSettings 时出现问题)



I also tried with configSource instead of file attributes in the app config appSettings element.

我在此处使用示例< /a> 寻求帮助,但不幸的是这还不够。

提前致谢。

Ok, I've just about given up on this.

I would like to be able to record user preferences using a user config file, which would be referenced from the app config file. I am trying to do this with ConfigurationManager and an app config file. I can read just fine from the user settings, but setting them is a whole other problem. I would like to keep the app settings separate from the user settings in two different files.



When I use this:

<appSettings file="user.config">
  </appSettings>

and user.config looks like:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="localSetting" value="localVal"/>
</appSettings>

I can use ConfigurationManager to READ the local setting, but not to save to the file.

var oldLocVal = ConfigurationManager.AppSettings["localSetting"]; 
ConfigurationManager.AppSettings.Set("localSetting", "newLocalValue"); // Doesn't save to file.


If instead my user.config file is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="localSetting" value="localVal"/>
  </appSettings>
</configuration>

I would then like to call and save the AppSettings in this way:

var uMap = new ConfigurationFileMap("user.config");
var uConfig = ConfigurationManager.OpenMappedMachineConfiguration(uMap);
var oldLocalVarFromConfig = uConfig.AppSettings.Settings["localSetting"]; // NO
uConfig.AppSettings.Settings.Remove("localSetting");                      // NO
uConfig.AppSettings.Settings.Add("localSetting", "newValue");
uConfig.Save();

but it won't let me access the configuration's app settings. (It has a problem casting something as an AppSettings)



I also tried with configSource instead of file attributes in the app config appSettings element.

I was using the examples here for help, but unfortunately it wasn't enough.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-12-13 05:15:40

您可以将外部配置文件加载到“配置”实例中。下面是一个带有使用此策略的静态构造函数的单例类的示例。我想,你可以稍微调整它来做你想做的事。

  private const string _path = @"E:\WhateverPath\User.config"

  static ConfigManager()        
    { 
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap()          
        { 
            ExeConfigFilename = _path
        }; 

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

        // get some custom configuration section
        _someConfigSection = config.GetSection("SomeSection") as SomeSection; 

        // or just get app settings 
        _appSettingSection = config.AppSettings; 
    }

也许尝试以这种方式添加,确保调用 Save()

_appSettingSection.Settings.Add("SomeKey", "SomeValue"); 

//make sure to call save
config.Save(); 

You can load external config files into a 'Configuration' instance. Here is an example of a singleton class with a static constructor that uses this strategy. You can tweak it a bit to do what you want, I think.

  private const string _path = @"E:\WhateverPath\User.config"

  static ConfigManager()        
    { 
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap()          
        { 
            ExeConfigFilename = _path
        }; 

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

        // get some custom configuration section
        _someConfigSection = config.GetSection("SomeSection") as SomeSection; 

        // or just get app settings 
        _appSettingSection = config.AppSettings; 
    }

Maybe try adding this way, making sure to call Save()

_appSettingSection.Settings.Add("SomeKey", "SomeValue"); 

//make sure to call save
config.Save(); 
琴流音 2024-12-13 05:15:40

值得赞扬的是,此代码在这里有效

另外,我还创建了一个简单的 XML 可序列化对象,每当调用 Save 时,该对象都会将自身保存到磁盘。

To its credit, this code here works.

Also, I made a simple XML serializable object that saved itself to disk any time Save was called on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文