Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.IO;
- namespace TE.Network
- {
- public class FtpClient
- {
- private FtpWebRequest CurrentFtpRequest;
- private string _Username;
- public string Username
- {
- get { return _Username; }
- set { _Username = value; }
- }
- private string _Password;
- public string Password
- {
- get { return _Password; }
- set { _Password = value; }
- }
- private string _IpAddress;
- public string IpAddress
- {
- get { return _IpAddress; }
- set { _IpAddress = value; }
- }
- public FtpClient()
- {
- }
- public FtpClient(string ipAddress, string username, string password)
- {
- this.Open(ipAddress, username, password);
- }
- public void Open(string ipAddress, string username, string password)
- {
- if (!ipAddress.StartsWith(@"ftp://", StringComparison.CurrentCultureIgnoreCase))
- {
- ipAddress = @"ftp://" + ipAddress;
- }
- this.IpAddress = ipAddress;
- this.Username = username;
- this.Password = password;
- this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(this.IpAddress));
- this.CurrentFtpRequest.KeepAlive = true;
- this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
- }
- public void Close()
- {
- //take out by tps 03/29/2010, error: This operation cannot be performed after the request has been submitted
- //this.CurrentFtpRequest.KeepAlive = false;
- this.CurrentFtpRequest = null;
- }
- public List<string> GetDirectoryListing(string directory)
- {
- string uri = this.IpAddress;
- if(!String.IsNullOrEmpty(directory))
- {
- if (!uri.EndsWith("/")) uri = uri + "/";
- uri = uri + directory;
- }
- this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
- this.CurrentFtpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
- this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
- FtpWebResponse ftpResponse = (FtpWebResponse)this.CurrentFtpRequest.GetResponse();
- StreamReader sr = new StreamReader(ftpResponse.GetResponseStream(), System.Text.Encoding.ASCII);
- string directoryListing = sr.ReadToEnd();
- sr.Close();
- ftpResponse.Close();
- List<string> files = new List<string>();
- string[] splitString = new string[] {Environment.NewLine};
- foreach (string file in directoryListing.Split(splitString, StringSplitOptions.RemoveEmptyEntries))
- {
- files.Add(file);
- }
- return files;
- }
- public DateTime GetDateTimeStamp(string remotePath)
- {
- string uri = this.IpAddress;
- if (!String.IsNullOrEmpty(remotePath))
- {
- uri = uri + @"/" + remotePath;
- }
- this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
- this.CurrentFtpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
- this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
- FtpWebResponse ftpResponse = (FtpWebResponse)this.CurrentFtpRequest.GetResponse();
- return ftpResponse.LastModified;
- }
- public void DownloadFile(string remotePath, string localPath, bool useBinary)
- {
- string uri = this.IpAddress;
- if (!String.IsNullOrEmpty(remotePath))
- {
- uri = uri + @"/" + remotePath;
- }
- this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
- this.CurrentFtpRequest.UseBinary = useBinary;
- this.CurrentFtpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
- this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
- FtpWebResponse ftpResponse = (FtpWebResponse)this.CurrentFtpRequest.GetResponse();
- Stream responseStream = ftpResponse.GetResponseStream();
- BinaryReader reader = new BinaryReader(responseStream);
- FileStream outputStream = new FileStream(localPath, FileMode.Create);
- long cl = ftpResponse.ContentLength;
- int bufferSize = 2048;
- int readCount;
- byte[] buffer = new byte[bufferSize];
- readCount = responseStream.Read(buffer, 0, bufferSize);
- while (readCount > 0)
- {
- outputStream.Write(buffer, 0, readCount);
- readCount = responseStream.Read(buffer, 0, bufferSize);
- }
- outputStream.Close();
- reader.Close();
- ftpResponse.Close();
- }
- public void UploadFile(string remotePath, string localPath, bool useBinary)
- {
- string uri = this.IpAddress;
- if (!String.IsNullOrEmpty(remotePath))
- {
- uri = uri + @"/" + remotePath;
- }
- this.CurrentFtpRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
- this.CurrentFtpRequest.UseBinary = useBinary;
- this.CurrentFtpRequest.Method = WebRequestMethods.Ftp.UploadFile;
- this.CurrentFtpRequest.Credentials = new NetworkCredential(this.Username, this.Password);
- Stream ftpStream = this.CurrentFtpRequest.GetRequestStream();
- //BinaryReader reader = new BinaryReader(new FileStream(localPath, FileMode.Create));
- //FileInfo localFile = new FileInfo(localPath);
- //this.CurrentFtpRequest.ContentLength = localFile.Length;
- FileStream localFile = File.OpenRead(localPath);
- //StreamReader sourceStream = new StreamReader(localPath);
- //byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
- //sourceStream.Close();
- int len = 1024;
- byte[] buff = new byte[len];
- int bytesread = 0;
- do
- {
- bytesread = localFile.Read(buff, 0, len);
- ftpStream.Write(buff, 0, bytesread);
- }
- while (bytesread != 0);
- localFile.Close();
- ftpStream.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement