FluentFTP を利用

 

https://github.com/robinrodricks/FluentFTP

 

接続サンプル

internal static class ConnectExample {

        public static void Connect() {

            using (var conn = new FtpClient()) {
                conn.Host = "localhost";
                conn.Credentials = new NetworkCredential("ftptest", "ftptest");

                conn.Connect();
            }
        }

        public static void ConnectAlt() {

            using (var conn = new FtpClient("127.0.0.1", "ftptest", "ftptest")) {

                conn.Connect();
            }
        }

        public static async Task ConnectAsync() {
            var token = new CancellationToken();

            using (var conn = new FtpClient()) {
                conn.Host = "localhost";
                conn.Credentials = new NetworkCredential("ftptest", "ftptest");

                await conn.ConnectAsync(token);
            }
        }

        public static async Task ConnectAsyncAlt() {
            var token = new CancellationToken();

            using (var conn = new FtpClient("127.0.0.1", "ftptest", "ftptest")) {

                await conn.ConnectAsync(token);
            }
        }
    }


FTPSのサンプル
namespace Examples {
    internal static class ConnectFTPSExample {

        public static void ConnectFTPS() {
            using (var conn = new FtpClient("127.0.0.1", "ftptest", "ftptest")) {
                conn.EncryptionMode = FtpEncryptionMode.Explicit;
                conn.ValidateAnyCertificate = true;
                conn.Connect();
            }
        }

        public static async Task ConnectFTPSAsync() {
            var token = new CancellationToken();
            using (var conn = new FtpClient("127.0.0.1", "ftptest", "ftptest")) {

                conn.EncryptionMode = FtpEncryptionMode.Explicit;
                conn.ValidateAnyCertificate = true;
                await conn.ConnectAsync(token);
            }
        }
    }
}

 

フォルダ存在チェック

        public static void DirectoryExists() {
            using (var conn = new FtpClient("127.0.0.1", "ftptest", "ftptest")) {
                conn.Connect();

                if (conn.DirectoryExists("/full/or/relative/path")) {
                    // do something
                }
            }
        }

 

フォルダ作る

        public static void CreateDirectory() {
            using (var conn = new FtpClient("127.0.0.1", "ftptest", "ftptest")) {
                conn.Connect();

                conn.CreateDirectory("/test/path/that/should/be/created", true);
            }
        }

 

ファイルUP

        public static void UploadFile() {
            using (var ftp = new FtpClient("127.0.0.1", "ftptest", "ftptest")) {
                ftp.Connect();

                // upload a file to an existing FTP directory
                ftp.UploadFile(@"D:\Github\FluentFTP\README.md", "/public_html/temp/README.md");

                // upload a file and ensure the FTP directory is created on the server
                ftp.UploadFile(@"D:\Github\FluentFTP\README.md", "/public_html/temp/README.md", FtpRemoteExists.Overwrite, true);

                // upload a file and ensure the FTP directory is created on the server, verify the file after upload
                ftp.UploadFile(@"D:\Github\FluentFTP\README.md", "/public_html/temp/README.md", FtpRemoteExists.Overwrite, true, FtpVerify.Retry);

            }
        }

 

フォルダUP


        public static void UploadDirectory() {
            using (var ftp = new FtpClient("127.0.0.1", "ftptest", "ftptest")) {
                ftp.Connect();


                // upload a folder and all its files
                ftp.UploadDirectory(@"C:\website\videos\", @"/public_html/videos", FtpFolderSyncMode.Update);
                
                // upload a folder and all its files, and delete extra files on the server
                ftp.UploadDirectory(@"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror);
                
            }
        }