private myFTPFileInfo GetFileInfo(string filename) { myFTPFileInfo ret = null; //Si es necesario actualiza la lista de archivos de la carpeta actual if (_workingFolder != _lastWorkingFolderListed || _fileInfoListDirty) { GetFileListDetailed(null); } foreach (myFTPFileInfo fileInfo in _fileInfoList) { if (fileInfo.Nombre.ToLower() == filename.ToLower()) { return(fileInfo); } } return(ret); }
/// <summary> /// Obtains a detailed list of the files in the current remote FTP folder according to a wildcard filename specification and allows to bypass the cached filelist /// </summary> /// <param name="fileSpec">Wildcard filename specification. Can contain subfolders and *'s. Example: subfolder/*.txt</param> /// <param name="forceRefresh">If true the cache is ignored</param> /// <returns></returns> /// <remarks>If wildcard is specified or forceRefresh is set to true the resulting file list is not cached</remarks> public List <myFTPFileInfo> GetFileListDetailed(string fileSpec, bool forceRefresh) { if (fileSpec == null || fileSpec.Trim().Length == 0) { fileSpec = "*"; } if (fileSpec.Length > 0 && !fileSpec.StartsWith("/")) { fileSpec = "/" + fileSpec; } List <myFTPFileInfo> fileList = new List <myFTPFileInfo>(); if (forceRefresh || fileSpec.Trim().Length > 0 || _fileInfoListDirty || _lastWorkingFolderListed != _workingFolder) { var req = (FtpWebRequest)WebRequest.Create("ftp://" + Server + RootFolder + _workingFolder + fileSpec); req.Proxy = null; req.EnableSsl = false; req.Credentials = new NetworkCredential(User, Password); req.Method = WebRequestMethods.Ftp.ListDirectoryDetails; myFTPFileInfo ftpFileInfo; _fileInfoList.Clear(); try { using (FtpWebResponse resp = (FtpWebResponse)req.GetResponse()) { using (StreamReader reader = new StreamReader(resp.GetResponseStream())) { while (!reader.EndOfStream) { ftpFileInfo = new myFTPFileInfo(reader.ReadLine()); if (ftpFileInfo.Nombre != null && ftpFileInfo.Nombre.Trim().Length > 0) { fileList.Add(ftpFileInfo); } } ; }; RecordResponseStatus(resp); }; } catch (System.Net.WebException ex) { if (((FtpWebResponse)ex.Response).StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { RecordResponseStatus((FtpWebResponse)ex.Response); } else { throw ex; } } if (fileSpec.Trim().Length == 0 || fileSpec.Trim() == "/*") { //Solo conserva cache de la lista de archivos cuando se solicita sin comodines _fileInfoList = fileList; _lastWorkingFolderListed = _workingFolder; _fileInfoListDirty = false; } return(fileList); } else { return(_fileInfoList); } }