Существует ли простой способ получить отфильтрованный список файлов с ftp-сервера, используя обычное выражение фильтра (* .txt, Ben * .csv, *. *).
В настоящее время у меня есть возможность получить список всех файлов и каталогов, а затем выполнить итерацию по ним, сравнивая каждый из них с выражением фильтра, создавая новый список для возврата.
Хотя мой нынешний способ сработает, мне интересно, есть ли более элегантный способ сделать это.
Вот некоторый псевдокод для создания списка файлов:
/// <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;
}