Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6.  
  7. namespace TE.Network
  8. {
  9.     public class FtpClient
  10.     {
  11.         private FtpWebRequest CurrentFtpRequest;
  12.  
  13.         private string _Username;
  14.         public string Username
  15.         {
  16.             get { return _Username; }
  17.             set { _Username = value; }
  18.         }
  19.  
  20.         private string _Password;
  21.         public string Password
  22.         {
  23.             get { return _Password; }
  24.             set { _Password = value; }
  25.         }
  26.  
  27.         private string _IpAddress;
  28.         public string IpAddress
  29.         {
  30.             get { return _IpAddress; }
  31.             set { _IpAddress = value; }
  32.         }
  33.  
  34.         public FtpClient()
  35.         {      
  36.         }
  37.  
  38.         public FtpClient(string ipAddress, string username, string password)
  39.         {
  40.             this.Open(ipAddress, username, password);
  41.         }
  42.  
  43.         public void Open(string ipAddress, string username, string password)
  44.         {
  45.             if (!ipAddress.StartsWith(@"ftp://", StringComparison.CurrentCultureIgnoreCase))
  46.             {
  47.                 ipAddress = @"ftp://" + ipAddress;
  48.             }
  49.            
  50.             this.IpAddress = ipAddress;
  51.             this.Username = username;
  52.             this.Password = password;
  53.  
  54.             this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(this.IpAddress));
  55.             this.CurrentFtpRequest.KeepAlive = true;
  56.             this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
  57.         }
  58.  
  59.         public void Close()
  60.         {
  61.             //take out by tps 03/29/2010, error: This operation cannot be performed after the request has been submitted
  62.             //this.CurrentFtpRequest.KeepAlive = false;
  63.  
  64.             this.CurrentFtpRequest = null;
  65.         }
  66.  
  67.         public List<string> GetDirectoryListing(string directory)
  68.         {
  69.             string uri = this.IpAddress;
  70.             if(!String.IsNullOrEmpty(directory))
  71.             {
  72.                 if (!uri.EndsWith("/")) uri = uri + "/";
  73.                 uri = uri + directory;
  74.             }          
  75.  
  76.             this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  77.             this.CurrentFtpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
  78.             this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
  79.             FtpWebResponse ftpResponse = (FtpWebResponse)this.CurrentFtpRequest.GetResponse();
  80.             StreamReader sr = new StreamReader(ftpResponse.GetResponseStream(), System.Text.Encoding.ASCII);
  81.             string directoryListing = sr.ReadToEnd();
  82.             sr.Close();
  83.             ftpResponse.Close();
  84.  
  85.             List<string> files = new List<string>();
  86.             string[] splitString = new string[] {Environment.NewLine};
  87.             foreach (string file in directoryListing.Split(splitString, StringSplitOptions.RemoveEmptyEntries))
  88.             {
  89.                 files.Add(file);
  90.             }
  91.             return files;          
  92.         }
  93.  
  94.         public DateTime GetDateTimeStamp(string remotePath)
  95.         {
  96.             string uri = this.IpAddress;
  97.             if (!String.IsNullOrEmpty(remotePath))
  98.             {
  99.                 uri = uri + @"/" + remotePath;
  100.             }
  101.  
  102.             this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  103.             this.CurrentFtpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
  104.             this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
  105.             FtpWebResponse ftpResponse = (FtpWebResponse)this.CurrentFtpRequest.GetResponse();
  106.             return ftpResponse.LastModified;
  107.  
  108.         }
  109.  
  110.         public void DownloadFile(string remotePath, string localPath, bool useBinary)
  111.         {
  112.             string uri = this.IpAddress;
  113.             if (!String.IsNullOrEmpty(remotePath))
  114.             {
  115.                 uri = uri + @"/" + remotePath;
  116.             }
  117.  
  118.             this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  119.             this.CurrentFtpRequest.UseBinary = useBinary;          
  120.             this.CurrentFtpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  121.             this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
  122.             FtpWebResponse ftpResponse = (FtpWebResponse)this.CurrentFtpRequest.GetResponse();
  123.  
  124.             Stream responseStream = ftpResponse.GetResponseStream();
  125.             BinaryReader reader = new BinaryReader(responseStream);
  126.  
  127.             FileStream outputStream = new FileStream(localPath, FileMode.Create);      
  128.  
  129.             long cl = ftpResponse.ContentLength;
  130.             int bufferSize = 2048;
  131.             int readCount;
  132.             byte[] buffer = new byte[bufferSize];
  133.  
  134.             readCount = responseStream.Read(buffer, 0, bufferSize);
  135.             while (readCount > 0)
  136.             {
  137.                 outputStream.Write(buffer, 0, readCount);
  138.                 readCount = responseStream.Read(buffer, 0, bufferSize);
  139.             }
  140.  
  141.             outputStream.Close();
  142.             reader.Close();
  143.             ftpResponse.Close();  
  144.         }
  145.  
  146.         public void UploadFile(string remotePath, string localPath, bool useBinary)
  147.         {
  148.             string uri = this.IpAddress;
  149.             if (!String.IsNullOrEmpty(remotePath))
  150.             {
  151.                 uri = uri + @"/" + remotePath;
  152.             }
  153.  
  154.             this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  155.             this.CurrentFtpRequest.UseBinary = useBinary;
  156.             this.CurrentFtpRequest.Method = WebRequestMethods.Ftp.UploadFile;
  157.             this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
  158.  
  159.             Stream ftpStream = this.CurrentFtpRequest.GetRequestStream();
  160.  
  161.             //BinaryReader reader = new BinaryReader(new FileStream(localPath, FileMode.Create));
  162.             //FileInfo localFile = new FileInfo(localPath);
  163.             //this.CurrentFtpRequest.ContentLength = localFile.Length;
  164.  
  165.             FileStream localFile = File.OpenRead(localPath);
  166.             //StreamReader sourceStream = new StreamReader(localPath);
  167.             //byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
  168.             //sourceStream.Close();
  169.        
  170.             int len = 1024;
  171.             byte[] buff = new byte[len];
  172.             int bytesread = 0;
  173.  
  174.             do
  175.             {
  176.                 bytesread = localFile.Read(buff, 0, len);
  177.                 ftpStream.Write(buff, 0, bytesread);
  178.             }
  179.             while (bytesread != 0);
  180.             localFile.Close();
  181.             ftpStream.Close();
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement