첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.IO;
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 이벤트를 설정한다.

            Load                    += Form_Load;
            this.fileButton.Click   += fileButton_Click;
            this.uploadButton.Click += uploadButton_Click;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            this.fileButton.Image = Image.FromFile("ellipsis.png");

            this.fileTextBox.Text = Application.ExecutablePath;
        }

        #endregion
        #region 파일 버튼 클릭시 처리하기 - fileButton_Click(sender, e)

        /// <summary>
        /// 파일 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void fileButton_Click(object sender, EventArgs e)
        {
            if(this.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.fileTextBox.Text = this.openFileDialog.FileName;
            }
        }

        #endregion
        #region 업로드 버튼 클릭시 처리하기 - uploadButton_Click(sender, e)

        /// <summary>
        /// 업로드 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void uploadButton_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

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

                Application.DoEvents();

                UploadFile
                (
                    this.fileTextBox.Text,
                    this.urlTextBox.Text,
                    this.userNameTextBox.Text,
                    this.passwordTextBox.Text
                );

                this.statusLabel.Text = "작업을 완료했습니다.";
            }
            catch(Exception exception)
            {
                this.statusLabel.Text = "에러가 발생했습니다.";

                MessageBox.Show(exception.Message);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        #endregion

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

        #region 파일 업로드 하기 - private void UploadFile(filePath, ftpURL, ftpUserName, ftpPassword)

        /// <summary>
        /// 파일 업로드 하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <param name="ftpURL">FTP URL</param>
        /// <param name="ftpUserName">FTP 사용자명</param>
        /// <param name="ftpPassword">FTP 패스워드</param>
        private void UploadFile(string filePath, string ftpURL, string ftpUserName, string ftpPassword)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);

            request.Method = WebRequestMethods.Ftp.UploadFile;

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

            byte[] byteArray = File.ReadAllBytes(filePath);

            request.ContentLength = byteArray.Length;

            using(Stream stream = request.GetRequestStream())
            {
                stream.Write(byteArray, 0, byteArray.Length);

                stream.Close();
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요