0

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?

gosr
  • 4,593
  • 9
  • 46
  • 82
  • 1
    Are there any differences between the servers? What do you see if you download the file from both servers? Can you verify that both downloaded versions have same line endings (probably CR LF)? – svick Aug 06 '11 at 22:34
  • @svick: First server is Windows, not sure about the other one. Both downloaded versions have the same line endings: CR LF. – gosr Aug 06 '11 at 22:54

3 Answers3

2

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

Yahia
  • 69,653
  • 9
  • 115
  • 144
1

My guess is that the first server is windows, whereas the second one is linux. Windows new line: \r\n Linux new line: \n is not displayed correctly in windows

Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
0

try

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, Encoding.UTF8);

tbxArtists.Text = reader.ReadToEnd();

reader.Close();
response.Close();
Peter
  • 37,042
  • 39
  • 142
  • 198