【问题标题】:Best way to store associative array in app.config在 app.config 中存储关联数组的最佳方式
【发布时间】:2012-07-27 05:04:02
【问题描述】:

我最近一直在研究我的软件,我一直想知道存储关联数组的最佳方法是什么。

我唯一能想到的就是做这样的事情:

<add key="disks" value="C|1|10,D|2|20,E|1|5,Z|1|3"/>

但这并没有在我的配置文件中提供很多可读性,我希望我的配置文件是可读的,因为它是一个控制台应用程序。

之所以会这样,是因为我编写了一个程序来检查 app.config 文件中指定的磁盘的磁盘空间,但我想要不同磁盘的不同阈值。

你会怎么解决? 这是我当前配置文件的一部分。

<!-- DISK FEATURE SETTINGS -->
  <!-- Type 1 is threshold by percentage and type 2 is threshold by a certain limit -->
  <add key="threshold_type" value="1" />
  <add key="threshold_limit" value="0,1" />
  <!-- Space_type defines if you want to limit using kilobytes (1), megabytes (2) or gigabytes (3) if using threshold_type 2 -->
  <add key="space_type" value="3" />
  <!-- Put the disks here delimited by a comma like this: C,D,E -->
  <add key="disks" value="C,D,E,Z"/>
<!-- SERVICE FEATURE SETTINGS -->
  <!-- Put the services here delimited by a comma like this: C,D,E -->
  <add key="services" value="spooler,ekrn,RadeonPro Support Service,TeamViewer6"/>
  <!-- Put this on 1 if you want to log your output to a text file -->
  <add key="logging" value="1"/>

我想对我的 performancecounter 程序使用相同的原理,该程序使用 perfmon 计数器获取一些数据并将其存储在文本文件中。

我希望人们可以在这里帮助我一点:)

【问题讨论】:

    标签: c# configuration app-config system.configuration


    【解决方案1】:

    我建议您创建自己的配置部分。自定义配置提供了更高的可读性和类型安全性。以下是创建自定义配置的链接 http://msdn.microsoft.com/en-us/library/2tw134k3.aspxhttp://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx(旧的但易于理解)。

    【讨论】:

    • 我同意了这个解决方案,尽管我需要更多的搜索才能得到我想要的。谢谢你的建议:)
    【解决方案2】:

    就标准配置机制与 XML 序列化一起使用而言,在 App.config 中存储字典的最佳(恕我直言,明智的)方法是 List&lt;KeyValuePair&lt;K,V&gt;&gt;

    【讨论】:

      【解决方案3】:

      您可能想要使用 system.collection 或 List 中的 Hashtable

      下面是一些哈希表的指针, http://www.dotnetperls.com/hashtable http://www.tutorialspoint.com/csharp/csharp_hashtable.htm

      我希望这会有所帮助!谢谢:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-10
        • 1970-01-01
        • 2016-07-19
        • 2020-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多