Duplicate Key in ini FILE VB.net or C#

637 views Asked by At

i'm trying to have duplicate keys in ini file like this:

[section]
key1 = "value1"
key2 = "value2"
key1 = "value....."

Private Declare Ansi Function WritePrivateProfileString _
  Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
  (ByVal lpApplicationName As String, _
  ByVal lpKeyName As String, ByVal lpString As String, _
  ByVal lpFileName As String) As Integer
1

There are 1 answers

0
ANewGuyInTown On

You might want to read your ini file duplicate keys into lookup instance.

Something like below might help:

System.IO.StreamReader YourIniFile = new System.IO.StreamReader("yourIniFilePath");
string fileText = YourIniFile.ReadToEnd();  
string[] splitText = fileText.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
ILookup<string, string> ini = splitText.ToLookup(key=> key.Substring(0, key.IndexOf("=")),
                               value => value.Substring(value.IndexOf("=")));