簡體   English   中英

使用庫中的自定義方法從 C# 中的 App.config 獲取所有對(鍵、值)

[英]Using custom method from library to get all pairs (key, value) from App.config in c#

我正在嘗試構建自己的最常用方法/代碼庫。 我目前正在使用的方法假設從用戶提供的部分從配置文件中讀取所有對(值、鍵)並將它們記錄到記錄器中。 當我將代碼轉換為方法並將其移動到單獨的文件時,我發現了一個與App.config位置相關的問題。

所以我有我的App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="General" type="System.Configuration.NameValueSectionHandler"/>
    <section name="Generalxxxxxxxxxxxxx" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>
  <General>
    <add key="ApplicationName" value="Configuration Example Project"/>
    <add key="Language" value="CSharp"/>
    <add key="SecretKey" value="012345"/>
  </General>
  <Generalxxxxxxxxxxxxx>
    <add key="ApplicationName" value="Configuration Example Project"/>
    <add key="Language" value="CSharp"/>
    <add key="SecretKey" value="012345"/>
  </Generalxxxxxxxxxxxxx>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>

只需使用以下代碼,我就可以從中讀取所有對(鍵、值)。 在這種情況下,當所有內容都在一個文件下時,我不必提供App.config位置。

var general = ConfigurationManager.GetSection("General") as NameValueCollection;  
            if (applicationSettings.Count == 0)  
            {  
                Console.WriteLine("Application Settings are not defined");  
            }  
            else  
            {  
                foreach (var key in general.AllKeys)  
                {  
                    Console.WriteLine(key + " = " + applicationSettings[key]);  
                }  
            }  

我找到了一種傳遞位置的方法,但現在由於轉換錯誤,我無法將sectionLoad讀取為NameValueCollection ,因此我無法使用AllKeys方法。

 public static Tuple<string, string> LogSaveConfig(string location, string sectionName)
        {

            //var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var config = ConfigurationManager.OpenExeConfiguration(location);

            var sectionLoad = config.GetSection(sectionName);

            return Tuple.Create("key", "value"); //just temporary
        }

我還對“sectionLoad”進行了一些調試,只有我能找到對(鍵,值)的地方是 rawXML: 在此處輸入圖片說明

我的問題是,當我通過作為單獨庫的一部分的方法調用配置文件時,如何獲得對(鍵、值)? 我的目標是能夠輸入MyLib.LoggerDefined.LogSaveConfig(location, section); 它將把對(鍵,值)記錄到記錄器中。 另外,如果我的方法有誤,請告訴我。 謝謝。

您可以直接讀取xml格式的xml文件。 我使用了 xml linq 並創建了一個字典

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication158
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, List<List<KeyValuePair<string, string>>>> dict = doc.Root.Elements()
                .GroupBy(x => x.Name.LocalName, y => y.Elements()
                    .Select(z => z.Attributes().Select(a => new KeyValuePair<string, string>(a.Name.LocalName, (string)a)).ToList()).ToList())
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
}

暫無
暫無

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

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