遍历设置文件
Iterate through settings files
我目前正在开发一个 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
但是我似乎无法获得 "LEL" 设置的实际 值 应该 return 和 Double
?
第二次更新
我实际上已经放弃并决定改用本地数据库 - 但我仍然想知道这是否可行,而且我认为其他人也想知道所以我打算悬赏一下试试并产生一些兴趣。
我相信您可以使用 System.IO 命名空间 类 来遍历具有相同扩展名的文件。没有内置的机制或属性。
一旦您看到答案就非常简单(无需遍历类型或使用 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 的方法,但我相信这也会使其他人受益。
Jeremy 的回答让我完成了 post,但我认为我会 post 我使用的最终代码,以便可以在上下文中看到它:
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.
}
我目前正在开发一个 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
但是我似乎无法获得 "LEL" 设置的实际 值 应该 return 和 Double
?
第二次更新
我实际上已经放弃并决定改用本地数据库 - 但我仍然想知道这是否可行,而且我认为其他人也想知道所以我打算悬赏一下试试并产生一些兴趣。
我相信您可以使用 System.IO 命名空间 类 来遍历具有相同扩展名的文件。没有内置的机制或属性。
一旦您看到答案就非常简单(无需遍历类型或使用 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 的方法,但我相信这也会使其他人受益。
Jeremy 的回答让我完成了 post,但我认为我会 post 我使用的最终代码,以便可以在上下文中看到它:
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.
}