将更改保存到自定义部分的 configSource 属性中指定的外部配置文件
我已在 App.config 文件中定义了一个自定义部分,并且该 customSection 的所有配置属性均已在 configSource 文件中指定的外部配置文件中定义。
现在的情况是,每当我运行程序时,我都会修改外部配置文件中存在的属性值,并且我需要一种方法将这些值保存在外部配置文件中。
如何保存这些值?使用正常方式写入文件是唯一的选择吗?
关于我想在应用程序中执行的操作,请考虑以下场景:
App.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="example" type="CustomConfig.ExampleSection,
CustomConfig" />
</configSections>
<example
configSource="example.config"
/>
<appSettings>
<add key="version_string" value="1.01" />
</appSettings>
</configuration>
example.config
<?xml version="1.0"?>
<example version="A sample string value."/>
假设 version 的值在程序执行期间更改为“Foo”。如何将该值永久保存在 example.config 文件中,以便当我退出应用程序并再次重新加载时,版本的值将为 Foo。
I have defined a custom section in the App.config file and all the Configuration properties present for the customSection have been defined in an external config file specified in configSource file.
Now, the scenarios is that whenever I run the program I modify the values of the properties present in the external config file and I need a way to save these values in the external config file.
How would it be possible to save these values? Is using the normal ways to write to a file the only option?
Consider the following scenario as to what I want to do in the application:
App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="example" type="CustomConfig.ExampleSection,
CustomConfig" />
</configSections>
<example
configSource="example.config"
/>
<appSettings>
<add key="version_string" value="1.01" />
</appSettings>
</configuration>
example.config
<?xml version="1.0"?>
<example version="A sample string value."/>
Suppose the value of version is changed to "Foo" during program execution. How can I save that value in example.config file permanently so that when I exit the application and reload it again, the value of version will be Foo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做。
配置c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
c.AppSettings.Settings["您的配置"].Value=....;
c.Save(ConfigurationSaveMode.Modified);
You can do it like this.
Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
c.AppSettings.Settings["Your config"].Value=....;
c.Save(ConfigurationSaveMode.Modified);