Question

I have a small HttpWebRequest that grabs some text from a online .txt file

After it gets it i want to save it to a .txt file on the computer.

Content of the text is formatet like this:

Line one
Line two
Line four
Line Five
Line ten etc.

But when it saves it ends up like this:

Line oneLine twoLine fourLine FiveLine ten etc.

How may I fix this?

Code is as follows:

HttpWebRequest WebReq3 = (HttpWebRequest)WebRequest.Create("http://test.net/test.txt");

HttpWebResponse WebResp3 = (HttpWebResponse)WebReq3.GetResponse();

System.IO.StreamReader sr3 = new System.IO.StreamReader(WebResp3.GetResponseStream());


System.IO.StreamWriter _WriteResult = new StreamWriter(Application.StartupPath + "\Test.txt");
_WriteResult.Write(sr3.ReadToEnd());
_WriteResult.Close();

sr3.Close();
Was it helpful?

Solution

Read data using ReadLine() and write using WriteLine() instead of ReadToEnd() and WriteToEnd().

Remove this line:

_WriteResult.Write(sr3.ReadToEnd());

And modify your code with this:

string readval = sr3.ReadLine();
while(readval != null)
{
    _WriteResult.WriteLine(readval);
    readval = sr3.ReadLine();
}

For more details, see the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top