我目前正在开发一个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的方法,但我相信这对其他人也有好处.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。