using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; namespace FTP_client { class FtpClient { private string _Host; private string _UserName; private string _Password; private FtpWebRequest ftpRequest; private FtpWebResponse ftpResponse; private bool _KeepAlive = true; private bool _UseBinary = true; private bool _UsePassive = true; private bool _UseSSL = false; private int bufferSize = 2048; private string last_error = string.Empty; private FtpStatusCode _StatusCode; private string _StatusDescription; public bool KeepAlive { set { _KeepAlive = value; } } public bool UseBinary { set { _UseBinary = value; } } public bool UsePassive { set { _UsePassive = value; } } public string Host { set { _Host = value; } } public string UserName { set { _UserName = value; } } public string Password { set { _Password = value; } } public string Error { get { return last_error; } } public bool UseSSL { set { _UseSSL = value; } } public FtpStatusCode StatusCode { get { return _StatusCode; } } public string StatusDescription { get { return _StatusDescription; } } // FTP NLIST - list directory contents File/Folder name only public string[] ListDirectory(string path) { return List(path, false); } // FTP LIST - list directory contents in detail (Name, Size, Created, etc.) public string[] ListDirectoryDetails(string path) { return List(path, true); } private string[] List(string path, bool details) { if (path == null || path == string.Empty) path = "/"; ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + _Host + path)); Options(ref ftpRequest); if (details) ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; else ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; string directoryRaw = string.Empty; using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { using (Stream ftpStream = ftpResponse.GetResponseStream()) { using (StreamReader ftpReader = new StreamReader(ftpStream, System.Text.Encoding.UTF8)) { directoryRaw = ftpReader.ReadToEnd(); } } _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; } ftpRequest = null; string[] directoryList = directoryRaw.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); return directoryList; } private void Options(ref FtpWebRequest obj) { obj.Credentials = new NetworkCredential(_UserName, _Password); obj.UseBinary = _UseBinary; obj.UsePassive = _UsePassive; obj.KeepAlive = _KeepAlive; obj.EnableSsl = _UseSSL; } // FTP RETR public bool DownloadFile(string path, string fileName) { try { ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + _Host + path)); Options(ref ftpRequest); ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; FileStream downloadedFile = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite); ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); Stream responseStream = ftpResponse.GetResponseStream(); byte[] buffer = new byte[bufferSize]; int size = 0; while ((size = responseStream.Read(buffer, 0, bufferSize)) > 0) { downloadedFile.Write(buffer, 0, size); } downloadedFile.Close(); responseStream.Close(); _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpResponse.Close(); ftpRequest = null; return true; } catch (Exception ex) { this.last_error = ex.ToString(); return false; } } // FTP STOR public bool UploadFile(string path, string filePath) { return Upload(path, filePath, false); } // FTP STOU public bool UploadFileWithUniqueName(string path, string filePath) { return Upload(path, filePath, true); } private bool Upload(string path, string filePath, bool uniqueName) { try { if (!path.EndsWith("/")) path += "/"; if (uniqueName) { ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + _Host + path)); ftpRequest.Method = WebRequestMethods.Ftp.UploadFileWithUniqueName; } else { string onlyName = Path.GetFileName(filePath); ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + _Host + path + onlyName)); ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; } Options(ref ftpRequest); byte[] buffer = new byte[bufferSize]; int count = 0; int readBytes = 0; FileStream stream = File.OpenRead(filePath); Stream requestStream = ftpRequest.GetRequestStream(); do { readBytes = stream.Read(buffer, 0, bufferSize); requestStream.Write(buffer, 0, bufferSize); count += readBytes; } while (readBytes != 0); requestStream.Close(); using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; return true; } } catch (Exception ex) { this.last_error = ex.ToString(); return false; } } // FTP DELE public bool DeleteFile(string path) { try { ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + _Host + path)); Options(ref ftpRequest); ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile; using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; return true; } } catch (Exception ex) { this.last_error = ex.ToString(); return false; } } // FTP MKD public bool MakeDirectory(string path, string newname) { try { if (!path.EndsWith("/")) path += "/"; FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + _Host + path + newname)); Options(ref ftpRequest); ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory; using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; return true; } } catch (Exception ex) { this.last_error = ex.ToString(); return false; } } // FTP RMD public bool RemoveDirectory(string path) { try { FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + _Host + path)); Options(ref ftpRequest); ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory; using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; return true; } } catch (Exception ex) { this.last_error = ex.ToString(); return false; } } // FTP PWD public string PrintWorkingDirectory() { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + _Host)); Options(ref ftpRequest); ftpRequest.Method = WebRequestMethods.Ftp.PrintWorkingDirectory; using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; System.Text.RegularExpressions.Regex regexp = new System.Text.RegularExpressions.Regex("\\s\"([^\"]*)\"\\s"); return regexp.Match(_StatusDescription).Groups[1].Value; } } // FTP SIZE public long GetFileSize(string path) { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + _Host + path)); Options(ref ftpRequest); ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize; using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; return ftpResponse.ContentLength; } } // FTP MDTM public DateTime GetDateTimestamp(string path) { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + _Host + path)); Options(ref ftpRequest); ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp; using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; return ftpResponse.LastModified; } } // FTP RENAME public bool Rename(string path, string newname) { try { ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + _Host + path)); Options(ref ftpRequest); ftpRequest.Method = WebRequestMethods.Ftp.Rename; ftpRequest.RenameTo = newname; using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; return true; } } catch (Exception ex) { this.last_error = ex.ToString(); return false; } } // FTP APPE public bool AppendFile(string path, string filePath) { try { ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + _Host + path)); ftpRequest.Method = WebRequestMethods.Ftp.AppendFile; Options(ref ftpRequest); FileStream appendedFile = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte [] fileContents = new byte[appendedFile.Length]; appendedFile.Read(fileContents, 0, fileContents.Length); appendedFile.Close(); ftpRequest.ContentLength = fileContents.Length; Stream requestStream = ftpRequest.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); using (ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { _StatusCode = ftpResponse.StatusCode; _StatusDescription = ftpResponse.StatusDescription; ftpRequest = null; return true; } } catch (Exception ex) { this.last_error = ex.ToString(); return false; } } } }