落書きノート

ふと自分が気になった事を書いてます

とりあえずFTPクライアント作成

ドキュメントを読みながらFTPクライアントをとりあえず作成しました。

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ftp_client_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void ftp_address_TextChanged(object sender, EventArgs e)
        {

        }

        private void download_Click(object sender, EventArgs e)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp_address.Text);
            result_box.Text += "Connecting...\r\n";
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            request.Credentials = new NetworkCredential("osmc", "osmc");

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

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            result_box.Text += reader.ReadToEnd() + "\r\n";
            result_box.Text += "Download Complete, Status " + response.StatusDescription + "\r\n";

            reader.Close();
            response.Close();
        }

        private void upload_Click(object sender, EventArgs e)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp_address_upload.Text);
            result_box.Text += "Connecting...\r\n";
            request.Method = WebRequestMethods.Ftp.UploadFile;

            request.Credentials = new NetworkCredential("osmc", "osmc");

            StreamReader sourceStream = new StreamReader("sample.txt");
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

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

            result_box.Text += "Upload File Complete, status " + response.StatusDescription + "\r\n";

            response.Close();
        }

        private void result_box_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void ftp_address_upload_TextChanged(object sender, EventArgs e)
        {

        }

        private void listing_box_TextChanged(object sender, EventArgs e)
        {

        }

        private void listing_button_Click(object sender, EventArgs e)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(listing_box_address.Text);
            result_box.Text += "Connecting...\r\n";
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            request.Credentials = new NetworkCredential("osmc", "osmc");

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

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            listing_box.Text += reader.ReadToEnd() + "\r\n";

            result_box.Text += "Directory List Complete, status " + response.StatusDescription + "\r\n";

            reader.Close();
            response.Close();
        }

        private void listing_box_address_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Form1.Designer.cs

namespace ftp_client_1
{
    partial class Form1
    {
        /// <summary>
        /// 必要なデザイナー変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナーで生成されたコード

        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.download = new System.Windows.Forms.Button();
            this.ftp_address = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.upload = new System.Windows.Forms.Button();
            this.result_box = new System.Windows.Forms.TextBox();
            this.ftp_address_upload = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.listing_box = new System.Windows.Forms.TextBox();
            this.listing_box_address = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.listing_button = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // download
            // 
            this.download.Location = new System.Drawing.Point(429, 12);
            this.download.Name = "download";
            this.download.Size = new System.Drawing.Size(75, 19);
            this.download.TabIndex = 0;
            this.download.Text = "ダウンロード";
            this.download.UseVisualStyleBackColor = true;
            this.download.Click += new System.EventHandler(this.download_Click);
            // 
            // ftp_address
            // 
            this.ftp_address.Location = new System.Drawing.Point(74, 12);
            this.ftp_address.Name = "ftp_address";
            this.ftp_address.Size = new System.Drawing.Size(347, 19);
            this.ftp_address.TabIndex = 1;
            this.ftp_address.TextChanged += new System.EventHandler(this.ftp_address_TextChanged);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(8, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(60, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "ダウンロード";
            this.label1.Click += new System.EventHandler(this.label1_Click);
            // 
            // upload
            // 
            this.upload.Location = new System.Drawing.Point(429, 38);
            this.upload.Name = "upload";
            this.upload.Size = new System.Drawing.Size(75, 20);
            this.upload.TabIndex = 3;
            this.upload.Text = "アップロード";
            this.upload.UseVisualStyleBackColor = true;
            this.upload.Click += new System.EventHandler(this.upload_Click);
            // 
            // result_box
            // 
            this.result_box.Location = new System.Drawing.Point(10, 395);
            this.result_box.Multiline = true;
            this.result_box.Name = "result_box";
            this.result_box.Size = new System.Drawing.Size(494, 82);
            this.result_box.TabIndex = 4;
            this.result_box.TextChanged += new System.EventHandler(this.result_box_TextChanged);
            // 
            // ftp_address_upload
            // 
            this.ftp_address_upload.Location = new System.Drawing.Point(74, 38);
            this.ftp_address_upload.Name = "ftp_address_upload";
            this.ftp_address_upload.Size = new System.Drawing.Size(347, 19);
            this.ftp_address_upload.TabIndex = 5;
            this.ftp_address_upload.TextChanged += new System.EventHandler(this.ftp_address_upload_TextChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(8, 41);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(58, 12);
            this.label2.TabIndex = 6;
            this.label2.Text = "アップロード";
            this.label2.Click += new System.EventHandler(this.label2_Click);
            // 
            // listing_box
            // 
            this.listing_box.Location = new System.Drawing.Point(10, 88);
            this.listing_box.Multiline = true;
            this.listing_box.Name = "listing_box";
            this.listing_box.Size = new System.Drawing.Size(494, 301);
            this.listing_box.TabIndex = 7;
            this.listing_box.TextChanged += new System.EventHandler(this.listing_box_TextChanged);
            // 
            // listing_box_address
            // 
            this.listing_box_address.Location = new System.Drawing.Point(74, 63);
            this.listing_box_address.Name = "listing_box_address";
            this.listing_box_address.Size = new System.Drawing.Size(347, 19);
            this.listing_box_address.TabIndex = 8;
            this.listing_box_address.TextChanged += new System.EventHandler(this.listing_box_address_TextChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(8, 66);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(53, 12);
            this.label3.TabIndex = 9;
            this.label3.Text = "リスト取得";
            // 
            // listing_button
            // 
            this.listing_button.Location = new System.Drawing.Point(429, 63);
            this.listing_button.Name = "listing_button";
            this.listing_button.Size = new System.Drawing.Size(75, 19);
            this.listing_button.TabIndex = 10;
            this.listing_button.Text = "取得";
            this.listing_button.UseVisualStyleBackColor = true;
            this.listing_button.Click += new System.EventHandler(this.listing_button_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(516, 489);
            this.Controls.Add(this.listing_button);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.listing_box_address);
            this.Controls.Add(this.listing_box);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.ftp_address_upload);
            this.Controls.Add(this.result_box);
            this.Controls.Add(this.upload);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.ftp_address);
            this.Controls.Add(this.download);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button download;
        private System.Windows.Forms.TextBox ftp_address;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button upload;
        private System.Windows.Forms.TextBox result_box;
        private System.Windows.Forms.TextBox ftp_address_upload;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox listing_box;
        private System.Windows.Forms.TextBox listing_box_address;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button listing_button;
    }
}

Form1.Designer.csは、普通は編集してはいけませんが、コントロールの名前の付け方を失敗したとき等で、名前を修正するときに使えます。

スクリーンショットは以下のようになります。

20160111161831

後は調べながら自分でカスタマイズすれば、実用に耐えうるアプリが出来ると思います。

文字コード関係をしっかりしておかないとファイルの文字化けを引き起こすので注意が必要です。