遍历设置文件 [英] Iterate through settings files

查看:128
本文介绍了遍历设置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理VSTO项目,该项目有5个.settings文件:

I'm currently working on a VSTO project for which I have 5 .settings files:

  • Settings.settings(默认)
  • s201213.settings
  • s201314.settings
  • s201415.settings
  • s201516.settings
  • Settings.settings (Default)
  • s201213.settings
  • s201314.settings
  • s201415.settings
  • s201516.settings

随着时间的流逝,遵循相同的命名约定('s',然后是纳税年度),将包含更多的设置文件.

Over time there will be more settings files included following the same naming convention ('s' followed by a tax year).

我知道我可以遍历设置文件,但是有没有办法遍历实际的设置文件呢?

I know I can iterate through a settings file, but is there a way to iterate through the actual settings files themselves?

我已经尝试过诸如:

 public void Example()
        {
            System.Collections.IEnumerator testSetting = MyAddIn.Properties.s201213.Default.Properties.GetEnumerator();

            while (testSetting.MoveNext())
            {
                System.Diagnostics.Debug.WriteLine("Setting:\t" + testSetting.Current.ToString());
            } 
        }  

很显然,这仅循环访问一个设置文件,但是我似乎无法弄清楚将所有设置文件作为集合进行循环的逻辑,而无需在代码中显式地命名每个集合.我希望这是有道理的,感谢您的帮助.

Which obviously only iterates through a single settings file, but I can't seem to figure out the logic of iterating through all the settings files as a collection, without explicitly naming each one in the code. I hope that makes sense, any help is appreciated.

更新:
我想我可以使用以下代码:

Update:
I think I'm getting somewhere with the following code:

foreach(Type test in Assembly.GetExecutingAssembly().GetTypes())
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(test.Name, "^s[0-9]{6}$"))
                {
                    PropertyInfo value = test.GetProperty("LEL");

                    try
                    {
                        System.Diagnostics.Debug.WriteLine("Name:\t" + test.Name + 
                                                            "\nNameSpace:\t" + test.Namespace + 
                                                            "\nProperty:\t" + test.GetProperty("LEL").ToString() +
                                                            "\n");
                    }
                    catch(Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine(e.Message);
                    }
                }   
            }  

似乎正在识别设置文件和存储的值:

Which seems to be recognising the settings files and the stored values:

输出:

Name:   s201213
NameSpace:  MyAddIn.Properties
Property:   Double LEL

Name:   s201314
NameSpace:  MyAddIn.Properties
Property:   Double LEL

Name:   s201415
NameSpace:  MyAddIn.Properties
Property:   Double LEL

Name:   s201516
NameSpace:  MyAddIn.Properties
Property:   Double LEL  

但是我似乎无法获得应该返回Double的"LEL"设置的实际吗?

However I can't seem to get the actual value of the "LEL" setting which should return a Double?

第二次更新
我实际上已经放弃了,决定改用本地数据库-但是我仍然想知道这是否可行,而且我想其他人也想知道,所以我将尽全力尝试一下.并产生一些兴趣.

2nd Update
I've actually given up and decided to use a local DB instead - but I would still like to know if this is possible, and I think other people would like to know too so I'm going to throw a bounty at it to try and generate some interest.

推荐答案

看到答案(答案不需要遍历Types或不使用System.IO目录): >

The answer is really simple once you see it (no need to iterate through Types nor use System.IO directory):

using System.Configuration; //Add a reference to this DLL

...

var fileMap = new ConfigurationFileMap(Application.StartupPath + @"\GetSettingFilesValues.exe.config");
var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
var sectionGroup = configuration.GetSectionGroup("applicationSettings"); // This is the section group name, change to your needs, ie application or user
var section = (ClientSettingsSection)sectionGroup.Sections.Get("GetSettingFilesValues.Properties.s201415"); //This is the section name, change to your needs (you know the tax years you need)
var setting = section.Settings.Get("LEL");
System.Diagnostics.Debug.WriteLine(setting.Value.ValueXml.InnerXml);

我同意您使用dB的方法,尽管我确信这也会使其他人受益.

I agree with your approach to use the dB, though I'm sure this will benefit other people too.

这篇关于遍历设置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆