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 static FTPItem Parse(string line) { if (string.IsNullOrEmpty(line)) { return(null); } string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 4) { return(null); } FTPItem retVal; if (parts[2] == "<DIR>") { retVal = new FTPFolder(); } else { retVal = new FTPFile(); ((FTPFile)retVal).Size = Convert.ToUInt32(parts[2]); } DateTime dt; string s = parts[0] + " " + parts[1]; if (DateTime.TryParse(s, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal, out dt) == false) { return(null); } retVal.DateTime = dt; retVal.Name = string.Join(" ", parts.Skip(3)); return(retVal); }
public bool DownloadFile(string ftpFullFileName, string localFullFileName) { _errorMsg = null; if (string.IsNullOrEmpty(ftpFullFileName) || string.IsNullOrEmpty(localFullFileName)) { return(false); } DownloadFileBeforeHandler?.Invoke(this, ftpFullFileName); string[] tmpPair = parseFTPFileFullName(ftpFullFileName); if (tmpPair == null) { _errorMsg = "Error during parse full name to path and name: " + ftpFullFileName; DownloadFileAfterHandler?.Invoke(this, _errorMsg); return(false); } string ftpPath = tmpPair[0]; string ftpFileName = tmpPair[1]; FTPFile ftpFile = GetFTPFile(ftpPath, ftpFileName); if (ftpFile == null) { DownloadFileAfterHandler?.Invoke(this, "Can not get file: " + ftpFullFileName); return(false); } try { FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create(ftpFullFileName); requestDir.KeepAlive = false; requestDir.UsePassive = true; requestDir.Method = WebRequestMethods.Ftp.DownloadFile; requestDir.Credentials = new NetworkCredential(this.Login, this.PWD); FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse(); Stream responseStream = responseDir.GetResponseStream(); StreamReader stream = new StreamReader(responseStream); using (FileStream writer = new FileStream(localFullFileName, FileMode.Create)) { long length = responseDir.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[2048]; readCount = responseStream.Read(buffer, 0, bufferSize); while (readCount > 0) { writer.Write(buffer, 0, readCount); readCount = responseStream.Read(buffer, 0, bufferSize); } } stream.Dispose(); responseStream.Dispose(); responseDir.Dispose(); // set created date FileInfo fInfo = new FileInfo(localFullFileName); fInfo.CreationTime = ftpFile.DateTime; fInfo.LastWriteTime = ftpFile.DateTime; DownloadFileAfterHandler?.Invoke(this, "Success downloaded to: " + localFullFileName); } catch (Exception ex) { _errorMsg = ex.Message; DownloadFileAfterHandler?.Invoke(this, _errorMsg); return(false); } return(true); }