Du er ikke logget ind. Vil du oprettes som bruger?
ThinkSharp FTP Client Component for .NET
manual

Beskrivelse

Produktet består af et enkelt assembly indeholdende 7 klasser. Hovedklassen hedder FtpClient hvorigennem kommunikationen med FTP serveren foregår.
Arbejdet med mapper og filer foregår gennem klasserne FtpFileInfo og FtpDirectoryInfo. Disse arver begge fra den abstrakte klasse FtpInfo der indeholder metoder og properties der er delt mellem fil -og mappeklassen.

Til produktet medfølger der html dokumentation der beskriver samtlige klasse, metoder og properties.

Brug

Nedenfor er der givet en kort gennemgang af de mest benyttede funktioner, ud fra et kodeeksempel skrevet i C#. Komponenten kan dog benyttes fra et vilkårligt .NET sprog.


using System;
using System.Diagnostics;
using ThinkSharp.Components.Net.Ftp;

namespace ThinkSharp.Components.Net.Ftp {
///
/// Demo and explanation of the Thinksharp Ftp Client
/// Component. This is a fictive example with hardcoded
/// servernames, directories and so on. The only purpose
/// of this class is to SHOW some of the common features
/// of the ThinkSharp.Components.Net.Ftp assembly
///

public class FtpDemo {
FtpClient _ftp;

public FtpDemo() {

try {

/** Establish a connection to a sever and setup
the connect mode and transfertype **/

//Create an instance of the object with the
//hostname as the only mandatory paramater
_ftp = new FtpClient("sharpdev.dk");

//Establish a connection to the FTP server
_ftp.Connect("guest","guest123");

//Setup the connect mode
_ftp.ConnectMode = ConnectModes.PASV;
//Setup the transfer type
_ftp.TransferType = TransferTypes.BINARY;


/** Setup the current working directory on the server **/
_ftp.WorkingDirectory = "/test";


//** List a directory on the server **//

//The following code runs through all directories
//and files in the current working directory.
//FtpInfo is an abstract base class
//for FtpDirectoryInfo and FtpFileInfo
foreach (FtpInfo info in _ftp.Dir()) {
//if it is a directory on the server
if (info is FtpDirectoryInfo) {
FtpDirectoryInfo dir = (FtpDirectoryInfo)info;
//example of ftp dir attributes
Trace.WriteLine("Directory name: " + dir.Name);
Trace.WriteLine("Directory creation time: " + dir.CreationTime);

//the FtpDirectoryInfo object also has methods for
//deleting it self, listing subdirectories, files and so on

//if it is a file on the server
} else if (info is FtpFileInfo) {
FtpFileInfo file = (FtpFileInfo)info;
//example of ftp file attributes
Trace.WriteLine("File name: " + file.Name);
Trace.WriteLine("File size: " + file.Size);

//the FtpFileInfo object also has methods for
//deleting it self, returing the path and so on
}
}

/** Simple put and get functions **/
//put and get has several overloads, but in this example
//we will only SHOW the most commonly used

//put a local file to the server in the current
//working directory
_ftp.Put("c:/test/somefile.txt","somefile.txt");

//get a remote file FROM the server to the local machine
_ftp.Get("c:/test/somefile_copy.txt","somefile.txt");


/** Recursive Upload, download and deletion of directories **/

//Recursive upload of a local directory to the ftp server
_ftp.PutDirectory("c:/testlocal","/testremote");

//Recursive download of a remote directory to the local machine
_ftp.GetDirectory("c:/testlocal","/testremote/testlocal");

//Recursive DELETE a directory on the server
_ftp.DeleteDirectory("/testremote");


/** Cathing Ftp Specific exeptions **/

} catch (FtpException ex) {
//FtpException gives you the last ftp replycode to work with
Trace.WriteLine("Ftp Specific error [Replycode " + ex.ReplyCode + "]: " + ex.Message);

} catch (Exception ex) {
Trace.WriteLine("Unknown error: " + ex.Message);

} finally {

/** Disconnect FROM the sever after use **/
//remeber to call Disconnect in the finally statement
//to avoid hanging connections if something goes wrong
if (_ftp != null) { _ftp.Disconnect(); }
}

}
}
}


ovenstående eksempel kan også findes på sourceforge, hvor koden er indrykket.