コード例 #1
0
ファイル: clsUtilsFtp.cs プロジェクト: nhhuy1804/Wmall
        /// <summary>
        /// 서버 폴더 및 파일목록 조회
        /// </summary>
        /// <param name="strPath">서버 경로</param>
        /// <returns></returns>
        public List <clsDirectoryItem> FtpGetList(string strPath, bool bAllList)
        {
            List <clsDirectoryItem> listDirectoryItem = new List <clsDirectoryItem>();

            try
            {
                //서버 풀 주소
                strFullPath = string.Format("{0}{1}:{2}/{3}", strBaseUrl, strServerIP, strServerPort, strPath);

                FtpWebRequest req = GetRequest(WebRequestMethods.Ftp.ListDirectoryDetails);

                // FTP Request 결과를 가져온다.
                using (FtpWebResponse resp = (FtpWebResponse)req.GetResponse())
                {
                    // FTP 결과 스트림
                    using (Stream stream = resp.GetResponseStream())
                    {
                        // 결과를 문자열로 읽기 (바이너리로 읽을 수도 있다)
                        string[] list = null;
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            list = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                        }

                        foreach (string line in list)
                        {
                            // Windows FTP Server Response Format
                            // DateCreated    IsDirectory    Name
                            string data = line;

                            // Parse date
                            string   date     = data.Substring(0, 17);
                            DateTime dateTime = DateTime.Parse(date, System.Globalization.CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat); //생성일자
                            data = data.Remove(0, 24);

                            // Parse <DIR>
                            string dir         = data.Substring(0, 5);
                            bool   isDirectory = dir.Equals("<dir>", StringComparison.InvariantCultureIgnoreCase); //폴더여부
                            data = data.Remove(0, 5);
                            string size = data.Substring(0, 10);
                            data = data.Remove(0, 10);

                            // Parse name
                            string name = data; //폴더명 or 파일명

                            clsDirectoryItem item = new clsDirectoryItem();
                            item.DateCreated = dateTime;
                            item.IsDirectory = isDirectory;
                            item.Name        = name;
                            item.ParentNm    = strPath;
                            item.Size        = size.Replace(" ", "");
                            listDirectoryItem.Add(item);

                            if (bAllList)
                            {
                                if (isDirectory)
                                {
                                    List <clsDirectoryItem> addlistDirectoryItem = FtpGetList(string.Format("{0}/{1}", strPath, name), bAllList);

                                    if (addlistDirectoryItem == null)
                                    {
                                        return(null);
                                    }
                                    else
                                    {
                                        foreach (clsDirectoryItem addItem in addlistDirectoryItem)
                                        {
                                            listDirectoryItem.Add(addItem);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                bSuccess  = false;
                strErrMsg = ex.Message.ToString();
                return(null);
            }
            finally
            {
            }

            return(listDirectoryItem);
        }
コード例 #2
0
ファイル: clsUtilsFtp.cs プロジェクト: nhhuy1804/Wmall
        public bool FtpDeleteAll(string strServerPath)
        {
            bool bReturn = false;

            try
            {
                //서버 풀 주소(파일명 포함)
                strFullPath = string.Format("{0}{1}:{2}/{3}", strBaseUrl, strServerIP, strServerPort, strServerPath);

                List <clsDirectoryItem> list = FtpGetList(strServerPath, true);
                string strPath = string.Empty;
                string result  = string.Empty;
                if (list != null && list.Count > 0)
                {
                    foreach (clsDirectoryItem item in list)
                    {
                        strPath = item.ParentNm + "/" + item.Name;

                        if (!item.IsDirectory)
                        {
                            if (!FtpDelete(strPath, false))
                            {
                                return(false);
                            }
                        }
                    }

                    for (int i = list.Count - 1; i >= 0; i--)
                    {
                        clsDirectoryItem item = list[i];

                        strPath = item.ParentNm + "/" + item.Name;

                        if (item.IsDirectory)
                        {
                            if (!FtpDelete(strPath, true))
                            {
                                return(false);
                            }
                        }
                    }
                }

                if (!FtpDelete(strServerPath, true))
                {
                    return(false);
                }

                bReturn = true;
            }
            catch (WebException ex)
            {
                bSuccess  = false;
                strErrMsg = ex.Message.ToString();
            }
            finally
            {
            }

            return(bReturn);
        }