FTP Uploading with C#
void ftpUpload(String filename, String ftpserver, String username, String password)
{
	try
	{
		FileInfo file = new FileInfo(filename);
		FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpserver));
 
		request.Method = WebRequestMethods.Ftp.UploadFile;
		request.UseBinary = true;
		request.UsePassive = true;
		request.KeepAlive = true;
		request.Credentials = new NetworkCredential(username, password);
		request.ContentLength = file.Length;
		int buffer_length = 16 * 1024;
		byte[] buffer = new byte[file.Length];
		FileStream fstream = file.OpenRead();
		Stream stream = request.GetRequestStream();
		int len = 0;
 
		while ((len = fstream.Read(buffer, 0, buffer_length)) != 0)
			stream.Write(buffer, 0, len);
 
		stream.Close();
		fstream.Close();
 
		FtpWebResponse response = (FtpWebResponse)request.GetResponse();
 
		response.Close();
	}
	catch
	{
	}
}
Categories: C# Tags: , ,
%d bloggers like this: