落書きノート

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

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

全部実装しようとすると、なかなか大変ですね…。

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)
        {
            string ftp_address;

            if (!listing_box_address.Text.StartsWith("ftp://"))
            {
                ftp_address = "ftp://" + listing_box_address.Text;    
            }
            else
            {
                ftp_address = listing_box_address.Text;
            }

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp_address);
            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 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            remote_url.Text = get_remote_current_path(ftp_address);

            local_directory_show(local_url.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();

                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 local_directory_show(string 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 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)
        {

        }

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

いつ、2ちゃんねるブラウザの作成に入ろう…。

20160324054300