在 ASP.NET 应用程序运行时将项目添加到 Properties.Settings
我想在运行时将一个项目添加到 ASP.Net Web 应用程序的全局设置中。似乎 Properties.Settings.Default.Properties
对象是只读的,或者至少它的属性是只读的,所以我试图直接写入 web.config
文件。这工作正常,但我发现的东西只是将我的信息放入 AppSettings 部分,当我需要它位于 ApplicationName.Properties.Settings
中时,以便通过 Default.Properties 对象提供它。
我的代码基本上是这样的:
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
config.AppSettings.Add(mySettingName, myValue);
config.Save();
这很好,很花哨,除了 - 显然足够 - 我的设置出现在文件的
部分。
有没有办法使用 Configuration 对象来访问配置文件的 ApplicationSettings/ApplicationName.Properties.Settings 部分?如果是这样怎么办?如果没有,是否有另一种方法可以从代码将值写入我的应用程序设置?
I want to add an item at runtime to my global settings on an ASP.Net web application. It seems that the Properties.Settings.Default.Properties
object is read-only, or at least it's Attributes are so I was trying to write directly into the web.config
file. This works correctly but the stuff I found was just dropping my info into the AppSettings section, when I need it to be in the ApplicationName.Properties.Settings
so it is made available through the Default.Properties object.
The code I have basically goes like this:
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
config.AppSettings.Add(mySettingName, myValue);
config.Save();
Which is fine and dandy, except that - obviously enough - my setting turns up in the <appSettings>
section of the file.
Is there a way I can use the Configuration object to access the ApplicationSettings/ApplicationName.Properties.Settings part of the configuration file? If so how? If not is there another way to write values into my application settings from code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ConfigurationManager.GetSection()
方法从配置文件中检索任何部分。但框架内不支持更新。请在此处了解更多信息。
You can use the
ConfigurationManager.GetSection()
method to retrieve any section from a configuration file. However, there is no support for updating within the framework.Read more here.