Calling FTP process

G

Guest

I want to write a console application which will connect to another machine
and FTP files from that machine to mine.

Does .Net have classes which would allow me to do this natively in the .Net
platform? (Any examples???)

If the .Net does not have any classes which allow me to do this natively,
how do I call the FTP application and send and receive information back (any
examples?)?

Thanks
 
A

Anthony Brown

.Net 2.0 has built in support using FtpWebRequest, here's an example:

// setup a new ftp upload request, specifying deestination path, user id
and password

FtpWebRequest ftpreq = (FtpWebRequest)WebRequest.Create(serverPath +
destFile);
ftpreq.Method = WebRequestMethods.Ftp.UploadFile;
ftpreq.Credentials = new NetworkCredential(FTPuserID, FTPpassword);

// read the file to be uploaded into a byte array

StreamReader sourceStream = new StreamReader(sourcePath + sourceFile);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();

// upload the byte array

ftpreq.ContentLength = fileContents.Length;
Stream requestStream = ftpreq.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

// get the response (note most ftp errors won't trigger the
// catch block below)

FtpWebResponse response = (FtpWebResponse)ftpreq.GetResponse();
Console.WriteLine("upload complete, status {0}",
response.StatusDescription);

response.Close();

depending on how your proxies are setup you may also need to switch off
the default web proxy with WebRequest.DefaultWebProxy = null; before
setting up the ftp request.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top