2015-07-21 96 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. 
}