0

I have a checkedListBox and in collection items I want to add what I have in a text file installer.ini after #product=name of checkbox. How I can do this ? Something like this :

var lines = System.IO.File.ReadAllLines(path + "installer.ini");
   var items = new List<string>();
        lines.Where(x => x.StartsWith("#product="))
            .Select(x =>x.Replace("#product=", "").Trim())
           .ToList()
           .ForEach(item =>
           {
               string line;
               items.Add(line);
           }                  

           );
       checkedListBox2.AddRange( items );

        }
ben
  • 135
  • 8
  • Didn't really understand what you need... It seems your code is doing exactly what you described. Only remove "ForEach" block and set ListBox2.AddRange(lines) –  Sep 28 '15 at 11:39
  • possible duplicate of [Reading/writing an INI file](http://stackoverflow.com/questions/217902/reading-writing-an-ini-file) – Sinatr Sep 28 '15 at 11:42

1 Answers1

1

You almost did everything right:

var items = System.IO.File.
    ReadAllLines(path + "installer.ini").
    Where(x => x.StartsWith("#product=")).
    Select(x =>x.Replace("#product=", "").Trim()).
    ToArray();

ListBox2.Items.AddRange(items);
  • I put this code in public Form2 but don't recognize the ListBox-ul, way? – ben Sep 28 '15 at 11:50
  • Because you dont have ListBox2 on your Form2? –  Sep 28 '15 at 11:52
  • oh I change the name I have checkedListBox2, but now don't recognize the "AddRange " I have an error : "doesn't containt a definition – ben Sep 28 '15 at 11:54