2017-08-09 9 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私は現在、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 

ファイルの例ですファイルを作成し、それを私のlistBoxに表示したいのですが、ここでは[Place]セクションの名前と値をPlaceというリストに保存しますs Positionsの名前と値をリストし、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

大変感謝します。ジョンクラーク – Oxlade

+0

素晴らしい!あなたはこれを答えとしてマークして、他の人が将来容易に見つけることができます。 –

+0

申し訳ありませんジョン・クラーク、私は持っていると思いました。それは今マークされています。ありがとう – Oxlade

関連する問題