Upload files with ftp and C#

C# and .Net framework offer FtpWebRequest for ftp transactions.

FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://example.org/test.txt");
 
ftp.Credentials = new NetworkCredential("username", "password");
ftp.Method = WebRequestMethods.Ftp.UploadFile;
ftp.KeepAlive = false;
ftp.UsePassive = true;
ftp.UseBinary = true;
ftp.KeepAlive = false;
ftp.Timeout = timeout;
 
StreamReader sr= new StreamReader("test.txt");
byte [] content= Encoding.UTF8.GetBytes(sr.ReadToEnd());
sr.Close();
 
ftp.ContentLength = content.Length;
 
Stream stream= ftp.GetRequestStream();
stream.Write(content, 0, content.Length);
stream.Close();
 
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
MessageBox.Show(response.StatusDescription);
response.Close();
Categories: C# Tags: , , , ,
%d bloggers like this: