Introduction
An addition to the Microsoft .NET framework 2.0 to 1.x is the support for FTP. All these days we need to rely on third-party libraries that pretty well satisfies most of our needs, but for sure, there is an extra pleasure with the use of the .Net framework library classes. The code included is not designed to be a full-fledged reusable library, but rather an easy to use and reusable piece of code that is easily comprehensible and can be reused and tweaked to fit your specific needs. Therefore the code for each functionality (upload, download, delete and so on) can be easy picked up separately and reused. The main motive behind this article was the unavailability of .Net 2.0 FTP sample codes and their usage in C#, maybe because it's a new entrant to the .Net scenario, or the third-party implementations available were working pretty well, that this area of the .Net 2.0 library hasn't gotten enough focus.
I started working on this FTP module as part of my official work, but the requirement soon changed and I need to do it for .Net 1.1. So, I haven't travelled deeper into the Rabbit hole. But I believe this gives a good, instant start for using the FTP support in .Net 2.0.
Don't forget to add the following directive:
using System.Net;
using System.IO;
The following steps can be considered as a generic procedure of getting an FTP request executed using a "FtpWebRequest" object:
"FtpWebRequest"
object over an FTP server URIOne
point to watch out while coding for any FTP application is to have the
settings for the FTP request proper to suit the FTP server and its
specific configurations. A "FtpWebRequest"
object exposes many poperties to have these settings in place.
The sample for the upload functionality is as follows.
First
a URI is created that represents the FTP address along with the
filename (directory structure included). This URI is used to create the "FtpWebRequest"
instance.
Then the properties of the "FtpWebRequest"
object are set, that determines the settings for the FTP request.
Some of its important properties are:
private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Until Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav