Sir/Ma'am
I have a .txt file int the following format:
[test1]
blue
red
green
[test2]
one
two
three
[test3]
white
black
grey
I'm trying to read the everything below [test2] and above [test3] into a combobox. So, I'm trying to populate comboBox1 with one, two, and three. I thought this might work, but I was wrong:
{
comboBox1.Items.Clear();
using (StreamReader sr = new StreamReader(@"c:\test.cfg"))
{
String line1;
do
{
line1 = sr.ReadLine();
if (line1.StartsWith("[test2"))
{
line1 = line1.Split(']')[1].Trim();
String[] strgArray;
strgArray = line1.Split('\n');
foreach (String strg in strgArray)
{
comboBox1.Items.Add(strg.Trim());
}
break;
}
}
while ((line1 != null));
Console.Read();
}
}
}
THANK YOU!!!
Loading
Jaish MathewsPosted Mar 28, 2010, 1:47 PM
Which set needs to populate with combo?
matthew kingPosted Mar 27, 2010, 11:36 AM
You guys nailed it! You came up with exactly what I asked for. However, I did a poor job of trying to articulate what I was try to accomplish. I'm trying to make the .cfg file dynamic, so the the order in which the [test1], [test2], and [test3] won't matter. From what I asked, test2 would have to be between [test1] and [test3] for combox2 to be populated with the data. However, what happens if the user moves [test2] up one position. Thus, [test1] would be populated in the combobox. Again, I appreciate the help!
Jaish MathewsPosted Mar 27, 2010, 2:47 AM
using (StreamReader str = new StreamReader(@"c:\test.cfg"))
{
String line1;
while (!str.EndOfStream)
{
line1 = str.ReadLine();
if (line1.StartsWith("[test2"))
{
line1 = str.ReadLine();
while (!line1.StartsWith("[test3"))
{
//ADD ITEMS HERE//////////////
line1 = str.ReadLine();
}
}
}
}
Amit ChoudharyPosted Mar 27, 2010, 2:38 AM
i have worked on your code.. it is reading form the file and binding to combobox is working well:
{{{{if (line1.Contains("[")){But there is a problem i found that your string "[test1]" is not readable by StreamReader. it just getting skipped while reading from file and so
the condition
never gets executed.Hope it helps. Please mark as answer if you get some help.