■ FTP 파일 크기 및 시간 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Net;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.searchButton.Click += searchButton_Click;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 조회 버튼 클릭시 처리하기 - searchButton_Click(sender, e)

 

        /// <summary>

        /// 조회 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void searchButton_Click(object sender, EventArgs e)

        {

            try

            {

                Cursor = Cursors.WaitCursor;

 

                this.statusLabel.Text = "작업을 시작합니다.";

 

                this.fileSizeTextBox.Clear();

                this.timeStampTextBox.Clear();

 

                Refresh();

 

                this.fileSizeTextBox.Text = GetFTPFileSize

                (

                    this.urlTextBox.Text,

                    this.userNameTextBox.Text,

                    this.passwordTextBox.Text

                ).ToString();

 

                this.timeStampTextBox.Text = GetFTPFileTimestamp

                (

                    this.urlTextBox.Text,

                    this.userNameTextBox.Text,

                    this.passwordTextBox.Text

                ).ToString();

 

                this.statusLabel.Text = "작업을 완료했습니다.";

            }

            catch(Exception exception)

            {

                this.statusLabel.Text = "에러가 발생했습니다.";

 

                MessageBox.Show(exception.Message);

            }

            finally

            {

                Cursor = Cursors.Default;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region FTP 파일 크기 구하기 - GetFTPFileSize(ftpURL, ftpUserName, ftpPassword)

 

        /// <summary>

        /// FTP 파일 크기 구하기

        /// </summary>

        /// <param name="ftpURL">FTP URL</param>

        /// <param name="ftpUserName">FTP 사용자명</param>

        /// <param name="ftpPassword">FTP 패스워드</param>

        /// <returns>FTP 파일 크기</returns>

        private long GetFTPFileSize(string ftpURL, string ftpUserName, string ftpPassword)

        {

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);

 

            request.Method = WebRequestMethods.Ftp.GetFileSize;

 

            request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

 

            try

            {

                using(FtpWebResponse response = (FtpWebResponse)request.GetResponse())

                {

                    return response.ContentLength;

                }

            }

            catch(Exception exception)

            {

                if(exception.Message.Contains("File unavailable"))

                {

                    return -1;

                }

 

                throw;

            }

        }

 

        #endregion

        #region FTP 파일 타임스탬프 구하기 - GetFTPFileTimestamp(ftpURL, ftpUserName, ftpPassword)

 

        /// <summary>

        /// FTP 파일 타임스탬프 구하기

        /// </summary>

        /// <param name="ftpURL">FTP URL</param>

        /// <param name="ftpUserName">FTP 사용자명</param>

        /// <param name="ftpPassword">FTP 패스워드</param>

        /// <returns>FTP 파일 타임스탬프</returns>

        private DateTime GetFTPFileTimestamp(string ftpURL, string ftpUserName, string ftpPassword)

        {

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);

 

            request.Method = WebRequestMethods.Ftp.GetDateTimestamp;

 

            request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

 

            try

            {

                using(FtpWebResponse response = (FtpWebResponse)request.GetResponse())

                {

                    return response.LastModified;

                }

            }

            catch(Exception exception)

            {

                if(exception.Message.Contains("File unavailable"))

                {

                    return new DateTime(3000, 1, 1);

                }

 

                throw;

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요