Read File From FTP Using C#
// May 18th, 2010 // Useful Code
Here is a small snippet on reading a text file from an FTP server. This could be replaced with a binary file or any other type; I just used a text file to keep it simple.
Code
//CREATE AN FTP REQUEST WITH THE DOMAIN AND CREDENTIALS
System.Net.FtpWebRequest tmpReq = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create("ftp://domain.com/file.txt");
tmpReq.Credentials = new System.Net.NetworkCredential("userName", "Password");
//GET THE FTP RESPONSE
using (System.Net.WebResponse tmpRes = tmpReq.GetResponse())
{
//GET THE STREAM TO READ THE RESPONSE FROM
using (System.IO.Stream tmpStream = tmpRes.GetResponseStream())
{
//CREATE A TXT READER (COULD BE BINARY OR ANY OTHER TYPE YOU NEED)
using (System.IO.TextReader tmpReader = new System.IO.StreamReader(tmpStream))
{
//STORE THE FILE CONTENTS INTO A STRING
string fileContents = tmpReader.ReadToEnd();
//DO SOMETHING WITH SAID FILE CONTENTS
}
}
}
2 Responses to “Read File From FTP Using C#”
Leave a Reply
You must be logged in to post a comment.
Michael E. Chancey Jr. Software Engineer Extraordinaire
[...] artykuł na: Read File From FTP Using C# | Michael E. Chancey Jr. a-small-snippet, a-text-file, binary-file, ftp, keep-it-simple, replaced-with, simple, [...]
Remote text viewers like this one are really useful. I’m currently working on a remote viewer for a few other file types, including Excel.