Recently I had to write a console application that periodically downloaded a file from an FTP server and parsed into items. Those items went into a database.
Now the parsing and inserting them into a database is quite standard operation.
The getting the file from an FTP was new for me.
Here is how I solved it:
public bool RetrieveFile(string relativeFilePath, string targetLocalFilePath, string server, string username, string password)
bool result = true;
string fileContents = null;
string FTPuri = server + "/" + relativeFilePath;
WebRequest ftpReq = FtpWebRequest.Create(FTPuri);
ftpReq.Credentials = new NetworkCredential(username, password);
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream stream = ftpReq.GetResponse().GetResponseStream())
using (StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("windows-1252")))
fileContents = sr.ReadToEnd();
result = false;
What is particularly important is the specifying the encoding. This should be the encoding of the file that is on the FTP, which is not necessarily the encoding you presume it is in.