Table of Contents

Class FTP

Namespace
SunamoFtp.FtpClients
Assembly
SunamoFtp.dll

FTP client implementation using raw socket commands

public class FTP : FtpBase
Inheritance
FTP
Inherited Members
Extension Methods

Constructors

FTP(IFtpClientExt)

IK, OOP.

public FTP(IFtpClientExt ftpClient)

Parameters

ftpClient IFtpClientExt

Methods

ChdirLite(string)

Changes the current directory on the FTP server, creating it if necessary. Removes trailing slash from directory name if present. Lists directory contents to verify the directory exists before changing to it. If directory doesn't exist, creates it with mkdir. Updates the PathSelector when changing directories.

public override void ChdirLite(string directoryName)

Parameters

directoryName string

The name of the directory to change to. Empty string changes to www root.

Close()

Closes the FTP connection and cleans up resources. Sends QUIT command if client socket is not null. Closes and nullifies the client socket, and sets IsLoggedIn to false.

public void Close()

Connect()

Connects to the FTP server. Not implemented - throws NotImplementedMethod exception.

public override void Connect()

CreateDataSocket()

Creates a data socket for passive mode FTP transfer. Sends PASV command, parses the IP address and port from the server's reply. Extracts the IP address parts and joins them with dots. Calculates the port by bit-shifting the 5th part by 8 and adding the 6th part. Creates a Socket, IPEndPoint and attempts to connect to the server.

public Socket CreateDataSocket()

Returns

Socket

A connected socket ready for data transfer

CreateDirectoryIfNotExists(string)

Changes to the specified directory, creating it if it doesn't exist. Skips "." and ".." directory references. If the directory doesn't exist, creates it with mkdir, otherwise changes to it with ChdirLite.

public override void CreateDirectoryIfNotExists(string directoryName)

Parameters

directoryName string

The name of the directory to change to or create

DebugActualFolder()

Outputs isDebug information about the current folder. Not implemented - throws NotImplementedMethod exception.

public override void DebugActualFolder()

DeleteRecursively(List<string>, string, int, List<DirectoriesToDeleteFtp>)

Recursively deletes a directory and all its contents from the FTP server. Changes to the directory, lists all contents, deletes files and recursively deletes subdirectories. After all contents are deleted, goes to parent folder and removes the now-empty directory.

public override void DeleteRecursively(List<string> foldersToSkip, string directoryName, int i, List<DirectoriesToDeleteFtp> directoriesToDelete)

Parameters

foldersToSkip List<string>

List of folder names to skip during deletion

directoryName string

The name of the directory to delete

i int

Recursion depth level (currently unused)

directoriesToDelete List<DirectoriesToDeleteFtp>

DeleteRemoteFile(string)

Deletes a remote file from the FTP server. Logs in if not authenticated, then sends DELE command with the file name. If the first attempt fails, tries again with URL-decoded file name.

public override bool DeleteRemoteFile(string fileName)

Parameters

fileName string

The name of the file to delete

Returns

bool

Always returns true (throws exception on failure)

Download(string, string, bool)

Downloads a file from the FTP server to a local file. If the local file exists and deleteLocalIfExists is true, deletes it first. Creates the local file if it doesn't exist, then sends RETR command and writes all received bytes.

public override bool Download(string remFileName, string locFileName, bool deleteLocalIfExists)

Parameters

remFileName string

The name of the remote file to download

locFileName string

The local file path to save to. Throws exception if null.

deleteLocalIfExists bool

Whether to delete the local file if it already exists

Returns

bool

True if download succeeded, false if file couldn't be deleted or already exists

GetFSEntriesListRecursively(List<string>)

Recursively retrieves all file system entries (files and folders) from the FTP server starting at the current path.

public override Dictionary<string, List<string>> GetFSEntriesListRecursively(List<string> foldersToSkip)

Parameters

foldersToSkip List<string>

List of folder names to skip during traversal

Returns

Dictionary<string, List<string>>

Dictionary mapping folder paths to lists of entry details

GetFileList(string)

Gets list of files matching the specified mask from current FTP directory

public List<string> GetFileList(string mask)

Parameters

mask string

File mask pattern

Returns

List<string>

GetFileSize(string)

Gets the size of a remote file by sending the SIZE command. Logs in if not already authenticated.

public override long GetFileSize(string fileName)

Parameters

fileName string

The name of the file to get the size of

Returns

long

The size of the file in bytes

GetRemotePath()

Gets current remote directory.

public string GetRemotePath()

Returns

string

GetSslStream()

Gets an SSL stream on the client socket. This is a convenience method that calls GetSslStream(Socket) with the clientSocket.

public void GetSslStream()

GetSslStream(Socket)

Creates an SSL stream from the specified socket and authenticates as client. If authentication succeeds, assigns the SSL stream to stream2 (for upload) or stream (for download). Catches and re-throws exceptions with message details.

public void GetSslStream(Socket clientSocket)

Parameters

clientSocket Socket

The socket to create the SSL stream from

GoToPath(string)

Navigates to the specified remote folder path on the FTP server, creating directories if needed.

public override void GoToPath(string remoteFolder)

Parameters

remoteFolder string

The full path to the remote folder to navigate to

GoToUpFolder()

Navigates to parent folder on FTP server with validation

public override void GoToUpFolder()

GoToUpFolderForce()

Navigates to parent folder on FTP server (forced, no validation)

public override void GoToUpFolderForce()

ListDirectoryDetails()

Lists all files and directories in the current remote directory with detailed information. Uses FtpWebRequest with ListDirectoryDetails method.

public override List<string> ListDirectoryDetails()

Returns

List<string>

List of strings containing detailed directory entry information

Login()

Logs in to the FTP server using the configured credentials. This method should be called immediately after setting the connection variables. If not connected, connects to the server first, then sends USER command, and PASS command if required. Response code 230 means login successful without password, otherwise sends password with PASS command.

public void Login()

LoginIfIsNot(bool)

Performs login to FTP server if not already logged in

public override void LoginIfIsNot(bool isInitialLogin)

Parameters

isInitialLogin bool

Indicates if this is initial startup login

LoginWithoutUser()

Establishes a TCP socket connection to the FTP server without user authentication. Creates a Stream-type TCP socket and connects to the remote server. Throws IOException if the response code is not 220. This method must always be called before authenticating with user credentials.

public void LoginWithoutUser()

Mkdir(string)

Creates a directory in the current folder on the FTP server. Sends MKD command with directory name, then changes to the new directory. Logs in if not authenticated before creating the directory.

public override bool Mkdir(string directoryName)

Parameters

directoryName string

The name of the directory to create

Returns

bool

Always returns true (throws exception on failure)

RenameRemoteFile(string, string)

Renames a file on the FTP server. Sends RNFR command with old file name, waits for 350 response, then sends RNTO command with new file name. Logs in if not authenticated before executing the rename operation.

public override void RenameRemoteFile(string oldFileName, string newFileName)

Parameters

oldFileName string

The current name of the file

newFileName string

The new name for the file

Rmdir(List<string>, string)

Removes a directory from the current folder on the FTP server. Sends RMD command with directory name. If the directory is not empty (error 550), deletes it recursively. Logs in if not authenticated before removing the directory.

public override bool Rmdir(List<string> foldersToSkip, string directoryName)

Parameters

foldersToSkip List<string>

List of folder names to skip during recursive deletion

directoryName string

The name of the directory to remove

Returns

bool

Always returns true (throws exception on failure)

SendCommand(string)

Sends a command to the FTP server. Converts the command to ASCII bytes, sends it via stream or socket, and reads the server's reply. Stores the response in reply and retValue properties.

public void SendCommand(string command)

Parameters

command string

The FTP command to send (without CRLF terminator)

SetBinaryMode(bool)

Sets the file transfer mode on the FTP server. Sends TYPE I command for binary mode or TYPE A command for ASCII mode.

public void SetBinaryMode(bool isBinary)

Parameters

isBinary bool

True for binary mode, false for ASCII mode

SetDebug(bool)

Sets the isDebug mode for the FTP client. When enabled, outputs detailed command and response information.

public void SetDebug(bool isDebug)

Parameters

isDebug bool

True to enable isDebug mode, false to disable

SetRemotePath(string)

Sets the remote FTP path and navigates to it

public void SetRemotePath(string remotePath)

Parameters

remotePath string

Remote FTP path to navigate to

SetUseStream(bool)

Sets whether to use binary transfer mode

public void SetUseStream(bool useBinaryMode)

Parameters

useBinaryMode bool

True to enable binary transfer, false for ASCII

Upload(string, bool, byte[])

Uploads a file to the FTP server, with optional resume capability. Logs in if not authenticated, sends PASV command, and creates data socket. If resuming, sets binary mode and gets remote file size to determine offset. Sends REST command with offset if resuming, then STOR command with file name. Reads bytes from file and sends them via socket, then Closes socket and verifies server response.

public void Upload(string filePath, bool resume, byte[] buffer)

Parameters

filePath string

The path to the file to upload

resume bool

Whether to resume a previous upload from the last position

buffer byte[]

The byte buffer to use for reading the file

UploadSecure(string, bool)

Uploads a file to the FTP server using a secure SSL connection. Sends PASV command, creates secure data socket, and optionally resumes from previous upload. If resuming, sets binary mode and gets remote file size to determine offset. Sends STOR command with file name and writes all bytes from file to the secure stream. Closes socket and stream after upload and verifies server response.

public void UploadSecure(string filePath, bool isResume)

Parameters

filePath string

The path to the file to upload

isResume bool

Whether to resume a previous upload from the last position

WriteDebugLog(string, string, params object[])

isDebug output method. Not implemented - throws NotImplementedMethod exception.

public override void WriteDebugLog(string context, string text, params object[] args)

Parameters

context string

What to isDebug

text string

Format string for output

args object[]

Arguments for the format string

uploadSecureFolder()

Placeholder method for uploading a folder securely. Currently empty - not implemented. Should verify that _.txt file is uploaded first.

public void uploadSecureFolder()