Initial commit
This commit is contained in:
31
Handler/Project/Class/FTP/EventArgs.cs
Normal file
31
Handler/Project/Class/FTP/EventArgs.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace AR.FTPClient
|
||||
{
|
||||
public class MessageEventArgs : EventArgs
|
||||
{
|
||||
protected Boolean _isError = false;
|
||||
public Boolean IsError { get { return _isError; } }
|
||||
protected string _message = string.Empty;
|
||||
public string Message { get { return _message; } }
|
||||
public MessageEventArgs(Boolean isError, string Message)
|
||||
{
|
||||
_isError = isError;
|
||||
_message = Message;
|
||||
}
|
||||
}
|
||||
public class ListProgressEventArgs : EventArgs
|
||||
{
|
||||
//public long Max { get; set; }
|
||||
//public long Value { get; set; }
|
||||
public long TotalRecv { get; set; }
|
||||
public ListProgressEventArgs(long TotalRecv_)
|
||||
{
|
||||
this.TotalRecv = TotalRecv_;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
574
Handler/Project/Class/FTP/FTPClient.cs
Normal file
574
Handler/Project/Class/FTP/FTPClient.cs
Normal file
@@ -0,0 +1,574 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace AR.FTPClient
|
||||
{
|
||||
public partial class FTPClient : IFTPClient
|
||||
{
|
||||
public string errorMessage { get; set; }
|
||||
|
||||
private int bufferSize = 2048;
|
||||
private int timeout = 20000;
|
||||
|
||||
#region "Contstruct"
|
||||
|
||||
public FTPClient() : this("127.0.0.1", "Anonymous", "") { }
|
||||
|
||||
public FTPClient(string Host, string UserID, string UserPass) :
|
||||
this(Host, UserID, UserPass, 21)
|
||||
{ }
|
||||
|
||||
public FTPClient(string Host, string UserID, string UserPass, bool passive) :
|
||||
this(Host, UserID, UserPass, 21, passive)
|
||||
{ }
|
||||
|
||||
public FTPClient(string Host, string UserID, string UserPass, int port) :
|
||||
this(Host, UserID, UserPass, port, false)
|
||||
{ }
|
||||
|
||||
public FTPClient(string host, string userid, string userpass, int port, bool passive)
|
||||
{
|
||||
Host = host;
|
||||
UserID = userid;
|
||||
UserPassword = userpass;
|
||||
Port = port;
|
||||
PassiveMode = passive;
|
||||
this.TextEncoding = System.Text.Encoding.UTF8;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public event EventHandler<MessageEventArgs> Message;
|
||||
public event EventHandler<ListProgressEventArgs> ListPrgress;
|
||||
|
||||
#region "Properties"
|
||||
|
||||
/// <summary>
|
||||
/// 접속하려는 FTP서버의 IP혹은 도메인 주소를 입력하세요
|
||||
/// 기본값 : 127.0.0.1
|
||||
/// </summary>
|
||||
public string Host { get; set; }
|
||||
/// <summary>
|
||||
/// FTP의 사용자 ID
|
||||
/// 기본값 : Anonymous
|
||||
/// </summary>
|
||||
public string UserID { get; set; }
|
||||
/// <summary>
|
||||
/// FTP 사용자 ID의 암호
|
||||
/// 기본값 : 없음
|
||||
/// </summary>
|
||||
public string UserPassword { get; set; }
|
||||
/// <summary>
|
||||
/// FTP 접속 포트
|
||||
/// 기본값 : 21
|
||||
/// </summary>
|
||||
public int Port { get; set; }
|
||||
/// <summary>
|
||||
/// FTP 접속 방식 (능동형/수동형)
|
||||
/// </summary>
|
||||
public bool PassiveMode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 문자수신시 사용할 인코딩 방식
|
||||
/// </summary>
|
||||
public System.Text.Encoding TextEncoding { get; set; }
|
||||
|
||||
#endregion
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 파일을 다운로드 합니다.
|
||||
/// </summary>
|
||||
/// <param name="remoteFile">원격위치의 전체 경로</param>
|
||||
/// <param name="localFile">로컬위치의 전체 경로</param>
|
||||
public Boolean Download(string remoteFile, string localFile, bool overwrite = false)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
|
||||
//overwrite funcion - 190622 - chi
|
||||
errorMessage = string.Empty;
|
||||
if (overwrite == false && System.IO.File.Exists(localFile))
|
||||
{
|
||||
errorMessage = "Target file alreay exists";
|
||||
return false;
|
||||
}
|
||||
else if (overwrite == true && System.IO.File.Exists(localFile))
|
||||
{
|
||||
try
|
||||
{
|
||||
System.IO.File.Delete(localFile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errorMessage = ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool retval = true;
|
||||
try
|
||||
{
|
||||
long Receive = 0;
|
||||
var url = CombineUrl(remoteFile);
|
||||
|
||||
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(this.UserID, this.UserPassword);
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.UsePassive = this.PassiveMode;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpStream = ftpResponse.GetResponseStream();
|
||||
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
|
||||
byte[] byteBuffer = new byte[bufferSize];
|
||||
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
|
||||
Receive += bytesRead;
|
||||
if (ListPrgress != null)
|
||||
ListPrgress(this, new ListProgressEventArgs(Receive));
|
||||
try
|
||||
{
|
||||
while (bytesRead > 0)
|
||||
{
|
||||
localFileStream.Write(byteBuffer, 0, bytesRead);
|
||||
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
|
||||
Receive += bytesRead;
|
||||
if (ListPrgress != null)
|
||||
ListPrgress(this, new ListProgressEventArgs(Receive));
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { retval = false; Console.WriteLine(ex.ToString()); }
|
||||
localFileStream.Close();
|
||||
ftpStream.Close();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
}
|
||||
catch (Exception ex) { retval = false; this.errorMessage = ex.Message; Console.WriteLine(ex.ToString()); }
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 파일을 업로드 합니다.
|
||||
/// </summary>
|
||||
/// <param name="remoteFile">원격위치의 전체 경로</param>
|
||||
/// <param name="localFile">로컬위치의 전체 경로</param>
|
||||
/// <returns></returns>
|
||||
public Boolean Upload(string remoteFile, string localFile)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(remoteFile);
|
||||
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.UsePassive = PassiveMode;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
|
||||
ftpStream = ftpRequest.GetRequestStream();
|
||||
FileStream localFileStream = new FileStream(localFile, FileMode.Open);
|
||||
byte[] byteBuffer = new byte[bufferSize];
|
||||
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
|
||||
Boolean bError = false;
|
||||
try
|
||||
{
|
||||
System.Diagnostics.Stopwatch wat = new System.Diagnostics.Stopwatch();
|
||||
wat.Restart();
|
||||
while (bytesSent != 0)
|
||||
{
|
||||
ftpStream.Write(byteBuffer, 0, bytesSent);
|
||||
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
bError = true;
|
||||
}
|
||||
localFileStream.Close();
|
||||
ftpStream.Close();
|
||||
ftpRequest = null;
|
||||
if (bError) return false;
|
||||
else return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.errorMessage = ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 원격파일을 삭제 합니다.
|
||||
/// </summary>
|
||||
/// <param name="remotefile"></param>
|
||||
public Boolean Delete(string remotefile)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(remotefile);
|
||||
ftpRequest = (FtpWebRequest)WebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.UsePassive = this.PassiveMode;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) { this.errorMessage = ex.Message; return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 원격파일의 이름을 변경 합니다.
|
||||
/// </summary>
|
||||
/// <param name="currentFileNameAndPath"></param>
|
||||
/// <param name="newFileName"></param>
|
||||
public Boolean rename(string currentFileNameAndPath, string newFileName)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(currentFileNameAndPath);
|
||||
ftpRequest = (FtpWebRequest)WebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UsePassive = this.PassiveMode;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.Rename;
|
||||
ftpRequest.RenameTo = newFileName;
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) { this.errorMessage = ex.Message; return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 원격위치에 폴더를 생성 합니다.
|
||||
/// 트리구조로 폴더를 생성하지 않습니다. 여러개의 폴더를 생성하려면 각각 호출 해야 합니다.
|
||||
/// </summary>
|
||||
/// <param name="newDirectory"></param>
|
||||
/// <returns></returns>
|
||||
public bool createDirectory(string newDirectory)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(newDirectory, false);
|
||||
ftpRequest = (FtpWebRequest)WebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UsePassive = this.PassiveMode;
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) { this.errorMessage = ex.Message; return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 원격위치의 폴더를 삭제합니다. 폴더 삭제 전 대상 폴더는 비어있어야 합니다.
|
||||
/// </summary>
|
||||
/// <param name="Directory"></param>
|
||||
/// <returns></returns>
|
||||
public bool RemoveDirectory(string Directory)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(Directory, false);
|
||||
ftpRequest = (FtpWebRequest)WebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UsePassive = this.PassiveMode;
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) { this.errorMessage = ex.Message; return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 파일의 생성일자 반환
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public string getFileCreatedDateTime(string fileName)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(fileName);
|
||||
ftpRequest = (FtpWebRequest)WebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UsePassive = this.PassiveMode;
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpStream = ftpResponse.GetResponseStream();
|
||||
StreamReader ftpReader = new StreamReader(ftpStream, this.TextEncoding);
|
||||
string fileInfo = null;
|
||||
try { fileInfo = ftpReader.ReadToEnd(); }
|
||||
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
|
||||
ftpReader.Close();
|
||||
ftpStream.Close();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
return fileInfo;
|
||||
}
|
||||
catch (Exception ex) { this.errorMessage = ex.Message; Console.WriteLine(ex.ToString()); }
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 파일의 크기를 반환
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public string getFileSize(string fileName)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(fileName);
|
||||
ftpRequest = (FtpWebRequest)WebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.UsePassive = this.PassiveMode;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpStream = ftpResponse.GetResponseStream();
|
||||
StreamReader ftpReader = new StreamReader(ftpStream, this.TextEncoding);
|
||||
string fileInfo = null;
|
||||
try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } }
|
||||
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
|
||||
ftpReader.Close();
|
||||
ftpStream.Close();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
return fileInfo;
|
||||
}
|
||||
catch (Exception ex) { errorMessage = ex.Message; Console.WriteLine(ex.ToString()); }
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 폴더와 파일의 이름만 반환 합니다.
|
||||
/// </summary>
|
||||
/// <param name="directory"></param>
|
||||
/// <returns></returns>
|
||||
public string[] directoryListSimple(string directory)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
errorMessage = string.Empty;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(directory, false);
|
||||
if (url.EndsWith("/") == false) url += "/";
|
||||
ftpRequest = (FtpWebRequest)WebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.UsePassive = PassiveMode;
|
||||
ftpRequest.KeepAlive = true;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.ReadWriteTimeout = 30000;
|
||||
ftpRequest.Timeout = 30000;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
|
||||
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpStream = ftpResponse.GetResponseStream();
|
||||
StreamReader ftpReader = new StreamReader(ftpStream, this.TextEncoding);
|
||||
string directoryRaw = null;
|
||||
try
|
||||
{
|
||||
while (ftpReader.Peek() != -1)
|
||||
{
|
||||
var dataLIne = ftpReader.ReadLine();
|
||||
if (dataLIne.Trim() != "")
|
||||
{
|
||||
if (directoryRaw != null && directoryRaw != "") directoryRaw += "|";
|
||||
directoryRaw += dataLIne;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { errorMessage += "\n" + ex.Message; }
|
||||
ftpReader.Close();
|
||||
ftpStream.Close();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
|
||||
catch (Exception ex) { errorMessage += "\n" + ex.Message; }
|
||||
}
|
||||
catch (Exception ex) { errorMessage = ex.Message; Console.WriteLine(ex.ToString()); }
|
||||
return new string[] { "" };
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 폴더 및 파일의 정보를 상세하게 표시합니다.
|
||||
/// </summary>
|
||||
/// <param name="directory"></param>
|
||||
/// <returns></returns>
|
||||
public FTPdirectory ListDirectoryDetail(string directory)
|
||||
{
|
||||
FtpWebRequest ftpRequest = null;
|
||||
FtpWebResponse ftpResponse = null;
|
||||
Stream ftpStream = null;
|
||||
errorMessage = string.Empty;
|
||||
try
|
||||
{
|
||||
var url = CombineUrl(directory, false);
|
||||
|
||||
if (url.EndsWith("/") == false) url += "/";
|
||||
ftpRequest = (FtpWebRequest)WebRequest.Create(url);
|
||||
ftpRequest.Credentials = new NetworkCredential(UserID, UserPassword);
|
||||
ftpRequest.UsePassive = true;
|
||||
ftpRequest.UseBinary = true;
|
||||
ftpRequest.KeepAlive = false;
|
||||
ftpRequest.ReadWriteTimeout = this.timeout;
|
||||
ftpRequest.ReadWriteTimeout = 30000;
|
||||
ftpRequest.Timeout = 30000;
|
||||
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
|
||||
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
||||
ftpStream = ftpResponse.GetResponseStream();
|
||||
|
||||
StreamReader ftpReader = new StreamReader(ftpStream, this.TextEncoding);
|
||||
string directoryRaw = null;
|
||||
|
||||
try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "\r"; } }
|
||||
catch (Exception ex) { errorMessage += "\n" + ex.Message; }
|
||||
ftpReader.Close();
|
||||
ftpStream.Close();
|
||||
ftpResponse.Close();
|
||||
ftpRequest = null;
|
||||
directoryRaw = directoryRaw.Replace("\r\n", "\r").TrimEnd((char)0x0D);
|
||||
return new FTPdirectory(directoryRaw, directory);
|
||||
}
|
||||
catch (Exception ex) { errorMessage += "\n" + ex.Message; }
|
||||
return null;
|
||||
}
|
||||
|
||||
#region "utillity"
|
||||
|
||||
/// <summary>
|
||||
/// Url을 Host와 결합하여 생성
|
||||
/// </summary>
|
||||
/// <param name="file">경로명</param>
|
||||
/// <param name="isfile">파일인지 여부</param>
|
||||
/// <returns></returns>
|
||||
string CombineUrl(string file, bool isfile = true)
|
||||
{
|
||||
file = file.Replace("\\", "/");
|
||||
string url = this.Host;
|
||||
if (this.Host.ToLower().StartsWith("ftp://") == false) url = "ftp://" + url;
|
||||
if (url.Substring(5).LastIndexOf(':') == -1)
|
||||
url += ":" + this.Port.ToString();
|
||||
|
||||
if (this.Host.EndsWith("/")) url = this.Host.Substring(0, Host.Length - 1);
|
||||
|
||||
if (file.StartsWith("/"))
|
||||
url = url + System.Web.HttpUtility.UrlPathEncode(file);
|
||||
else
|
||||
url = url + "/" + System.Web.HttpUtility.UrlPathEncode(file);
|
||||
|
||||
url = url.Replace("#", "%23");
|
||||
|
||||
if (isfile)
|
||||
return url;
|
||||
else
|
||||
return url + "/";
|
||||
}
|
||||
public string PathCombine(string path, string add)
|
||||
{
|
||||
var newpath = string.Empty;
|
||||
if (path.EndsWith("/")) newpath = path + add;
|
||||
else newpath = path + "/" + add;
|
||||
if (!newpath.EndsWith("/")) newpath += '/';
|
||||
return newpath;
|
||||
|
||||
}
|
||||
public string PathFileCombine(string path, string add)
|
||||
{
|
||||
var newpath = string.Empty;
|
||||
if (path.EndsWith("/")) newpath = path + add;
|
||||
else newpath = path + "/" + add;
|
||||
if (newpath.EndsWith("/")) newpath = newpath.Substring(0, newpath.Length - 1);
|
||||
return newpath;
|
||||
|
||||
}
|
||||
public string getParent(string path)
|
||||
{
|
||||
if (path == "/") return path;
|
||||
else if (path == "") return "/";
|
||||
else
|
||||
{
|
||||
//서브폴더를 찾아서 처리해준다.
|
||||
if (path.IndexOf('/') == -1) return "/";
|
||||
else
|
||||
{
|
||||
if (path.EndsWith("/")) path = path.Substring(0, path.Length - 1);
|
||||
var slashindex = path.LastIndexOf('/');
|
||||
var parent = path.Substring(0, slashindex);
|
||||
if (!parent.EndsWith("/")) parent += '/';
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RaiseMessage(Boolean isError, string message)
|
||||
{
|
||||
if (isError) errorMessage = message; //170920
|
||||
if (Message != null) Message(this, new MessageEventArgs(isError, message));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
95
Handler/Project/Class/FTP/FTPdirectory.cs
Normal file
95
Handler/Project/Class/FTP/FTPdirectory.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace AR.FTPClient
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// ''' Stores a list of files and directories from an FTP result
|
||||
/// ''' </summary>
|
||||
/// ''' <remarks></remarks>
|
||||
public class FTPdirectory : List<FTPfileInfo>
|
||||
{
|
||||
public FTPdirectory()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ''' Constructor: create list from a (detailed) directory string
|
||||
/// ''' </summary>
|
||||
/// ''' <param name="dir">directory listing string</param>
|
||||
/// ''' <param name="path"></param>
|
||||
/// ''' <remarks></remarks>
|
||||
public FTPdirectory(string dir, string path)
|
||||
{
|
||||
var lines = dir.Replace("\n","").Split('\r');
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (line != "")
|
||||
this.Add(new FTPfileInfo(line, path));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// ''' Filter out only files from directory listing
|
||||
/// ''' </summary>
|
||||
/// ''' <param name="ext">optional file extension filter</param>
|
||||
/// ''' <returns>FTPdirectory listing</returns>
|
||||
public FTPdirectory GetFiles(string ext = "")
|
||||
{
|
||||
return this.GetFileOrDir(FTPfileInfo.DirectoryEntryTypes.File, ext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ''' Returns a list of only subdirectories
|
||||
/// ''' </summary>
|
||||
/// ''' <returns>FTPDirectory list</returns>
|
||||
/// ''' <remarks></remarks>
|
||||
public FTPdirectory GetDirectories()
|
||||
{
|
||||
return this.GetFileOrDir(FTPfileInfo.DirectoryEntryTypes.Directory);
|
||||
}
|
||||
|
||||
// internal: share use function for GetDirectories/Files
|
||||
private FTPdirectory GetFileOrDir(FTPfileInfo.DirectoryEntryTypes type, string ext = "")
|
||||
{
|
||||
FTPdirectory result = new FTPdirectory();
|
||||
foreach (FTPfileInfo fi in this)
|
||||
{
|
||||
if (fi.FileType == type)
|
||||
{
|
||||
if (ext == "")
|
||||
result.Add(fi);
|
||||
else if (ext == fi.Extension)
|
||||
result.Add(fi);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool FileExists(string filename)
|
||||
{
|
||||
foreach (FTPfileInfo ftpfile in this)
|
||||
{
|
||||
if (ftpfile.Filename == filename)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private const char slash = '/';
|
||||
|
||||
public static string GetParentDirectory(string dir)
|
||||
{
|
||||
string tmp = dir.TrimEnd(slash);
|
||||
int i = tmp.LastIndexOf(slash);
|
||||
if (i > 0)
|
||||
return tmp.Substring(0, i - 1);
|
||||
else
|
||||
throw new ApplicationException("No parent for root");
|
||||
}
|
||||
}
|
||||
}
|
||||
216
Handler/Project/Class/FTP/FTPfileInfo.cs
Normal file
216
Handler/Project/Class/FTP/FTPfileInfo.cs
Normal file
@@ -0,0 +1,216 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AR.FTPClient
|
||||
{
|
||||
/// <summary>
|
||||
/// ''' Represents a file or directory entry from an FTP listing
|
||||
/// ''' </summary>
|
||||
/// ''' <remarks>
|
||||
/// ''' This class is used to parse the results from a detailed
|
||||
/// ''' directory list from FTP. It supports most formats of
|
||||
/// ''' </remarks>
|
||||
public class FTPfileInfo
|
||||
{
|
||||
// Stores extended info about FTP file
|
||||
|
||||
public string FullName
|
||||
{
|
||||
get
|
||||
{
|
||||
var retval = Path + "/" + Filename;
|
||||
return retval.Replace("//", "/");
|
||||
}
|
||||
}
|
||||
public string Filename
|
||||
{
|
||||
get
|
||||
{
|
||||
return _filename;
|
||||
}
|
||||
}
|
||||
public string Path
|
||||
{
|
||||
get
|
||||
{
|
||||
return _path;
|
||||
}
|
||||
}
|
||||
public DirectoryEntryTypes FileType
|
||||
{
|
||||
get
|
||||
{
|
||||
return _fileType;
|
||||
}
|
||||
}
|
||||
public long Size
|
||||
{
|
||||
get
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
}
|
||||
public DateTime FileDateTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return _fileDateTime;
|
||||
}
|
||||
}
|
||||
public string Permission
|
||||
{
|
||||
get
|
||||
{
|
||||
return _permission;
|
||||
}
|
||||
}
|
||||
public string Extension
|
||||
{
|
||||
get
|
||||
{
|
||||
int i = this.Filename.LastIndexOf(".");
|
||||
if (i >= 0 & i < (this.Filename.Length - 1))
|
||||
return this.Filename.Substring(i + 1);
|
||||
else
|
||||
return "";
|
||||
}
|
||||
}
|
||||
public string NameOnly
|
||||
{
|
||||
get
|
||||
{
|
||||
int i = this.Filename.LastIndexOf(".");
|
||||
if (i > 0)
|
||||
return this.Filename.Substring(0, i);
|
||||
else
|
||||
return this.Filename;
|
||||
}
|
||||
}
|
||||
private string _filename;
|
||||
private string _path;
|
||||
private DirectoryEntryTypes _fileType;
|
||||
private long _size;
|
||||
private DateTime _fileDateTime;
|
||||
private string _permission;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// ''' Identifies entry as either File or Directory
|
||||
/// ''' </summary>
|
||||
public enum DirectoryEntryTypes
|
||||
{
|
||||
File,
|
||||
Directory,
|
||||
Link
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ''' Constructor taking a directory listing line and path
|
||||
/// ''' </summary>
|
||||
/// ''' <param name="line">The line returned from the detailed directory list</param>
|
||||
/// ''' <param name="path">Path of the directory</param>
|
||||
/// ''' <remarks></remarks>
|
||||
public FTPfileInfo(string line, string path)
|
||||
{
|
||||
// parse line
|
||||
|
||||
|
||||
|
||||
Match m = GetMatchingRegex(line);
|
||||
if (m == null)
|
||||
// failed
|
||||
throw new ApplicationException("Unable to parse line: " + line);
|
||||
else
|
||||
{
|
||||
_filename = m.Groups["name"].Value;
|
||||
_path = path;
|
||||
|
||||
|
||||
_permission = m.Groups["permission"].Value;
|
||||
string _dir = m.Groups["dir"].Value;
|
||||
if ((_dir != "" & (_dir == "d" || _dir == "D")))
|
||||
{
|
||||
_fileType = DirectoryEntryTypes.Directory;
|
||||
_size = 0; // CLng(m.Groups("size").Value)
|
||||
}
|
||||
else if ((_dir != "" & (_dir == "l" || _dir == "L")))
|
||||
{
|
||||
_fileType = DirectoryEntryTypes.Link;
|
||||
_size = 0; // CLng(m.Groups("size").Value)
|
||||
}
|
||||
else
|
||||
{
|
||||
_fileType = DirectoryEntryTypes.File;
|
||||
_size = System.Convert.ToInt64(m.Groups["size"].Value);
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var timestamp = m.Groups["timestamp"].Value;
|
||||
if (timestamp.IndexOf(':') == -1)
|
||||
{
|
||||
_fileDateTime = DateTime.Parse(timestamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
_fileDateTime = DateTime.Parse(DateTime.Now.Year + " " + timestamp);
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
// MsgBox("datetime err=" & Now.Year & Space(1) & "value=" & m.Groups("timestamp").Value & vbCrLf & ex.Message.ToString)
|
||||
_fileDateTime = DateTime.Parse("1982-11-23");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FTPfileInfo(string filename, string permission, string dir, int size, DateTime filetime, Boolean isdir, string path)
|
||||
{
|
||||
_filename = filename;// m.Groups["name"].Value;
|
||||
_path = path;
|
||||
_permission = permission; // m.Groups["permission"].Value;
|
||||
string _dir = dir;// m.Groups["dir"].Value;
|
||||
if (isdir == true)
|
||||
{
|
||||
_fileType = DirectoryEntryTypes.Directory;
|
||||
_size = 0; // CLng(m.Groups("size").Value)
|
||||
}
|
||||
else
|
||||
{
|
||||
_fileType = DirectoryEntryTypes.File;
|
||||
_size = size;//
|
||||
}
|
||||
|
||||
_fileDateTime = filetime;
|
||||
}
|
||||
|
||||
private Match GetMatchingRegex(string line)
|
||||
{
|
||||
Regex rx;
|
||||
Match m;
|
||||
for (int i = 0; i <= _ParseFormats.Length - 1; i++)
|
||||
{
|
||||
rx = new Regex(_ParseFormats[i]);
|
||||
m = rx.Match(line);
|
||||
if (m.Success)
|
||||
return m;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ''' List of REGEX formats for different FTP server listing formats
|
||||
/// ''' </summary>
|
||||
/// ''' <remarks>
|
||||
/// ''' The first three are various UNIX/LINUX formats, fourth is for MS FTP
|
||||
/// ''' in detailed mode and the last for MS FTP in 'DOS' mode.
|
||||
/// ''' I wish VB.NET had support for Const arrays like C# but there you go
|
||||
/// ''' </remarks>
|
||||
private static string[] _ParseFormats = new[] { @"(?<dir>[\-dl])(?<permission>([\-r][\-w][\-xs]){3})\s+\d+\s+\w+\s+\w+\s+(?<size>\d+)\s+(?<timestamp>\w+\s+\d+\s+\d{4})\s+(?<name>.+)", @"(?<dir>[\-dl])(?<permission>([\-r][\-w][\-xs]){3})\s+\d+\s+\d+\s+(?<size>\d+)\s+(?<timestamp>\w+\s+\d+\s+\d{4})\s+(?<name>.+)", @"(?<dir>[\-dl])(?<permission>([\-r][\-w][\-xs]){3})\s+\d+\s+\d+\s+(?<size>\d+)\s+(?<timestamp>\w+\s+\d+\s+\d{1,2}:\d{2})\s+(?<name>.+)", @"(?<dir>[\-dl])(?<permission>([\-r][\-w][\-xs]){3})\s+\d+\s+\w+\s+\w+\s+(?<size>\d+)\s+(?<timestamp>\w+\s+\d+\s+\d{1,2}:\d{2})\s+(?<name>.+)", @"(?<dir>[\-dl])(?<permission>([\-r][\-w][\-xs]){3})(\s+)(?<size>(\d+))(\s+)(?<ctbit>(\w+\s\w+))(\s+)(?<size2>(\d+))\s+(?<timestamp>\w+\s+\d+\s+\d{2}:\d{2})\s+(?<name>.+)", @"(?<timestamp>\d{2}\-\d{2}\-\d{2}\s+\d{2}:\d{2}[Aa|Pp][mM])\s+(?<dir>\<\w+\>){0,1}(?<size>\d+){0,1}\s+(?<name>.+)" };
|
||||
}
|
||||
}
|
||||
34
Handler/Project/Class/FTP/IFTPClient.cs
Normal file
34
Handler/Project/Class/FTP/IFTPClient.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace AR.FTPClient
|
||||
{
|
||||
public interface IFTPClient
|
||||
{
|
||||
void Dispose();
|
||||
string errorMessage { get; set; }
|
||||
//string Host { get; set; }
|
||||
//string UserID { get; set; }
|
||||
//string UserPassword { get; set; }
|
||||
//int Port { get; set; }
|
||||
System.Text.Encoding TextEncoding { get; set; }
|
||||
Boolean Download(string remoteFile, string localFile,bool overwrite=false);
|
||||
Boolean Upload(string remoteFile, string localFile);
|
||||
Boolean Delete(string remotefile);
|
||||
Boolean rename(string currentFileNameAndPath, string newFileName);
|
||||
bool createDirectory(string newDirectory);
|
||||
bool RemoveDirectory(string Directory);
|
||||
string getFileCreatedDateTime(string fileName);
|
||||
string getFileSize(string fileName);
|
||||
string[] directoryListSimple(string directory);
|
||||
FTPdirectory ListDirectoryDetail(string directory);
|
||||
event EventHandler<MessageEventArgs> Message;
|
||||
event EventHandler<ListProgressEventArgs> ListPrgress;
|
||||
void RaiseMessage(Boolean isError, string message);
|
||||
string PathCombine(string path, string add);
|
||||
string PathFileCombine(string path, string add);
|
||||
string getParent(string path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user