编程语言
首页 > 编程语言> > c# – 遍历设置文件

c# – 遍历设置文件

作者:互联网

我目前正在开发一个VSTO项目,我有5个.settings文件:

> Settings.settings(默认)
> s201213.settings
> s201314.settings
> s201415.settings
> s201516.settings

随着时间的推移,将有更多的设置文件包含在相同的命名约定之后(‘s’后面跟一个纳税年度).

我知道我可以遍历一个设置文件,但有没有办法迭代实际的设置文件本身?

我尝试过这样的事情:

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

这显然只是遍历单个设置文件,但我似乎无法弄清楚将所有设置文件作为集合进行迭代的逻辑,而无需在代码中明确命名每个文件.我希望这是有道理的,任何帮助都表示赞赏.

更新:
我想我正在使用以下代码:

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

这似乎是识别设置文件和存储的值:

输出:

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”设置的实际值?

第二次更新
我实际上已经放弃并决定使用本地数据库 – 但我仍然想知道这是否可行,而且我认为其他人也想知道,所以我会给它一个赏金来尝试并产生一些兴趣.

解决方法:

一旦你看到答案就很简单了(不需要迭代类型也不需要使用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);

我同意你使用dB的方法,但我相信这对其他人也有好处.

标签:c,settings,application-settings
来源: https://codeday.me/bug/20190708/1399645.html