C#.NET
private TKFP.Net.FtpClient OpenFtp(string server , string userid , string pass , string startpass)
{
TKFP.Net.IFtpLogon logon = new TKFP.Net.BasicFtpLogon(userid, pass);
TKFP.Net.FtpClient ftp = new TKFP.Net.FtpClient(logon, server, 21);
if (ftp.Connect())
{
Prefetch pr = new Prefetch();
pr.startdirectory = new TKFP.IO.DirectoryInfo(ftp, startpass);
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(pr.Start));
th.IsBackground = true;
th.Start();
System.Windows.Forms.MessageBox.Show("接続成功");
}
else
{
System.Windows.Forms.MessageBox.Show("接続失敗");
}
return ftp;
}
private class Prefetch
{
public TKFP.IO.DirectoryInfo startdirectory;
public void Start()
{
System.Collections.Queue Directories = new System.Collections.Queue();
Directories.Enqueue(startdirectory);
while (Directories.Count != 0)
{
TKFP.IO.DirectoryInfo Directory = (TKFP.IO.DirectoryInfo)Directories.Dequeue();
foreach (TKFP.IO.DirectoryInfo SubDirectory in Directory.GetDirectories())
{
Directories.Enqueue(SubDirectory);
}
}
}
}
VB.NET
Private Function OpenFtp(ByVal server As String, ByVal userid As String, ByVal pass As String, ByVal startpass As String) As TKFP.Net.FtpClient
Dim logon As New TKFP.Net.BasicFtpLogon(userid, pass)
Dim ftp As New TKFP.Net.FtpClient(logon, server, 21)
If ftp.Connect() Then
Dim pr As New Prefetch()
pr.startdirectory = New TKFP.IO.DirectoryInfo(ftp, startpass)
Dim th As New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf pr.Start))
th.IsBackground = True
th.Start()
System.Windows.Forms.MessageBox.Show("接続成功")
Else
System.Windows.Forms.MessageBox.Show("接続失敗")
End If
Return ftp
End Function
Private Class Prefetch
Public startdirectory As TKFP.IO.DirectoryInfo
Public Sub Start()
Dim Directories As New System.Collections.Queue()
Directories.Enqueue(startdirectory)
While Directories.Count <> 0
Dim Directory As TKFP.IO.DirectoryInfo = CType(Directories.Dequeue(), TKFP.IO.DirectoryInfo)
Dim SubDirectory As TKFP.IO.DirectoryInfo
For Each SubDirectory In Directory.GetDirectories()
Directories.Enqueue(SubDirectory)
Next SubDirectory
End While
End Sub
End Class
VB.NETのコードはC#.NETを元に「
C#.NET to VB.NET Translator」によって作成したものです
サンプルでは、FTPサーバーへの接続完了後に、フォルダを空読みするスレッドを立ち上げています。 先読み実行中でもTKFP.Net.FtpClientの通信はスレッドセーフなので、特別な排他処理を追加する必要がありません。