public List <SftpFile> ListDirectory(string server, int port, string directory, SSHListType listType, bool recurse, string directoryFilter, string fileFilter) { SftpClient client = null; List <SftpFile> ret = new List <SftpFile>(); try { client = OpenSftpClient(server, port); directory = AdjustPath(server, directory); List <string> directoriesListed = new List <string>(); Regex exclusiveFileFilters = STEM.Sys.IO.Path.BuildExclusiveFilter(fileFilter); Regex inclusiveFileFilters = STEM.Sys.IO.Path.BuildInclusiveFilter(fileFilter); Regex exclusiveDirectoryFilters = STEM.Sys.IO.Path.BuildExclusiveFilter(directoryFilter); Regex inclusiveDirectoryFilters = STEM.Sys.IO.Path.BuildInclusiveFilter(directoryFilter); ListDirectory(client, ret, directoriesListed, directory, listType, recurse, exclusiveDirectoryFilters, inclusiveDirectoryFilters, exclusiveFileFilters, inclusiveFileFilters); } catch { try { client.Disconnect(); } catch { } try { client.Dispose(); } catch { } throw; } return(ret); }
void ListDirectory(SftpClient client, List <SftpFile> sshListItems, List <string> directoriesListed, string directory, SSHListType listType, bool recurse, Regex exclusiveDirectoryFilters, Regex inclusiveDirectoryFilters, Regex exclusiveFileFilters, Regex inclusiveFileFilters) { List <SftpFile> list = null; try { try { foreach (SftpFile i in client.ListDirectory(directory)) { if (i.IsDirectory) { if (exclusiveDirectoryFilters != null && exclusiveDirectoryFilters.IsMatch(STEM.Sys.IO.Path.GetFileName(i.FullName).ToUpper())) { continue; } if (inclusiveDirectoryFilters != null && !inclusiveDirectoryFilters.IsMatch(STEM.Sys.IO.Path.GetFileName(i.FullName).ToUpper())) { continue; } if (listType == SSHListType.Directory || listType == SSHListType.All) { sshListItems.Add(i); } if (recurse && !directoriesListed.Contains(i.FullName)) { directoriesListed.Add(i.FullName); ListDirectory(client, sshListItems, directoriesListed, i.FullName, listType, recurse, exclusiveDirectoryFilters, inclusiveDirectoryFilters, exclusiveFileFilters, inclusiveFileFilters); } } else if (i.IsRegularFile) { if (listType == SSHListType.File || listType == SSHListType.All) { if (exclusiveFileFilters != null && exclusiveFileFilters.IsMatch(STEM.Sys.IO.Path.GetFileName(i.FullName).ToUpper())) { continue; } if (inclusiveFileFilters != null && !inclusiveFileFilters.IsMatch(STEM.Sys.IO.Path.GetFileName(i.FullName).ToUpper())) { continue; } sshListItems.Add(i); } } } } finally { RecycleClient(client); client = null; } if (list == null) { return; } } catch (Exception ex) { try { client.Disconnect(); } catch { } try { client.Dispose(); } catch { } STEM.Sys.EventLog.WriteEntry("Authentication.ListDirectory", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error); } }