Search code examples
c#text-filesstreamreaderftpwebrequestftpwebresponse

StreamReader ReadToEnd() gives different results for txt files on different servers


I have created a small FTP program, it's just for my own use, so login details + the file paths are hard coded.

I have a button which starts the downloading process of two txt files - the contents of these are put into two different textboxes.

The txt files are encoded with UTF-8, and look like this:

line1
line2
line3
etc.

I have placed these two files on two different servers (two files on each server). On server 1, both files are downloaded and shown in the textboxes correctly, like this:

line1
line2
line3
etc.

On server 2, both files are downloaded and shown in the textboxes like this:

line1line2line3etc.

I really don't understand why - I have not edited the software (the downloading process) nor the files, I have only edited the hard coded file paths of course, because of the change of server.

This is how I download one of the files (the other file is the same way, just with different names):

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(folder + artistsFileNameTxt);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(login, pass);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

tbxArtists.Text = reader.ReadToEnd();

reader.Close();
response.Close();

Any help?


Solution

  • try

    request.UseBinary = false;
    

    the default is true... do this ONLY when you are SURE that you are dealing with a text file.

    FTP protocol has this "built-in" to deal with system-differences regardings NewLine...

    BTW you must set this setting correctly when uploading via FTP too otherwise it can get messy...

    http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx
    http://www.rhinosoft.com/newsletter/NewsL2008-03-18.asp