落書きノート

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

VC# FTPクライアント作成その3

ここまで進みました。

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 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            remote_url.Text = get_remote_current_path(listing_box_address.Text);

            IEnumerable<string> local_files = System.IO.Directory.EnumerateFiles(local_url.Text, "*", System.IO.SearchOption.TopDirectoryOnly);
            
            foreach (string s in local_files)
            {
                string type;

                System.IO.FileInfo fi = null;
                fi = new System.IO.FileInfo(s);

                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 = "";

                string[] local_data = { fi.Name, fi.LastAccessTime.ToString(), fi.Length.ToString(), type };
                local_directory.Items.Add(new ListViewItem(local_data));
            }

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

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

        }
    }
}

実行結果は以下の通りです。

20160203214819