有没有一种简单的方法可以通过使用它的路径来读取web.config文件的appsettings?

时间:2011-10-22 15:31:42

标签: .net web-config configurationmanager

在我看来这很简单,但我似乎无法找到正确的谷歌查询来帮助我..

我有位于C:\ temp的 web.config文件,希望能够使用myWebConfig.AppSettings["myParam"]之类的东西。 可以这样做吗?

我尝试使用ConfigurationManager.OpenMappedMachineConfigurationConfigurationManager.OpenExeConfigurationWebConfigurationManager.OpenWebConfigurationWebConfigurationManager.OpenMappedWebConfiguration

也许我只是没有以正确的方式使用它们或者??

[更新]
tvanfossen写的是正确的。我没有编辑权限,所以我将编写我最后使用的代码。

/* Method based on what I found on http://stackoverflow.com/questions/4339167/how-to-read-a-configuration-section-from-xml-in-a-database/4844365#4844365
 * and tvanvossens answer.
 * There's probably room for improvement, but it does what I need now.
 */
private static T GetSection<T>(string pathToWebConfigFile, string configNode) where T : ConfigurationSection, new()
{
    var doc = XDocument.Load(pathToWebConfigFile);
    var element = doc.Element("configuration").Element(configNode);
    var reader = element.CreateReader();
    var settingsSection = new T();
    settingsSection.GetType().GetMethod("DeserializeSection", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(settingsSection, new object[] { reader });
    return settingsSection;
}

用法示例:

var settingsSection = GetSection<AppSettingsSection>(pathToWebConfigFile, "appSettings");
var connectionStringSection = GetSection<ConnectionStringsSection>(pathToWebConfigFile, "connectionStrings");

4 个答案:

答案 0 :(得分:1)

取决于你所说的“简单”。我认为可以使用反射来完成,但它不会特别灵活。

首先,您将文档打开为XML并找到appSettings部分。然后,您将为该部分创建一个XmlReader。然后,创建一个AppSettingsSection并使用以reader为参数的反射调用DeserializeSection()方法。您可以使用Reflector来查看ConfigurationManager如何执行此操作,因为AppSettingsSection上可能还有其他方法需要调用以完成该部分的加载。

完全未经测试的例子......

 XDocument doc = XDocument.Load(@"c:\temp\web.config");
 var appSettingsElement = doc.Elements("appSettings").First();
 var reader = appSettingsElement.CreateReader();
 var settings = new AppSettingsSection();
 var method = typeof(AppSettingsSection).GetMethod("DeserializeSection",
                                                   BindingFlags.NonPublic,
                                                   null,
                                                   null,
                                                   new[] { typeof(XmlReader) },
                                                   null );
 method.Invoke( settings, new [] { reader } );

 var value = settings["myParam"];

答案 1 :(得分:0)

假设您的变量是“YourVariable”

下面的代码行,返回文件路径。

HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["YourVariable"])

要仅访问 web.config 的值,请以此方式编写。

System.Configuration.ConfigurationManager.AppSettings["YourVariable"]

在您的项目中,您应该将 System.configuration 插入参考

答案 2 :(得分:0)

底线,是---否.appSettings配置部分是一个xml元素,其内容在整个主机应用程序 Hiearchical Configuration Structure 中,可以包含 许多 个别文件。 appSettings部分所在的文件(或文件*)由配置系统中的其他设置决定。所以你不能仅仅使用文件pr路径规范来实现设置,你必须通过内置的.Net配置系统方法。

  • 基于配置系统中的其他设置,一个部分可以放在另一个文件中,或者一个部分内的单个设置可以位于不同的文件中,位于配置系统的不同层次上。)

答案 3 :(得分:0)

我发现您已经尝试WebConfigurationManager.OpenWebConfiguration(...) - 您错过了config.AppSettings吗?

// Resolved from the application root with "~"
// "~/MySubfolder/With/SpecialConfig/" works; values are read hierarchically
var relativePath = "~/";

var config = WebConfigurationManager.OpenWebConfiguration(relativePath);

var myParam = config.AppSettings.Settings["myParam"].Value;

// You can use it to save settings too
config.AppSettings.Settings["myParam"].Value = "my new param value";

config.Save(ConfigurationSaveMode.Modified);