Created
May 14, 2013 19:22
-
-
Save yemrekeskin/5578696 to your computer and use it in GitHub Desktop.
Ftp Client - for FTP Server Connections (creating,transfer for cases)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface IFtpConnect | |
| { | |
| string[] GetFiles(); | |
| long GetFileSize(); | |
| void DownloadFile(string path); | |
| bool DeleteFile(); | |
| void UploadFile(string path); | |
| } | |
| public static class Bootstrapper | |
| { | |
| public static void Preload() | |
| { | |
| FileStream fs = new FileStream("C:\\FTPConnectiontLog.txt", FileMode.OpenOrCreate); | |
| TextWriterTraceListener textListener = new TextWriterTraceListener(fs); | |
| Trace.Listeners.Add(textListener); | |
| } | |
| } | |
| public class FtpException | |
| : Exception | |
| { | |
| public FtpException() { } | |
| public FtpException(string faultmsg) { } | |
| } | |
| public struct FtpCredential | |
| { | |
| public string RequestUrl { get; set; } | |
| public string UserName { get; set; } | |
| public string Password { get; set; } | |
| } | |
| // Actual Class - FtpClient | |
| public class FtpClient | |
| :IDisposable,IFtpConnect | |
| { | |
| FtpWebRequest _ftpRequest; | |
| FtpCredential _ftpCredential; | |
| public FtpClient(FtpCredential ftpCredential) | |
| { | |
| Bootstrapper.Preload(); //for initial settings | |
| this._ftpCredential = ftpCredential; | |
| this.Connect(); // To connect automatically | |
| } | |
| public FtpClient(string ftpRequestUrl,string ftpUserName,string ftpPassword) | |
| { | |
| Bootstrapper.Preload(); // for initial settings | |
| this._ftpCredential.RequestUrl = ftpRequestUrl; | |
| this._ftpCredential.UserName = ftpUserName; | |
| this._ftpCredential.Password = ftpPassword; | |
| this.Connect(); // To connect automatically | |
| } | |
| private void Connect() | |
| { | |
| this._ftpRequest = (FtpWebRequest)FtpWebRequest.Create(this._ftpCredential.RequestUrl); | |
| this._ftpRequest.Credentials = new NetworkCredential(this._ftpCredential.UserName, this._ftpCredential.Password); | |
| this._ftpRequest.KeepAlive = false; | |
| this._ftpRequest.UseBinary = true; | |
| this._ftpRequest.UsePassive = true; | |
| } | |
| public string[] GetFiles() | |
| { | |
| List<string> files = new List<string>(); | |
| _ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; | |
| string[] items; | |
| using (FtpWebResponse response = (FtpWebResponse)_ftpRequest.GetResponse()) | |
| { | |
| TextReader st = new StreamReader(response.GetResponseStream()); | |
| string responseText = st.ReadToEnd(); | |
| response.Close(); | |
| items = responseText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); | |
| } | |
| foreach (string item in items) | |
| { | |
| files.Add(item); | |
| } | |
| return (string[])files.ToArray(); | |
| } | |
| public long GetFileSize() | |
| { | |
| _ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize; | |
| try | |
| { | |
| FtpWebResponse response = (FtpWebResponse)_ftpRequest.GetResponse(); | |
| long length = response.ContentLength; | |
| response.Close(); | |
| return length; | |
| } | |
| catch (FtpException ex) | |
| { | |
| Trace.Write(DateTime.Now.ToString() + "" + ex.Message); | |
| } | |
| return default(long); | |
| } | |
| public void DownloadFile(string path) | |
| { | |
| _ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; | |
| try | |
| { | |
| FtpWebResponse response = (FtpWebResponse)_ftpRequest.GetResponse(); | |
| using (Stream sr = (Stream)response.GetResponseStream()) | |
| { | |
| MemoryStream memStream = new MemoryStream(); | |
| byte[] buffer = new byte[1024]; //downloads in chuncks | |
| while (true) | |
| { | |
| //Try to read the data | |
| int bytesRead = sr.Read(buffer, 0, buffer.Length); | |
| if (bytesRead == 0) | |
| { | |
| break; | |
| } | |
| else | |
| { | |
| //Write the downloaded data | |
| memStream.Write(buffer, 0, bytesRead); | |
| } | |
| } | |
| using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) | |
| { | |
| fs.Write(memStream.ToArray(), 0, (int)memStream.Length); | |
| } | |
| } | |
| response.Close(); | |
| } | |
| catch (FtpException ex) | |
| { | |
| Trace.Write(DateTime.Now.ToString() + "" + ex.Message); | |
| } | |
| } | |
| public bool DeleteFile() | |
| { | |
| _ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile; | |
| try | |
| { | |
| FtpWebResponse response = (FtpWebResponse)_ftpRequest.GetResponse(); | |
| bool status = (response.StatusCode == FtpStatusCode.FileActionOK); | |
| response.Close(); | |
| return status; | |
| } | |
| catch (FtpException ex) | |
| { | |
| Trace.Write(DateTime.Now.ToString() + "" + ex.Message); | |
| } | |
| return false; | |
| } | |
| public void UploadFile(string path) | |
| { | |
| _ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; | |
| try | |
| { | |
| using (FileStream sr = File.OpenRead(path)) | |
| using (Stream st = _ftpRequest.GetRequestStream()) | |
| { | |
| MemoryStream memStream = new MemoryStream(); | |
| memStream.SetLength(sr.Length); | |
| sr.Read(memStream.GetBuffer(), 0, (int)memStream.Length); | |
| st.Write(memStream.ToArray(), 0, (int)memStream.Length); | |
| st.Flush(); | |
| } | |
| FtpWebResponse response = (FtpWebResponse)_ftpRequest.GetResponse(); | |
| response.Close(); | |
| } | |
| catch (FtpException ex) | |
| { | |
| Trace.Write(DateTime.Now.ToString() + "" + ex.Message); | |
| } | |
| } | |
| public void Dispose() { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment