基于.Net的支持FTP及FTPS的FTP类库FluentFTP

基于.Net的支持FTP及FTPS的FTP类库FluentFTP

  • 开源协议:MIT
  • 操作系统: Windows
  • 开发语言:C#
  • 项目所有者:hgupta9
  • 收录时间:2017-01-08
  • 分享:
编辑评级
4

项目详细介绍

      FluentFTP是一款老外开发的基于.Net的支持FTP及的FTPS 的FTP类库,FluentFTP是完全托管的FTP客户端,被设计为易于使用和易于扩展。它支持文件和目录列表,上传和下载文件和SSL / TLS连接。它可以连接到Unix和Windows IIS建立FTP服务器。这个项目是完全开发托管C #。FluentFTP是MIT许可下发布许可。


主要特点:

支持FTP、FTPS(FTP SSL)
SFTP是不支持它的FTP在SSH,一个完全不同的协议(使用ssh.net为)
主动和被动的数据连接(PASV,电子,港口和EPRT)
同步和异步方法(IAsyncResult模式)的所有操作
支持DrFTPD的(非标)预处理命令
支持FTP代理(用户名@主机,HTTP 1.1)


它的用法非常简单:

// create an FTP client
FtpClient client = new FtpClient();
client.Host = "123.123.123.123";

// if you don't specify login credentials, we use the "anonymous" user account
client.Credentials = new NetworkCredential("david", "pass123");

// begin connecting to the server
client.Connect();

// get a list of files and directories in the "/htdocs" folder
foreach (FtpListItem item in client.GetListing("/htdocs") {

    // if this is a file
    if (item.Type == FtpFileSystemObjectType.File){

        // get the file size
        long size = client.GetFileSize(item.FullName);

    }

    // get modified date/time of the file or folder
    DateTime time = client.GetModifiedTime(item.FullName);

    // calculate a hash for the file on the server side (default algorithm)
    FtpHash hash = client.GetHash(item.FullName);

}

// upload a file
cl.UploadFile(@"C:\MyVideo.mp4", "/htdocs/big.txt");

// rename the uploaded file
cl.Rename("/htdocs/big.txt", "/htdocs/big2.txt");

// download the file again
cl.DownloadFile(@"C:\MyVideo_2.mp4", "/htdocs/big2.txt");

// delete the file
client.DeleteFile("/htdocs/big2.txt");

// delete a folder recursively
client.DeleteDirectory("/htdocs/extras/", true);

// check if a file exists
if (client.FileExists("/htdocs/big2.txt")){ }

// check if a folder exists
if (client.DirectoryExists("/htdocs/extras/")){ }

// disconnect! good bye!
client.Disconnect();


主要的Api如下:

Connection

  • new FtpClient() - Creates and returns a new FTP client instance.

  • Host - The FTP server IP or hostname. Required.

  • Port - The FTP port to connect to. Default: Auto (21 or 990 depending on FTPS config)

  • Credentials - The FTP username & password to use. Must be a valid user account registered with the server. Default: anonymous/anonymous

  • Connect() - Connects to an FTP server (uses TLS/SSL if configured).

  • Disconnect() - Closes the connection to the server immediately.

  • Execute() - Execute a custom or unspported command.

  • SystemType - Gets the type of system/server that we're connected to.

  • IsConnected - Checks if the connection is still alive.

  • Capabilities - Gets the server capabilties (represented by flags).

  • HasFeature() - Checks if a specific feature (FtpCapability) is supported by the server.

File Management

  • GetListing() - Get a file listing of the given directory. Returns one FtpListItem per file or folder with all available properties set. Each item contains:

    • Type : The type of the object. (File, Directory or Link)

    • Name : The name of the object. (minus the path)

    • FullName : The full file path of the object.

    • Created : The created date/time of the object. Default: DateTime.MinValue if not provided by server.

    • Modified : The last modified date/time of the object. If you get incorrect values, try adding the FtpListOption.Modify flag which loads the modified date/time using another MDTM command. Default: DateTime.MinValue if not provided by server.

    • Size : The size of the file in bytes. If you get incorrect values, try adding the FtpListOption.Size flag which loads the file size using another SIZE command. Default: 0 if not provided by server.

    • LinkTarget : The full file path the link points to. Only filled for symbolic links.

    • LinkObject : The file/folder the link points to. Only filled for symbolic links if FtpListOption.DerefLink flag is used.

    • SpecialPermissions : Gets special permissions such as Stiky, SUID and SGID. (*NIX only)

    • OwnerPermissions : User rights. Any combination of 'r', 'w', 'x' (using the FtpPermission enum). (*NIX only)

    • GroupPermissions : Group rights. Any combination of 'r', 'w', 'x' (using the FtpPermission enum). (*NIX only)

    • OtherPermissions : Other rights. Any combination of 'r', 'w', 'x' (using the FtpPermission enum). (*NIX only)

    • Input : The raw string that the server returned for this object. Helps debug if the above properties have been correctly parsed.

  • GetNameListing() - A simple command that only returns the list of file paths in the given directory, using the NLST command.

  • UploadFile() - Uploads a file from the local file system to the server. Returns true if succeeded, false if failed or file does not exist. Supports very large files since it uploads data in chunks of 65KB. Remote directories are NOT created if they do not exist.

  • DownloadFile() - Downloads a file from the server to the local file system. Returns true if succeeded, false if failed or file does not exist. Supports very large files since it downloads data in chunks of 65KB. Local directories are created if they do not exist.

  • GetWorkingDirectory() - Gets the full path of the current working directory.

  • SetWorkingDirectory() - Sets the full path of the current working directory.

  • CreateDirectory() - Creates a directory on the server. If the parent directories do not exist they are also created.

  • DeleteDirectory() - Deletes the specified directory on the server. If it is not empty then all subdirectories and files are recursively deleted.

  • DeleteFile() - Deletes the specified file on the server.

  • Rename() - Renames the file/directory on the server.

  • OpenRead() - Low level. Open a stream to the specified file for reading. Returns a standard Stream.

  • OpenWrite() - Low level. Opens a stream to the specified file for writing. Returns a standard Stream, any data written will overwrite the file, or create the file if it does not exist.

  • OpenAppend() - Low level. Opens a stream to the specified file for appending. Returns a standard Stream, any data written wil be appended to the end of the file.

  • GetModifiedTime() - Gets the last modified date/time of the file or folder.

  • GetFileSize() - Gets the size of the file in bytes.

  • DereferenceLink() - Recursively dereferences a symbolic link and returns the full path if found. The MaximumDereferenceCount property controls how deep we recurse before giving up.

File Hashing

  • HashAlgorithms - Get the hash types supported by the server, if any (represented by flags).

  • GetHash() - Gets the hash of an object on the server using the currently selected hash algorithm. Supported algorithms are available in the HashAlgorithms property. You should confirm that it's not equal to FtpHashAlgorithm.NONE (which means the server does not support the HASH command).

  • GetHashAlgorithm() - Query the server for the currently selected hash algorithm for the HASH command.

  • SetHashAlgorithm() - Selects a hash algorithm for the HASH command, and stores this selection on the server.

FTPS

  • EncryptionMode - Type of SSL to use, or none. Explicit is TLS, Implicit is SSL. Default: FtpEncryptionMode.None.

  • DataConnectionEncryption - Indicates if data channel transfers should be encrypted. Default: true.

  • SslProtocols - Encryption protocols to use. Default: SslProtocols.Default.

  • ClientCertificates - X509 client certificates to be used in SSL authentication process.

  • ValidateCertificate - Event is fired to validate SSL certificates. If this event is not handled and there are errors validating the certificate the connection will be aborted.

Advanced Settings

  • GetDataType() - Checks if the transfer data type is ASCII or binary.

  • SetDataType() - Sets the transfer data type to ASCII or binary. Internally called during file reads, writes and appends.

  • DataConnectionType - Active or Passive connection. Default: FtpDataConnectionType.AutoPassive (tries EPSV then PASV then gives up)

  • UngracefullDisconnection - Disconnect from the server without sending QUIT. Default: false.

  • Encoding - Text encoding (ASCII or UTF8) used when talking with the server. ASCII is default, but upon connection, we switch to UTF8 if supported by the server. Manually setting this value overrides automatic detection. Default: Auto.

  • InternetProtocolVersions - Whether to use IPV4 and/or IPV6 when making a connection. All addresses returned during name resolution are tried until a successful connection is made. Default: Any.

  • SocketPollInterval - Time that must pass (in milliseconds) since the last socket activity before calling Poll() on the socket to test for connectivity. Setting this interval too low will have a negative impact on perfomance. Setting this interval to 0 disables Poll()'ing all together. Default: 15000 (15 seconds).

  • ConnectTimeout - Time to wait (in milliseconds) for a connection attempt to succeed, before giving up. Default: 15000 (15 seconds).

  • ReadTimeout - Time to wait (in milliseconds) for data to be read from the underlying stream, before giving up. Default: 15000 (15 seconds).

  • DataConnectionConnectTimeout - Time to wait (in milliseconds) for a data connection to be established, before giving up. Default: 15000 (15 seconds).

  • DataConnectionReadTimeout - Time to wait (in milliseconds) for the server to send data on the data channel, before giving up. Default: 15000 (15 seconds).

  • SocketKeepAlive - Set SocketOption.KeepAlive on all future stream sockets. Default: false.

  • StaleDataCheck - Check if there is stale (unrequested data) sitting on the socket or not. In some cases the control connection may time out but before the server closes the connection it might send a 4xx response that was unexpected and can cause synchronization errors with transactions. To avoid this problem the Execute() method checks to see if there is any data available on the socket before executing a command. Default: true.

  • EnableThreadSafeDataConnections - Clone the control connection and establish another connection to the server for the data channel operation. This is a thread safe approach to make asynchronous operations on a single control connection transparent. Set this to false if your FTP server allows only one connection per username. Default: true.

  • IsClone - Checks if this control connection is a clone. Default: false.

  • MaximumDereferenceCount - The maximum depth of recursion that DereferenceLink() will follow symbolic links before giving up. Default: 20.




标签:ftp