I want to populate the arraylist with the contents of the file. now it reads the first line and adds it to 0 and reads the next line and again writes it to 0, so basically it keeps overwriting itself. how can i make it add to the next avail element?

        ArrayList files = new ArrayList();
        StreamReader sr = new StreamReader("C:\\TestFile.txt");
        String line;
        while ((line = sr.ReadLine()) != null)
        {

            files.Add(sr.ReadLine());
          for (int i=0; i<files.Count; i++)
            {
                files.Add(sr.ReadLine());
            }

        }
        sr.Close();
ArrayList files = new ArrayList();
StreamReader sr = new StreamReader("C:\\TestFile.txt");
String line;
while ((line = sr.ReadLine()) != null)
{
files.Add(line);
}
sr.Close();

you don't need that for loop in there, and a call to readline makes the stream read again, you are using line as sr.ReadLine() so you needed to add it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.