Home Ask Login Register

Developers Planet

Your answer is one click away!

Matheus Cirillo February 2016

Download TextFile from internet and load a ListBox with the content

I'm downloading a txt file from internet but unfortunately, I can't load my listBox with the content from that file.

That's my code:

var webRequest = WebRequest.Create(@"http://photo-51.netau.net/changelog");

        using (var response = webRequest.GetResponse())
        using (var content = response.GetResponseStream())
        using (var reader = new StreamReader(content))
        {
            List<string> lines = new List<string>();
            var strContent = reader.ReadToEnd();
            string line;
            while((line = reader.ReadLine()) != null){

                lines.Add(line);
                listBox1.Items.Add(lines);
            }
        }

When I run it, nothing happens.

Answers


Gusman February 2016

You first read the full stream content into strContent, thus, when you do reader.ReadLine() there's nothing left to read.

If you remove the "var strContent = reader.ReadToEnd();" it will work.

Also a simple way to reduce all the code and make it work is this:

List<string> lines = new List<string>();
reader.AddRange(sr.ReadToEnd().Split("\r\n".ToCharArray()));

Post Status

Asked in February 2016
Viewed 2,587 times
Voted 14
Answered 1 times

Search




Leave an answer


Quote of the day: live life