2015-07-21 67 views
4

我目前工作的一个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”设置的实际,应该返回Double


月2日更新
其实我已经放弃了,决定使用本地数据库,而不是 - 但我还是想知道,如果这是可能的,而且我认为其他人会喜欢我也知道,所以我会抛出赏金来尝试并产生一些兴趣。

回答

0

答案是非常简单的,一旦你看到它(无需通过迭代的类型,也不使用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,但我相信这也会使其他人受益。

+0

谢谢,我会尽快回到我自己的个人电脑上试试这个 - 所以如果我明白了,这是解析XML配置文件的值而不是?从来没有想过...... _(tag edi )_ –

+0

请原谅我的无知,因为我仍然在学习C#和VS的基础知识 - 'GetSettingFilesValues.exe.config'不存在于'XLSTART'或'Assembly.GetExecutingAssembly.BaseDirectory '所以我现在硬编码了我的'app.config'文件的路径。它是否正确?我在'section.Settings.Get(“LEL”)''上声明“未将对象引用设置为对象实例”时出现异常。 –

+0

是的,这对于VSTO是正确的,AddIns配置文件是你想要的。在我的例子中,我使用了一个winform应用程序。对于Web开发人员,他们会使用他们的web.config的路径。 –

0

我相信你可以使用System.IO命名空间类遍历具有相同扩展名的文件。没有内置的机制或属性。

+0

我用'系统有大约一出戏。 IO'命名空间,但仍然看不到任何东西来获取项目文件的枚举器。我开始认为我可能需要编制一些解决方法:( –

+0

'System.IO'命名空间没有提供任何特别的* .settings文件,但它允许查找所有具有指定扩展名的文件并穿过他们每个人 –

+0

好吧,我认为我遵循你的逻辑 - 让我有更多的发挥。干杯! –

1

杰里米的回答让我到终点后,但想到我会发布我用这样的最终代码,它可以在上下文中可以看出:

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