以下のサンプルコードは、FTPリストを取得する方法を示します。 このサンプルコードでは、Startメソッドを使用してgetListing関数をワーカースレッドで実行し、Marshalメソッドを使用してデータをUIスレッドにマーシャリングしています。
private void button1_Click(object sender, EventArgs e)
{
// FtpSessionを作成し、ワーカースレッドでリストを取得します。
FtpSession session = new FtpSession();
session.RemoteEndPoint.HostNameOrAddress = myServer;
session.Username = myUsername;
session.Password = myPassword;
// ワーカースレッドでリストを取得します。
ftp1.Start(getListing, session);
}
private void getListing(object state)
{
try
{
// サーバーにログインしてリストを取得します。
ftp1.Session = state as FtpSession;
ftp1.Connect();
ftp1.Authenticate();
Listing listing = ftp1.List("", "", ListType.Full);
// リストをUIスレッドにマーシャリングします。
ftp1.Marshal(listing, "", null);
}
catch (Exception ex)
{
ftp1.Marshal(ex);
}
finally
{
// サーバーからログアウトします。
ftp1.Close();
}
}
private void ftp1_Listing(object sender, ListingEventArgs e)
{
// リストに含まれるすべてのリスト項目をリストボックスに追加します。
foreach (ListEntry entry in e.Listing)
listBox1.Items.Add(entry.Text);
}
private void ftp1_Error(object sender, ErrorEventArgs e)
{
MessageBox.Show(e.GetException().Message);
}
private void ftp1_Connection_Log(object sender, DataEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.Data.ToString());
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
' FtpSessionを作成し、ワーカースレッドでリストを取得します。
Dim session As FtpSession = New FtpSession()
session.RemoteEndPoint.HostNameOrAddress = myServer
session.Username = myUsername
session.Password = myPassword
' ワーカースレッドでリストを取得します。
ftp1.Start(AddressOf getListing, session)
End Sub
Private Sub getListing(ByVal state As Object)
Try
' サーバーにログインしてリストを取得します。
ftp1.Session = TryCast(state, FtpSession)
ftp1.Connect()
ftp1.Authenticate()
Dim listing As Listing = ftp1.List("", "", ListType.Full)
' リストをUIスレッドにマーシャリングします。
ftp1.Marshal(listing, "", Nothing)
Catch ex As Exception
ftp1.Marshal(ex)
Finally
' サーバーからログアウトします。
ftp1.Close()
End Try
End Sub
Private Sub ftp1_Listing(ByVal sender As Object, ByVal e As ListingEventArgs) Handles ftp1.Listing
' リストに含まれるすべてのリスト項目をリストボックスに追加します。
For Each entry As ListEntry In e.Listing
listBox1.Items.Add(entry.Text)
Next entry
End Sub
Private Sub ftp1_Error(ByVal sender As Object, ByVal e As ErrorEventArgs) Handles ftp1.Error
MessageBox.Show(e.GetException().Message)
End Sub
Private Sub ftp1_Connection_Log(ByVal sender As Object, ByVal e As DataEventArgs) Handles ftp1.Connection.Log
System.Diagnostics.Debug.WriteLine(e.Data.ToString())
End Sub
System.Object
Dart.Ftp.ListEntry