2017-08-09 49 views
1

我在這裏搜索,沒有找到解決我的問題的答案,我有一個程序讀取報告(txt文件)並自動填充工作簿中的工作表的特定單元格。我創建了一個ini文件,用戶將在需要時進行更新。 我遇到的問題是我想讀取ini文件並將某些部分的內容保存到他們自己的列表中。這裏是我的代碼:如何讀取和保存ini文件到列表

public class IniFile 
{ 

    [DllImport("kernel32")] 
    static extern int GetPrivateProfileString(string Section, string Key, 
      string Value, StringBuilder Result, int Size, string FileName); 


    [DllImport("kernel32")] 
    static extern int GetPrivateProfileString(string Section, int Key, 
      string Value, [MarshalAs(UnmanagedType.LPArray)] byte[] Result, 
      int Size, string FileName); 


    [DllImport("kernel32")] 
    static extern int GetPrivateProfileString(int Section, string Key, 
      string Value, [MarshalAs(UnmanagedType.LPArray)] byte[] Result, 
      int Size, string FileName); 

    public string path; 
    public IniFile(string INIPath) 
    { 
     path = INIPath; 
    } 

    public string[] GetSectionNames() 
    { 
     for (int maxsize = 500; true; maxsize *= 2) 
     { 
      byte[] bytes = new byte[maxsize]; 
      int size = GetPrivateProfileString(0, "", "", bytes, maxsize, path); 

      if (size < maxsize - 2) 
      { 
       string Selected = Encoding.ASCII.GetString(bytes, 0, 
           size - (size > 0 ? 1 : 0)); 
       return Selected.Split(new char[] { '\0' }); 
      } 
     } 
    } 

    public string[] GetEntryKeyNames(string section) 
    { 
     for (int maxsize = 500; true; maxsize *= 2) 
     { 
      byte[] bytes = new byte[maxsize]; 
      int  size  = GetPrivateProfileString(section, 0, "", bytes, maxsize, path); 

      if (size < maxsize - 2) 
      { 
       string entries = Encoding.ASCII.GetString(bytes, 0, 
           size - (size > 0 ? 1 : 0)); 
       return entries.Split(new char[] { '\0' }); 
      } 
     } 
    } 

    public object GetEntryKeyValue(string section, string entry) 
    { 
     for (int maxsize = 250; true; maxsize *= 2) 
     { 
      StringBuilder result = new StringBuilder(maxsize); 
      int   size  = GetPrivateProfileString(section, entry, "", 
           result, maxsize, path); 
      if (size < maxsize - 1) 
      { 
       return result.ToString(); 
      } 
     } 
    } 
} 

}

這是我使用的代碼:

List<string> PlacesList= new List<string>(); 
    List<string> PositionsList= new List<string>(); 

    private void btnReadini_Click(object sender, EventArgs e) 
    { 
     IniFile INI = new IniFile(@"C:\Races.ini"); 
     try 
     { 
      string[] SectionHeader = INI.GetSectionNames(); 
      if (SectionHeader != null) 
      { 
       foreach (string SecHead in SectionHeader) 
       { 
        listBox1.Items.Add(""); 
        listBox1.Items.Add("[" +SecHead+"]"); 

        string[] Entry = INI.GetEntryKeyNames(SecHead); 
        if (Entry != null) 
        { 
         foreach (string EntName in Entry) 
         { 
          listBox1.Items.Add(EntName +"=" + 
             INI.GetEntryKeyValue(SecHead, EntName)); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      listBox1.Items.Add("Error: " + ex); 
     } 
    } 

下面是一個例子INI文件

[Places] 
IOM=Isle of man 
UK=United Kingdom 
IRE=Ireland 
[Races] 
IOM=7 
UK=6 
[Positions] 
WN=Win 
2nd=Second 
3rd=Third 
4th=Fourth 

我目前可以讀取INI文件並將其顯示在我的列表框中,我現在想要做的是將[位置]部分的名稱和值保存到名爲Place的列表中sList並將位置的名稱和值保存到名爲PositionsList的列表中。使用當前課程,我可以讀取所有部分,鍵和值,但是如何僅將我想要的數據放入列表中?

回答

1

你已經接近上面的代碼,而不是循環遍歷所有的部分,你可以請求你需要的部分(如果需要可以應用相同的條目)。

List<string> PlacesList= new List<string>(); 
List<string> PositionsList= new List<string>(); 

public void btnReadini_Click(object sender, EventArgs e) 
{ 
    PlacesList = ListEntries("Places"); 
    PositionsList = ListEntries("Positions"); 
} 

public List<string> ListEntries(string sectionName) 
{ 
    IniFile INI = new IniFile(@"C:\Races.ini"); 
    List<string> entries = null; 

    string[] Entry = INI.GetEntryKeyNames(sectionName); 
    if (Entry != null) 
    { 
     entries = new List<string>(); 

     foreach (string EntName in Entry) 
     { 
      entries.Add(EntName + "=" + INI.GetEntryKeyValue(sectionName, EntName)); 
     } 
    } 

    return entries; 
} 

然而,而不是存儲在一個列表,你應該看看使用Dictionary的數據,那麼你可以使用鍵來查找值。

public Dictionary<string, string> ListEntries(string sectionName) 
{ 
    IniFile INI = new IniFile(@"C:\Races.ini"); 

    string[] Entry = INI.GetEntryKeyNames(sectionName); 
    var entries = Entry .Where(x => !string.IsNullOrEmpty(x)) 
         .ToDictionary(m => m, 
             m => INI.GetEntryKeyValue(sectionName, m)); 

    return entries; 
} 
+0

太棒了,謝謝Jon Clarke – Oxlade

+0

太棒了!請您可以將此標記爲答案,以便其他人可以在將來輕鬆找到答案... –

+0

對不起,喬恩克拉克,我以爲我有。它現在被標記。謝謝 – Oxlade