가장 긴 Regular expression을 적고 만듯한 소스... 좀 고칠 방법을 찾아야지 될듯.
FTP Client Source
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; namespace SYNCmailDatabaseAPI.FTP { public class FTPClient { public string Host { get; set; } public string RemotePath { get; set; } public string UserName { get; set; } public string Password { get; set; } public int Port { get; set; } public string FTPStatusDescription { get; private set; } public FTPClient() { Port = 21; UserName = "anonymous"; Password = "anonymous@hostway.co.kr"; } public bool MakeDirectory(string subDirectory) { try { FtpWebRequest ftpRequest = BuildFtpWebRequest(subDirectory); ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory; FtpWebResponse ftpResponse = ( FtpWebResponse )ftpRequest.GetResponse(); FTPStatusDescription = ftpResponse.StatusDescription; ftpResponse.Close(); return true; } catch(Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.InnerException); return false; } } public bool UploadFile(string remoteFileName, byte[] btDatas) { FtpWebRequest ftpRequest = BuildFtpWebRequest(remoteFileName); ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; try { FtpWebResponse ftpResponse = (FtpWebResponse) ftpRequest.GetResponse(); using(Stream st = ftpRequest.GetRequestStream()) { Console.WriteLine(btDatas.Length); Console.WriteLine(btDatas); st.Write(btDatas, 0, btDatas.Length); } FTPStatusDescription = ftpResponse.StatusDescription; ftpResponse.Close(); } catch(Exception ex) { Console.WriteLine("Ex : " + ex.Message); Console.WriteLine(ex.InnerException); return false; } return true; } public FTPItem[] GetFileList(string rootDirectory) { FtpWebRequest ftpRequest = BuildFtpWebRequest(rootDirectory); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response = (FtpWebResponse) ftpRequest.GetResponse(); byte[] btDatas = new byte[4086]; int offset = 0; int readCount = 0; using(Stream st = response.GetResponseStream()) { while( (readCount = st.Read(btDatas, offset, 20)) > 0 ) { offset += readCount; } } string convertedString = Encoding.Default.GetString(btDatas, 0, offset); string[] filesInDirectory = convertedString.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries); List<FTPItem> ftpObjects = new List<FTPItem>(); foreach(string file in filesInDirectory) { Console.WriteLine("{0} / {1}", file, file.Length); FTPItem ftpObject = new FTPItem(file); ftpObjects.Add(ftpObject); } return ftpObjects.ToArray(); } private FtpWebRequest BuildFtpWebRequest(string subUri) { string ftpUri = "ftp://" + Host + "/" + subUri; Console.WriteLine(ftpUri); FtpWebRequest ftpRequest = (FtpWebRequest) FtpWebRequest.Create(ftpUri); ftpRequest.Credentials = new NetworkCredential(UserName, Password); ftpRequest.UseBinary = true; return ftpRequest; } } }
FTPItem
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace SYNCmailDatabaseAPI.FTP { public class FTPItem { public string Name { get; private set; } public int Size { get; private set; } public bool IsDirectory { get; private set; } public DateTime LastUpdateDate { get; private set; } private static DateTime ConvertDateTime(string ftpMessage) { int year = int.Parse(ftpMessage.Substring(6, 2)); int month = int.Parse(ftpMessage.Substring(0, 2)); int day = int.Parse(ftpMessage.Substring(3, 2)); int hour = int.Parse(ftpMessage.Substring(10, 2)); int minute = int.Parse(ftpMessage.Substring(13, 2)); bool isAM = ftpMessage.Contains("AM"); if(!isAM) { hour = hour + 12; } Console.WriteLine("Year : {0}, Month : {1}, Day {2}, Hour : {3}, Minute {4}", year, month, day, hour, minute); return new DateTime(year, month, day, hour, minute, 0); } public FTPItem(string ftpResponseString) { //04-14-08 02:22PM <DIR> App_Data //03-29-08 06:07AM 112 Global.asax Regex regx = new Regex(@"\A(?<date>[0-9]{2}\-[0-9]{2}\-[0-9]{2}\s\s[0-9]{2}\:[0-9]{2}(AM|PM))\s*((?<filesize>[0-9]+)|(?<dir>\<DIR\>\s*))\s(?<name>(\w|\W)*)\Z"); Match match = regx.Match(ftpResponseString); string dateString = match.Groups[ "date" ].Value; LastUpdateDate = ConvertDateTime(dateString); IsDirectory = match.Groups[ "dir" ].Success; if ( !IsDirectory ) { Size = int.Parse(match.Groups[ "filesize" ].Value); } else { Size = 0; } Name = match.Groups[ "name" ].Value; } } }