FTP サーバー上の各ファイルのファイル名、ファイル サイズ、および最終更新時刻を取得し、それを listView に入力したいと考えています。
FTP ホストを切り替えるまでは非常にうまく機能していましたが、新しいホストが実際の FTP クライアントと同じくらい高速であるにもかかわらず、今では非常に遅くなりました。
理由について明確な理由はありますか?
また、毎回ログイン資格情報を送信する必要がありますか?
最初のメソッドを使用して文字列配列を取得し、それを反復処理して、各項目で 2 番目のメソッドを使用してファイル サイズを取得しています。
public static string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest request;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://mysite.se/"));
request.UseBinary = true;
request.Credentials = new NetworkCredential(settings.Username, settings.Password);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UseBinary = true;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
// to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
public static int GetFileSize(string file)
{
//MessageBox.Show("getting filesize...");
StringBuilder result = new StringBuilder();
FtpWebRequest request;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://mysite.se/" + file));
request.UseBinary = true;
request.Credentials = new NetworkCredential(settings.Username, settings.Password);
request.Method = WebRequestMethods.Ftp.GetFileSize;
int dataLength = (int)request.GetResponse().ContentLength;
return dataLength;
}
catch (Exception ex)
{
//System.Windows.Forms.MessageBox.Show(ex.Message);
return 1337;
}
}