Assembly: edtFTPnetPRO (in edtFTPnetPRO.dll) Version: 18.104.22.168
FTPConnection provides FTP client functionality. It is a .NET Component which may be used in visual designers, or it may be used as a conventional class.
Constructing and connecting: The constructor for FTPConnection takes no arguments. Before connecting to a server, the FTPConnection must be configured with the ServerAddress, UserName and Password. After this has been done, the Connect()()()() method should be called, which will connect and log onto the server.
FTPConnection ftp = new FTPConnection(); ftp.ServerAddress = "myservername"; ftp.UserName = "myusername"; ftp.Password = "mypassword"; ftp.Connect(); ftp.Close();
Directory listings: Directory listings may be obtained in two basic forms: (1) The method GetFiles(String) returns strings containing file-names. (2) The method GetFileInfos(String) returns FTPFile objects which contain information about the file including name, size, and date.
Downloading and uploading files: There are many different methods for downloading files from the server and uploading them to the server. Data may be copied from or to:
- Files (DownloadFile(String, String) and UploadFile(String, String)
- Streams (DownloadStream(Stream, String) and UploadStream(Stream, String))
- Byte-arrays (DownloadByteArray(String) and UploadByteArray(array<Byte>(), String)
Other file operations: Other operations that may be performed on files are:
- Renaming files using the RenameFile(String, String) method.
- Deleting files using the DeleteFile(String) method.
- Getting a files size using the GetSize(String) method and its modification time using the GetLastWriteTime(String) method.
Directories: The server maintains a "working directory" for each session. The path of the current working directory may be set and retrieved using the WorkingDirectory property. Changing directory to a subdirectory of the current working directory may be done by setting the same property, or by using the ChangeWorkingDirectory(String) method. Changing up to a parent directory is done using the ChangeWorkingDirectoryUp()()()() method. Empty directories may be removed using DeleteDirectory(String). If a non-empty directory is to be deleted then all the files and subdirectories in it must be deleted first. ExFTPConnection and SecureFTPConnection have methods for achieving this in a single method-call.
Events:FTPConnection fires events before and after most FTP operations. For example, before a file is downloaded the Downloading event is fired, while it's downloading the BytesTransferred event is fired each time TransferNotifyInterval bytes have been transferred, and after it's been downloaded the Downloaded event is fired. Operations that have corresponding events are:
|Connecting and logging in||Connecting, LoggingIn, LoggedIn, and Connected|
|Closing||Closing and Closed|
|Downloading data||Downloading, BytesTransferred and Downloaded|
|Uploading data||Uploading, BytesTransferred and Uploaded|
|Changing directories||DirectoryChanging and DirectoryChanged|
|Deleting files/directories||Deleting and Deleted|
|Sending FTP commands||CommandSent and ReplyReceived|
Logging: It is often very helpful to look at the detailed logging output if any problems are experienced communicating with FTP servers. All commands sent to the FTP server and subsequent replies are logged, and can be made available on the console or directed to a log file. Much other useful information is also logged.
Operations are logged at different levels, ranging from no logging (Off) to most verbose (All). Intermediate levels are Fatal, Error, Warning and Information.For example, if the overall level is set to Information, then Fatal, Error, Warning and Information log statements will be outputted, but Debug statements will not be logged.
The overall level can be set via the LogLevel property:
ftpConnection.LogLevel = LogLevel.Information;
|LogToConsole||If set to true, logging is directed to the console.|
|LogToTrace||If set to true, logging is directed to .NET's Trace facility.|
|LogFile||If to a valid filename, logging is directed to the file.|
Overview of the FTP protocol: FTP is defined in the Request For Comments 959 document (RFC 959), which can be obtained from the Internet Engineering Task Force.
FTP requires a client program (FTP client) and a server program (FTP server). The FTP client can fetch files and file details from the server, and also upload files to the server. The server is generally loginPassword protected.
FTP commands are initiated by the FTP client, which opens a TCP connection called the control connection to the server. This control connection is used for the entire duration of a session between the FTP client and server. A session typically begins when the FTP client logs in, and ends when the quit command is sent to the server. The control connection is used exclusively for sending FTP commands and reading server replies - it is never used to transfer files.
Transient TCP connections called data connections are set up whenever data (normally a file's contents) is to be transferred. For example, the FTP client issues a command to retrieve a file from the server via the control channel. A data connection is then established, and the file's contents transferred to the FTP client across it. Once the transfer is complete, the data connection is closed. Meanwhile, the control connection is maintained.
Compliance: FTPClient implements FTP as defined by RFC959. It attempts to match the standard as closely as possible, but due to variation in the level of compliance of the numerous FTP servers available, it sometime allows servers some tolerance. If the property StrictReturnCodes is set to false then FTPClient is more tolerant of non-compliant servers.