user6274399
user6274399

Reputation: 65

Downloading a text file from a website ignores new lines

I am trying to download a text file (changelog) from a website and write it in a component in C#.

I tried listbox, listview, textbox but I don't think it matters. The new lines get completely ignored and it displays straight in 1 line all the data.

I tried the following methods:

var web = (HttpWebRequest)WebRequest.Create("http://somesite.com/changelog.txt");
web.Method = "GET";

using (var res = new StreamReader(web.GetResponse().GetResponseStream()))
{
    textBox1.Text = res.ReadToEnd();
}

And

using (WebClient wc = new WebClient())
{
    string changelog = wc.DownloadString("http://somesite.com/changelog.txt");
    textBox1.Text = changelog;
}

Both of the methods returned 1 line of all the data. I need the new lines so I can use it in textBox for example and make it look properly. Right now it's very messy in 1 line and unreadable.

Sample changelog.txt content:

1.0.1.2
- Initial release

1.0.0.3
- Initial release

1.0.0.2
- Initial release
- Initial release
- Initial release

1.0.0.1
- Initial release
- Initial release
- Initial release

1.0.0.0
- Initial release

Fixed it by adding .Replace("\n", "\r\n"); at the end of textBox1.Text = res.ReadToEnd();

textBox1.Text = res.ReadToEnd().Replace("\n", "\r\n");

Upvotes: 0

Views: 293

Answers (1)

user5308950
user5308950

Reputation:

You can do by this way :

var web = (HttpWebRequest)WebRequest.Create("http://somesite.com/changelog.txt");        
web.Method = "GET";    
using (var res = new StreamReader(web.GetResponse().GetResponseStream()))
{
    string line="";
    while ((line=res.ReadLine())!=null)
    {
        textBox1.AppendText(line+System.Environment.NewLine);
    }
}

Upvotes: 1

Related Questions