Iterate through settings files

I am currently working on a VSTO project for which I have 5 .settingsfiles:

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

Over time, more settings files will be added after the same naming convention ('s' followed by the tax year).

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

I tried things like:

 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 one settings file, but I can’t understand the logic of repeating all settings files as a collection without explicitly naming each of them in the code. Hope this makes sense, any help is appreciated.


Update:
I think I'm getting code 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);
                    }
                }   
            }  

It seems to recognize the settings files and 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  

, , "LEL", Double?



, , , , , , .

+4
3

, ( System.IO):

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

, , .

0

, , , , :

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.
}
+1

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

0
source

Source: https://habr.com/ru/post/1598895/


All Articles