我可以修改[APPNAME] .exe.config无需手动读/写XML? [英] Can I modify [appname].exe.config without having to manually read/write the XMl?

查看:134
本文介绍了我可以修改[APPNAME] .exe.config无需手动读/写XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用.NET 3.5和Visual Studio 2008的前preSS在C#应用程序的某些设置。我有一些应用程序范围的设置,我希望能够在应用程序中修改 - 我可以通过 Properties.Settings.Default 访问他们,但他们读只有符合市场预期。我不希望有让这些成为用户范围设置,他们应该是应用程序范围内。这是可能的,而无需加载XML和读/写从/到它自己?

我已经看到了使用例子 System.Configuration.ConfigurationManager.OpenExeConfiguration ,但XML配置看起来是在我使用(这是从不同的格式的一个旧版本?)

感谢


修改

我的工作了,我可以修改这样的值,但它似乎是一个荒谬的黑客攻击。

 配置配置= ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
SettingElementCollection settingElements =((ClientSettingsSection)config.GetSectionGroup(的applicationSettings)第[0]。)设定。

SettingElement元= settingElements.Get(SettingName);
element.Value.ValueXml.InnerText =新价值;

config.Save(ConfigurationSaveMode.Modified,真正的);
 

解决方案

<一个href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openexeconfiguration.aspx"相对=nofollow> OpenExeConfiguration (任何其他 ConfigurationManager中方法或)是preferred切入点大多数修改配置文件。一旦你有一个配置情况下,你应该让你要修改的部分和之后的修改调用任何的 ConfigurationManager.Save 方法。然而,这是不可能的检索的applicationSettings 部分这样的。

有从你的的app.config 文件中的的applicationSettings 部分更改设置的API。只有用户范围设置可以改变这种方式。

因此​​,实际上改变这些设置只能通过直接操作的app.config XML文件来完成。

可能会出现一些混乱,因为索引属性从 Properties.Settings.Default 实际上是可写的。以下是完全合法的:

  Properties.Settings.Default [MySetting] =新设定值;
Properties.Settings.Default.Save();
 

但是,设置不会被保存。

I have created some settings in a C# application using .NET 3.5 and Visual Studio 2008 Express. I have a number of application-scoped settings which I would like to be able to modify from within the application - I can access them through Properties.Settings.Default but they are read only as expected. I do not want to have to make these become user-scoped settings as they should be application-wide. Is this possible without loading the XML and reading/writing from/to it myself?

I have seen examples using System.Configuration.ConfigurationManager.OpenExeConfiguration, but the config xml looks to be in a different format to that which I am using (is this from an older version?)

Thanks


Edit

I worked out that I can modify the values doing this, but it seems like a ridiculous hack.

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
SettingElementCollection settingElements = ((ClientSettingsSection)config.GetSectionGroup("applicationSettings").Sections[0]).Settings;

SettingElement element = settingElements.Get("SettingName");
element.Value.ValueXml.InnerText = "new value";

config.Save(ConfigurationSaveMode.Modified, true);

解决方案

OpenExeConfiguration (or any of the other ConfigurationManager methods) is the preferred entry point for most modifications to configuration files. Once you have a Configuration instance you should get the section you wish to modify and after the modifications call any of the ConfigurationManager.Save methods. However, it is impossible to retrieve the applicationSettings section this way.

There is no API for changing settings from the applicationSettings section in your app.config file. Only user-scoped settings can be changed this way.

So actually changing these settings can only be done by directly manipulating the app.config XML file.

Some confusion may occur because the indexed property from Properties.Settings.Default is actually writable. The following is perfectly legal:

Properties.Settings.Default["MySetting"] = "New setting value";
Properties.Settings.Default.Save();

However, the setting will not be saved.

这篇关于我可以修改[APPNAME] .exe.config无需手动读/写XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆