簡體   English   中英

訪問Webconfig中的appSettings鍵

[英]Access appSettings Keys in Webconfig

我正在嘗試使用讀取的標准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>

我需要像“過期”這樣的應用程序設置中的鍵。

我擁有的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上犯了一個錯誤,我的大腦似乎無法解決它!

為什么不使用ConfigurationManager,例如:

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

關於SelectNodes,請嘗試刪除/ add。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM