public FTPFile GetFTPFile(string ftpPath, string ftpFileName) { if (_errorMsg != null) { _errorMsg = null; } FTPFile retVal = null; // fields checking if (string.IsNullOrEmpty(ftpPath) || string.IsNullOrEmpty(ftpFileName)) { return(retVal); } if (!ftpPath.EndsWith("/")) { ftpPath += "/"; } try { FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create(ftpPath); requestDir.KeepAlive = this.IsAlive; requestDir.UsePassive = this.IsPassive; requestDir.Method = WebRequestMethods.Ftp.ListDirectoryDetails; requestDir.Credentials = new NetworkCredential(this.Login, this.PWD); FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse(); using (StreamReader readerDir = new StreamReader(responseDir.GetResponseStream())) { string line = readerDir.ReadLine(); FTPItem item; while (line != null) { item = FTPItem.Parse(line); if ((item != null) && (item.Name == ftpFileName) && (item is FTPFile)) { FTPFile ftpFile = (FTPFile)item; retVal = new FTPFile() { ItemType = FSItemTypeEnum.File, FullName = ftpPath + ftpFileName, Name = ftpFileName, DateTime = ftpFile.DateTime, Size = ftpFile.Size }; break; } line = readerDir.ReadLine(); } } responseDir.Close(); } catch (Exception ex) { _errorMsg = ex.Message; } return(retVal); }
public FTPFolder getFTPFolder(string path) { FTPFolder retVal = null; FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create(path); FtpWebRequest.DefaultCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); requestDir.KeepAlive = this.IsAlive; requestDir.UsePassive = this.IsPassive; requestDir.Method = WebRequestMethods.Ftp.ListDirectoryDetails; requestDir.Credentials = new NetworkCredential(this.Login, this.PWD); FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse(); using (StreamReader readerDir = new StreamReader(responseDir.GetResponseStream())) { retVal = new FTPFolder() { ItemType = FSItemTypeEnum.Folder, FullName = path }; string line = readerDir.ReadLine(); FTPItem item; while (line != null) { item = FTPItem.Parse(line); if (item != null) { item.FullName = path + item.Name; retVal.Items.Add(item); } line = readerDir.ReadLine(); } } responseDir.Close(); return(retVal); }