■ FTP 텍스트 업로드 하기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.IO;

using System.Net;

using System.Text;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.uploadButton.Click += uploadButton_Click;

 

            #endregion

        }

 

        #endregion

 

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

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

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

 

        #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();

 

                UploadString

                (

                    DateTime.Now.ToString() + " : " + this.textTextBox.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 문자열 업로드하기 - UploadString(text, ftpURL, ftpUserName, ftpPassword)

 

        /// <summary>

        /// 문자열 업로드하기

        /// </summary>

        /// <param name="text">텍스트</param>

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

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

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

        private void UploadString(string text, string ftpURL, string ftpUserName, string ftpPassword)

        {

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

 

            request.Method = WebRequestMethods.Ftp.UploadFile;

 

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

 

            request.ContentLength = text.Length;

 

            using(Stream stream = request.GetRequestStream())

            {

                byte[] byteArray = Encoding.UTF8.GetBytes(text);

 

                stream.Write(byteArray, 0, text.Length);

 

                stream.Close();

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요