c#

[c#] FTP์—์„œ ๋””๋ ‰ํ† ๋ฆฌ(Directory)์™€ ํŒŒ์ผ(File) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

Heeyeon Choi 2023. 3. 21. 17:38
728x90
728x90

๐Ÿ’ก ๋””๋ ‰ํ† ๋ฆฌ์™€ ํŒŒ์ผ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•?

  1. ๋ถˆ๋Ÿฌ์˜จ ์ฃผ์†Œ๋ฅผ ๋‹ค์‹œ ๋ง๋ถ™์—ฌ์„œ ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ → ํ•˜์œ„ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, ๊ฒ€์‚ฌํ•˜๊ธฐ ์–ด๋ ค์›€
  2. ํŒŒ์ผ ์ ‘๊ทผ ๊ถŒํ•œ์— ‘d’๊ฐ€ ๋ถ™์€ ์ง€ ๊ฒ€์‚ฌ
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace loadingFtp
{
    internal class Class1
    {
        public static void Main(String[] args)
        {
            Uri ftpUri = new Uri("ftp://xxx.xxx.xxx.xxx//xxx");

            FtpWebRequest reqFtp = (FtpWebRequest)WebRequest.Create(ftpUri);
            reqFtp.Credentials = new NetworkCredential("ID", "PassWord");
            reqFtp.Timeout = 10000;

            reqFtp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            FtpWebResponse resFtp = (FtpWebResponse)reqFtp.GetResponse();

            StreamReader reader;
            reader = new StreamReader(resFtp.GetResponseStream());

            string strData;
            strData = reader.ReadToEnd();

            string[] filesInDirectory = strData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            //์ถœ๋ ฅ
            for (int i = 0; i < filesInDirectory.Length; i++)
            {
                Console.WriteLine(filesInDirectory[i] +" || " + checkDirectory(filesInDirectory[i]));
            }
            resFtp.Close();
        }
        //๋””๋ ‰ํ† ๋ฆฌ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜
        public static string checkDirectory(string str)
        {
            if (str[0] == 'd')
            {
                return "๋””๋ ‰ํ† ๋ฆฌ";
            }
            else
            {
                return "ํŒŒ์ผ";
            }
        }
    }
}

- Uri ftpUri ๋ถ€๋ถ„์— ์•Œ๋งž์€ ์ฃผ์†Œ๊ฐ’์„ x ๋ถ€๋ถ„์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

- reqFtp.Credentials = new NetworkCredential() ์˜ ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•Œ๋งž๊ฒŒ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

<๊ฒฐ๊ณผ>

728x90