メインコンテンツまでスキップ

FTP

Overview

PortDIC provides an FTP/FTPS communication handler (IFTPHandler) backed by portftp.dll (Rust FFI). It supports file upload/download, directory management, bulk transfers, and progress callbacks. Both plain FTP and FTPS (TLS/SSL) are supported.

Each [FTP] class registers its own independent FTP connection.


Quick Setup

1. Define an FTP handler class

using Portdic;
using Portdic.FTP;

[FTP]
public class MyFTPHandler
{
[FTPHandler]
public IFTPHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetHost("192.168.1.100");
handler.SetPort(21);
handler.SetUsername("admin");
handler.SetPassword("password");
handler.SetSecure(false); // true = FTPS (TLS)

handler.OnProgress += OnProgress;
handler.OnList += OnList;
handler.OnEvent += OnEvent;
}

private void OnProgress(string name, string fileName, long transferred, long total, int percent)
{
Console.WriteLine($"[{name}] {fileName}: {percent}% ({transferred}/{total} bytes)");
}

private void OnList(string name, string json)
{
Console.WriteLine($"[{name}] Listing: {json}");
}

private void OnEvent(string name, string eventType, string description)
{
Console.WriteLine($"[{name}] {eventType}: {description}");
}
}

2. Register and run

Port.Add<MyFTPHandler>("ftp_server1");
Port.Run();

FTPS (Secure FTP)

[Preset]
private void Preset()
{
handler.SetHost("secure.server.com");
handler.SetPort(990); // FTPS implicit port
handler.SetUsername("user");
handler.SetPassword("pass");
handler.SetSecure(true); // enable TLS
handler.SetPassive(true); // passive mode (recommended)

handler.OnEvent += OnEvent;
}

File Operations

Upload and download

// Upload a single file
int result = handler.Upload(@"C:\local\report.csv", "/remote/reports/report.csv");

// Download a single file
int result = handler.Download("/remote/data/log.txt", @"C:\local\log.txt");

// Bulk upload — all files in a local directory
int count = handler.UploadDir(@"C:\local\batch", "/remote/batch");

// Bulk download — all files from a remote directory
int count = handler.DownloadDir("/remote/archive", @"C:\local\archive");

Returns the number of bytes (single file) or file count (bulk), or -1 on error.

Directory management

// Get current working directory
string pwd = handler.Pwd();

// Change directory
handler.Cwd("/remote/data");

// Go up one level
handler.Cdup();

// Create directory
handler.Mkdir("/remote/new_folder");

// Remove empty directory
handler.Rmdir("/remote/old_folder");

File management

// Delete a file
handler.Delete("/remote/old_report.csv");

// Rename / move a file
handler.Rename("/remote/temp.csv", "/remote/final.csv");

// Get file size in bytes
long size = handler.Size("/remote/data.zip");

Directory listing

// Full listing (returns JSON array)
string json = handler.List("/remote/data");
// Each entry: { "name", "size", "is_dir", "modified", "raw" }

// File names only
string names = handler.Nlst("/remote/data");
// Returns JSON string array: ["file1.txt", "file2.csv", ...]

API Reference

Attributes

AttributeTargetDescription
[FTP]ClassMarks the class as an FTP handler container
[FTPHandler]PropertyInjects the IFTPHandler instance
[Preset]MethodCalled before Open() to configure the handler

Configuration methods

MethodDescription
SetHost(string host)FTP server address (e.g., "192.168.1.100")
SetPort(int port)Port number. Default: 21 (FTP), 990 (FTPS implicit)
SetUsername(string username)Authentication username
SetPassword(string password)Authentication password
SetSecure(bool secure)true = FTPS (TLS/SSL); false = plain FTP
SetPassive(bool passive)true = passive mode (default); false = active mode

Connection methods

MethodReturnsDescription
Open()ERROR_CODEOpen the FTP connection
Close()ERROR_CODEClose the FTP connection
IsConnectedbooltrue if connected

Transfer methods

MethodReturnsDescription
Upload(localPath, remotePath)intUpload a file
Download(remotePath, localPath)intDownload a file
UploadDir(localDir, remoteDir)intUpload all files in a directory
DownloadDir(remoteDir, localDir)intDownload all files from a directory

Directory & file methods

MethodReturnsDescription
Pwd()stringCurrent working directory
Cwd(path)Change directory
Cdup()Go to parent directory
Mkdir(path)Create directory
Rmdir(path)Remove empty directory
List(path)stringJSON array of directory entries
Nlst(path)stringJSON array of file names only
Delete(path)Delete a file
Rename(from, to)Rename or move a file
Size(path)longFile size in bytes

Logging

MethodDescription
SetLogger(string rootPath)Enable hourly-rotated transfer log files
WriteLog(string v)Write a custom entry to the log file

Events

OnProgress

Fired during file transfer with progress updates.

handler.OnProgress += (string name, string fileName, long transferred, long total, int percent) =>
{
Console.WriteLine($"[{name}] {fileName}: {percent}%");
};
ParameterDescription
nameRegistration key
fileNameName of the file being transferred
transferredBytes transferred so far
totalTotal file size in bytes
percentProgress percentage (0–100)

OnList

Fired when a directory listing result is available.

handler.OnList += (string name, string json) =>
{
// json: [{"name":"file.txt","size":1024,"is_dir":false,"modified":"...","raw":"..."}]
Console.WriteLine(json);
};

OnEvent

Fired on connection and transfer state changes.

handler.OnEvent += (string name, string eventType, string description) =>
{
Console.WriteLine($"[{name}] {eventType}: {description}");
};
eventTypeTriggered When
CONNECTINGConnection attempt started
CONNECTEDConnected to FTP server
TLS_ESTABLISHEDTLS handshake completed (FTPS)
DISCONNECTEDConnection closed
UPLOADINGFile upload started
UPLOADEDFile upload completed
DOWNLOADINGFile download started
DOWNLOADEDFile download completed
CWDDirectory changed
MKDIRDirectory created
RMDIRDirectory removed
DELETEDFile deleted
RENAMEDFile renamed
ERRORError occurred (description contains detail)

Error Codes

CodeValueMeaning
ERR_CODE_NO_ERROR1Success
ERR_CODE_OPEN-1Open/connect failed
ERR_CODE_DLL_NOT_LOADED-2portftp.dll not loaded
ERR_CODE_PORTNAME_EMPTY-3Connection name not set
ERR_CODE_DLL_FUNC_NOT_CONFIRM-4Required DLL function unavailable
ERR_CODE_CONNECT_FAILED-5Connection attempt failed

  • FileSender — QUIC-based high-speed file transfer
  • TCP — Raw TCP communication
  • Serial — RS232/RS485 serial communication