c# – 遍历设置文件
发布时间:2020-12-15 22:17:10 所属栏目:百科 来源:网络整理
导读:我目前正在开发一个VSTO项目,我有5个.settings文件: Settings.settings(默认) s201213.settings s201314.settings s201415.settings s201516.settings 随着时间的推移,将有更多的设置文件包含在相同的命名约定之后(‘s’后面跟一个纳税年度). 我知道我可以
我目前正在开发一个VSTO项目,我有5个.settings文件:
> Settings.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的方法,但我相信这对其他人也有好处. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |