0
檢索經過篩選的文件列表是否有一種簡單的方法可以使用普通篩選器表達式(* .txt,Ben * .csv,*)從ftp服務器檢索文件的篩選列表。*)。如何通過FTP使用IP * Works V8
我目前擁有的選項是檢索所有文件和目錄的列表,然後遍歷它們,並將它們與過濾器表達式進行比較,爲返回建立一個新列表。
雖然我目前的方式可行,但我想知道是否有更優雅的方式來做到這一點。
下面是一些僞代碼來創建文件列表:
/// <summary>
/// Get a list of files on the FTP server in the pre-specified remote dir
/// </summary>
/// <param name="wildCard">file filter, eg "*.txt"</param>
/// <returns>list of files on server</returns>
public List<string> ListFiles(string wildCard)
{
List<string> result = new List<string>();
try
{
// Instantiate the process object
Ftp processor = new Ftp();
processor.User = m_User;
processor.Password = m_Pass;
processor.RemoteHost = m_Server;
processor.RemotePort = Convert.ToInt32(m_Port);
// Connect to the server
processor.Logon();
// Ensure there is a connection
if (processor.Connected == true)
{
//Retrieve the file list
// Clear the remote file so that it's clear we aren't doing a transfer
processor.RemoteFile = string.Empty;
processor.ListDirectoryLong();
DirEntryList filelist = processor.DirList;
// Iterate through the returned listing from the remote server
foreach (DirEntry entry in filelist)
{
// Perform match on the file filter
// Ensure only files are added to the list
// Add the entry to the list
}
processor.Logoff();
}
}
catch (Exception ex)
{
// Log the error
}
return result;
}