FTP.NET is a file-transfer component for .NET languages (such as c# or VB.NET). Add ability to FTP files directly from your application - either in synchronous or asynchronous mode. All popular ftp and proxy servers are supported.
This sample is a simple GUI FTP client written in C#. It demonstrates the following features:
GetPut sample (both C# and VB.NET) is a command line utility for uploading and downloading files. Rebex FTP.NET makes this possible with just a few lines of code:
// create Ftp object, connect and log in
Ftp ftp = new Ftp();
ftp.Connect (hostname);
ftp.Login (username, password);
// set transfer type to binary
ftp.SetTransferType (FtpTransferType.Binary);
// download file and display number of bytes transferred
long bytes = ftp.GetFile (remotePath, localPath);
Console.WriteLine ("Transfered {0} bytes.", bytes);
// disconnect
ftp.Disconnect();
FtpList sample (both C# and VB.NET) demonstrates the power of GetList method and FtpList class. It is a command line utility that connects to a given FTP server and displays file list for a given directory.
The format of the file list (FTP LIST command) differs from server to server. But with Rebex FTP.NET, retrieving and using list of files in directory is extremely easy - GetList method does all the hard work and parses all common list formats automatically! Following code snippet displays the list of files in the remote directory:
ftp.ChangeDirectory (path);
FtpList list = ftp.GetList ();
foreach (FtpItem item in list)
{
Console.Write (item.Modified.ToString("u"));
Console.Write (item.Size.ToString().PadLeft(10,' '));
Console.Write (" " + item.Name);
Console.WriteLine ();
}
It is that simple!
The .NET Framework introducet Uri, WebRequest and WebResponse classes for accessing internet resources through a request/response model and includes support for HTTP protocol and file:// scheme to request local files.
Rebex FTP.NET fits nicely into this model with FtpWebRequest class, which provides an FTP-specific implementation of WebRequest class. Once registered, WebRequest.Create can be used for accessing files on FTP servers.
// register the component for the FTP prefix
WebRequest.RegisterPrefix ("ftp://", FtpWebRequest.Creator);
// WebRequest now supports ftp protocol in addition to http and local files
// the rest of this code snippet is protocol-agnostic
// create web request for the given URI
WebRequest request = WebRequest.Create (uri);
// get and read web response
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Stream local = File.Create (localPath);
byte[] buffer = new byte[1024];
int n;
do
{
n = stream.Read (buffer, 0, buffer.Length);
local.Write (buffer, 0, n);
} while (n > 0);
// close both streams
stream.Close();
local.Close();
GetPutAsync sample (both C# and VB.NET) is identical to GetPut sample, but it does the transfers asynchronously in the background. All methods of the Ftp class have the asychronous versions that behave according to .NET design pattern.
IAsyncResult asyncResult = ftp.BeginGetFile
(
remotePath,
localPath,
new AsyncCallback(MyCallback),
null
);
while (!asyncResult.IsCompleted)
{
// do something else here...
}
// finish asynchronous operation and get the result
long bytes = ftp.EndGetFile (asyncResult);
The MyCallback method is also called when the transfer is finished.
A simple console FTP client with Un*x rights and chmod support written in C#. It demonstrates the following features: