Iterate through settings files

妖精的绣舞 提交于 2019-12-10 17:03:52

问题


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

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

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?

I've tried things such as:

 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:

Output:

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  

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.


回答1:


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);

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




回答2:


Jeremy's answer got me to the finish post, but thought I'd post the final code I used so that it can be seen in context:

public void GetLEL()
{
    var fileMap = new ConfigurationFileMap(AppDomain.CurrentDomain.BaseDirectory + @"CustomAddIn.dll.config");
    var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
    var sectionGroup = configuration.GetSectionGroup("userSettings");
    var section = (ClientSettingsSection)sectionGroup.Sections.Get("MyAddIn.s201213");
    var setting = section.Settings.Get("LEL");
    System.Diagnostics.Debug.WriteLine(setting.Value.ValueXml.InnerXml);
    // Prints "107" as expected.
}



回答3:


I believe you can use the System.IO namespace classes for iterating through files with the same extension. There is no built-in mechanisms or properties for that.



来源:https://stackoverflow.com/questions/31540966/iterate-through-settings-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!