首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载和查询Web配置

加载和查询Web配置
EN

Stack Overflow用户
提问于 2009-03-09 15:05:02
回答 4查看 642关注 0票数 1

有没有可能在WinForms应用程序中加载一个web.config文件并查询结果配置,而不是使用XML来查找一些非语义的“添加”元素?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-03-10 01:23:33

在尝试了上面的所有建议之后,我使用了XmlDocument和XPath,并使用了两行代码和一个导入的命名空间,而不需要额外的程序集引用。

代码语言:javascript
复制
WebConfigurationManager.OpenWebConfiguration("/", "My Website", null)

上面的代码需要一个虚拟路径,这对WinForms应用程序意味着什么?我确实说过我需要在WinForms应用程序中打开一个web.config。

我的XPath也提高了一点点。:-)

票数 2
EN

Stack Overflow用户

发布于 2009-03-09 15:27:28

你需要添加一个对System.Web的引用,然后go会是这样的:

代码语言:javascript
复制
using System.Web.Configuration
WebConfigurationManager webCfg = WebConfigurationManager.OpenWebconfiguration("/WebApp");

// Edit/Query the configuration
webConfig.AppSettings.Settings.Add("NewSetting","SomeValue");
webConfig.AppSettings.SectionInformation.ForceSave = true;

webConfig.Save();

如果您想访问没有system.diagnostics等公共接口的配置节,那么可以使用泛型配置类。添加一个对System.Configuration的引用,并尝试如下所示:

代码语言:javascript
复制
ConfiguationSection sysDiagnostics = webConfig.GetSection("system.diagnostics");
ConfigurationElementCollection sources = sysDiagnostics.ElementInformation.Properties["sources"].Value as ConfigurationElementCollection;
foreach (ConfigurationElement source in sources)
{
    ConfigurationElementCollection listeners = source.ElementInformation.Properties["listeners"].Value as ConfigurationElementCollection;
     foreach (ConfigurationElement listener in listeners)
     {
        Console.WriteLine(listener.ElementInformation.Properties["name"].Value.ToString());
     }
}
票数 2
EN

Stack Overflow用户

发布于 2009-03-09 15:24:29

您可以在System.Web.Configuration名称空间中使用WebConfigurationManager.OpenWebConfiguration method

示例:

代码语言:javascript
复制
System.Configuration.Configuration config =
        WebConfigurationManager.OpenWebConfiguration("/", "My Website", null) as System.Configuration.Configuration;

KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

这将获取网站‘我的网站’的appsettings块。只需修改第一个参数,即配置文件的虚拟路径。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/626596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档