使用 ConfigurationManager 将用户值存储在与应用程序配置分开的文件中
好吧,我已经快要放弃这个了。
我希望能够使用用户配置文件记录用户首选项,该文件将从应用程序配置文件中引用。我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将外部配置文件加载到“配置”实例中。下面是一个带有使用此策略的静态构造函数的单例类的示例。我想,你可以稍微调整它来做你想做的事。
也许尝试以这种方式添加,确保调用 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.
Maybe try adding this way, making sure to call Save()
值得赞扬的是,此代码在这里有效。
另外,我还创建了一个简单的 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.