落書きノート

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

VC# FTPクライアント作成の途中経過その6

ここまで出来ました。もうすぐで完成かな。肝心のダウンロード機能とアップロード機能に専念すれば良いわけですね。

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
    {
        private static string ftp_address;
        private static string remote_path;

        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)
        {
            if (!listing_box_address.Text.StartsWith("ftp://"))
            {
                ftp_address = "ftp://" + listing_box_address.Text;
            }
            else
            {
                ftp_address = listing_box_address.Text;
            }

            local_url.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            remote_url.Text = get_remote_current_path();

            local_directory_show(local_url.Text);
            remote_directory_show();
        }

        private void local_directory_show(string uri)
        {
            if (Directory.Exists(uri))
            {
                local_directory.Items.Clear();

                IEnumerable<string> local_files = Directory.EnumerateFileSystemEntries(uri, "*", SearchOption.TopDirectoryOnly);

                foreach (string s in local_files)
                {
                    string size;
                    string type;
                    string[] local_data;

                    FileInfo fi = null;
                    DirectoryInfo di = null;

                    fi = new FileInfo(s);
                    di = new DirectoryInfo(s);

                    if (fi.Exists)
                    {
                        size = fi.Length.ToString();
                        string[] local_file_sep = fi.Name.Split(new char[] { '.' });

                        if (local_file_sep.Length > 1)
                            type = local_file_sep[local_file_sep.Length - 1];
                        else
                            type = "";

                        local_data = new string[] { fi.Name, fi.LastAccessTime.ToString(), size, type };
                    }
                    else
                    {
                        size = "<DIR>";
                        type = "";
                        local_data = new string[] { di.Name, di.LastAccessTime.ToString(), size, type };
                    }

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

                local_url.Text = uri;
            }
        }

        private void remote_directory_show()
        {
            remote_directory.Items.Clear();

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

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

                Array.Reverse(split);

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

                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];

                if (attribute.StartsWith("d"))
                {
                    size = "<DIR>";
                }
                else
                {
                    size = split[4];
                }

                owner = split[6];

                string[] data = { name, date, size, type, attribute, owner };
                remote_directory.Items.Add(new ListViewItem(data));
            }

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

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

        private void remote_directory_show(string target_item_uri)
        {
            remote_directory.Items.Clear();

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

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

                Array.Reverse(split);

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

                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];

                if (attribute.StartsWith("d"))
                {
                    size = "<DIR>";
                }
                else
                {
                    size = split[4];
                }

                owner = split[6];

                string[] data = { name, date, size, type, attribute, owner };
                remote_directory.Items.Add(new ListViewItem(data));
            }

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

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

            remote_url.Text = get_remote_current_path(target_item_uri);
        }

        private FtpWebResponse get_remote_file_list()
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp_address);
            result_box.Text = "Connecting...\r\n";
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

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

            return (FtpWebResponse)request.GetResponse();
        }

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

            remote_path = response.StatusDescription.Substring(5, response.StatusDescription.Length - 8);

            response.Close();

            return remote_path;
        }

        private string get_remote_current_path(string target_item_uri)
        {
            remote_path = remote_path + "/" + target_item_uri;

            return remote_path;
        }

        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)
        {

        }

        private void local_contextmenu_Opening(object sender, CancelEventArgs e)
        {
            ContextMenuStrip menu = (ContextMenuStrip)sender;
            Control source = menu.SourceControl;

            if (local_directory.SelectedIndices.Count > 0 && source != null)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void remote_contextmenu_Opening(object sender, CancelEventArgs e)
        {
            ContextMenuStrip menu = (ContextMenuStrip)sender;
            Control source = menu.SourceControl;

            if (remote_directory.SelectedIndices.Count > 0 && source != null)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void download_toolstripmenu_Click(object sender, EventArgs e)
        {

        }

        private void upload_toolstripmenu_Click(object sender, EventArgs e)
        {

        }

        private void local_directory_mouse_double_click(object sender, MouseEventArgs e)
        {
            ListView listview = (ListView)sender;
            ListViewItem target_item = listview.FocusedItem;

            local_directory_show(local_url.Text + "\\" + target_item.Text);
        }

        private void remote_directory_mouse_double_click(object sender, MouseEventArgs e)
        {
            ListView listview = (ListView)sender;
            ListViewItem target_item = listview.FocusedItem;

            ftp_address = ftp_address + "/" + target_item.Text;

            remote_directory_show(target_item.Text);
        }
    }
}

20160409183713