落書きノート

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

C# FTPクライアント作成その2

とりあえずここまで進みました。

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 result_box_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void local_url_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);

            local_url.Text = get_remote_current_path(listing_box_address.Text);

            string r;

            while((r = reader.ReadLine()) != null){

                string name;
                string date;
                string size;
                string type;
                string attribute;
                string owner;

                string[] split = r.Split(new char [] {' ', '\t'});
                split = split.Where(x => x != null).Where(x => x != "").ToArray();

                /*foreach(string s in split)
                {
                    Console.WriteLine(s);
                }*/

                Array.Reverse(split);

                name = split[0];
                date = split[1] + split[2] + split[3];
                size = split[4];

                string[] file_sep = split[0].Split(new char[] { '.' });

                if (file_sep.Length > 1)
                {
                    type = file_sep[file_sep.Length - 1];
                }
                else
                {
                    type = "";
                }

                attribute = split[8];
                owner = split[6];

                string[] data = { name, date, size, type, attribute, owner };

                local_directory.Items.Add(new ListViewItem(data));                            
            }

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

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

        private string get_remote_current_path(string uri)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
            request.Credentials = new NetworkCredential("osmc", "osmc");
            request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
            request.KeepAlive = false;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            return response.StatusDescription.Substring(5, response.StatusDescription.Length - 8);
        }

        private void listing_box_address_TextChanged(object sender, EventArgs e)
        {

        }

        private void local_directory_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void remote_directory_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void remote_url_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

リモートのファイル表示場所が左右逆ですが、それは後で修正します。後、日付表示がおかしいですね。

前回、コントロールの名前変更を手動でやってましたが、考えてみればリファクター機能があったんですね。これを使ってリファクタリングすれば簡単に名前を変えられます。これを使わないと厄介になることもありそうです。

20160118204833