[C# 으로 FTP 접속하는 방법] C# FTP 접속 Visual Studio 2019 기준으로 작성

2024. 3. 21. 10:06IT/Visual Studio (C#)

반응형

C# 으로 FTP 접속하는 방법

C# 으로 FTP 접속하는 방법, 파일 업로드, 다운로드, 삭제 등 진행

※ 예제코드 1

● FTPManager.cs

using System;

using System.Collections.Generic;

using System.Net;

using System.Text;

namespace Directory_copy
{
         public class FTPManager

        {
            public delegate void ExceptionEventHandler(string LocationID, Exception ex);
            public event ExceptionEventHandler ExceptionEvent;
            public Exception LastException = null;

            public bool IsConnected { get; set; }

            private string ipAddr = string.Empty;
            private string port = string.Empty;
            private string userId = string.Empty;
            private string pwd = string.Empty;

            public FTPManager()
            {
            }

            public bool ConnectToServer(string ip, string port, string userId, string pwd)
            {
                this.IsConnected = false;

                this.ipAddr = ip;
                this.port = port;
                this.userId = userId;
                this.pwd = pwd;

                string url = string.Format(@"FTP://{0}:{1}/", this.ipAddr, this.port);

                try
                {
                    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(url);
                    ftpRequest.Credentials = new NetworkCredential(userId, pwd);
                    ftpRequest.KeepAlive = false;
                    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                    ftpRequest.UsePassive = false;

                    using (ftpRequest.GetResponse())
                    {
                    }

                    this.IsConnected = true;
                }

                catch (Exception ex)
                {
                    this.LastException = ex;

                    System.Reflection.MemberInfo info = System.Reflection.MethodInfo.GetCurrentMethod();

                    string id = string.Format("{0}.{1}", info.ReflectedType.Name, info.Name);

                    if (this.ExceptionEvent != null)
                        this.ExceptionEvent(id, ex);
                    return false;
                }

                return true;
            }
        }
    }

 

※ 예제코드 2

 Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Windows.Forms;

using System.IO;

namespace Directory_copy
{
    public partial class Form1 : Form
    {
        //FTP 접속에 필요한 정보
        string addr = string.Empty;
        string user = string.Empty;
        string pwd = string.Empty;
        string port = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }        

        private void button1_Click(object sender, EventArgs e)
        {  
            addr = "192.168.0.1";
            user = "USERID";
            pwd = "USERPW";
            port = "21";

            FTPManager manager = new FTPManager();

            bool result = manager.ConnectToServer(addr, port, user, pwd);

            if (result == true)
            {
                Console.WriteLine("FTP 접속에 성공했습니다.");
            }
            else
            {
                Console.WriteLine("FTP 접속에 실패했습니다.");
            }
        }
    }
}

 

반응형