以下のサンプルコードは、サーバーから複数のファイルを取得する方法を示します。 mget関数は、Ftp.Startメソッドに渡して非同期に実行できます。
private void mget()
{
// ディレクトリー内のテキストファイルを取得します。
ftp1.Session.RemoteEndPoint.HostNameOrAddress = myServer;
ftp1.Session.Username = myUsername;
ftp1.Session.Password = myPassword;
ftp1.Connect();
ftp1.Authenticate();
// 取得するtxtファイルのリストを取得します。
List<ListEntry> filesToGet = ftp1.ListDirectoryTree("MyTextFiles", "*.txt", true);
// リストに特定のファイルが含まれないようにします。
foreach (ListEntry entry in filesToGet)
{
if (entry.Name == "not_me.txt")
{
filesToGet.Remove(entry);
break;
}
}
// ファイルを取得します。
string workingDirectory = ftp1.GetDirectory();
ftp1.Get(filesToGet, workingDirectory + "/MyTextFiles", myLocalDirectory, Synchronize.Off);
// ログアウトします。
ftp1.Close();
}
Private Sub mget()
' ディレクトリー内のテキストファイルを取得します。
ftp1.Session.RemoteEndPoint.HostNameOrAddress = myServer
ftp1.Session.Username = myUsername
ftp1.Session.Password = myPassword
ftp1.Connect()
ftp1.Authenticate()
' 取得するtxtファイルのリストを取得します。
Dim filesToGet As List(Of ListEntry) = ftp1.List("MyTextFiles", "*.txt", True)
' リストに特定のファイルが含まれないようにします。
For Each entry As ListEntry In filesToGet
If entry.Name = "not_me.txt" Then
filesToGet.Remove(entry)
Exit For
End If
Next entry
' ファイルを取得します。
Dim workingDirectory As String = ftp1.GetDirectory()
ftp1.Get(filesToGet, workingDirectory & "/MyTextFiles", myLocalDirectory, Synchronize.Off)
' ログアウトします。
ftp1.Close()
End Sub