访问Web配置中的appSettings键

时间:2013-11-28 14:28:19

标签: c# asp.net xml iis

我正在尝试使用标准XML文档读取来访问此web.config中的密钥,但是我似乎无法访问每个密钥。任何人都能够对此有所了解。我不能像往常一样通过configsettings访问它,因为我试图在IIS中查找每个人而不是一个。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <httpRedirect enabled="false" />
</system.webServer>
<appSettings>
    <add key='expires' value='expiry'/>
    <add key='createdby' value='created'/>
    <add key='DateCreated' value='date'/>
</appSettings>
</configuration>

应用程序设置中的键如“expires”就是我所需要的。

我拥有的C#是:

var items = drpVD.Items;
            items.Clear();

            ServerManager mgr = new ServerManager();

            foreach (Site s in mgr.Sites)
            {
                //Console.WriteLine("Site {0}", s.Name);

                foreach (Application app in s.Applications)
                {
                    //Console.WriteLine("\tApplication: {0}", app.Path);

                    foreach (VirtualDirectory virtDir in app.VirtualDirectories)
                    {
                        //Console.WriteLine("\t\tVirtual Dir: {0}", virtDir.Path);

                        String WhichDir = s.Name + " - " + virtDir.Path + " - " + virtDir.PhysicalPath;

                        if (virtDir.Path != "/")
                        {
                            string fileName = virtDir.PhysicalPath + @"\web.config";

                            XmlDocument doc = new XmlDocument();

                            doc.Load(fileName);

                            List<string> categories = new List<string>();

                            XmlNodeList nList = doc.SelectNodes("/configuration/appSettings/add/key");

                            foreach (XmlNode node in nList)
                            {
                                categories.Add(node.Value);
                            }

                            items.Add(WhichDir);

                        }
                    }
                }

先谢谢你,看起来对我来说不错,但我觉得我的SelectNodes错了,我的大脑似乎无法修复它!

1 个答案:

答案 0 :(得分:0)

为什么不使用ConfigurationManager,例如:

var settings = ConfigurationManager.AppSettings;
for (int i = 0; i < settings.Count; i++)
{
    categories.Add(settings[i]);
}

关于SelectNodes,请尝试删除/添加。