記録

DropFTPを配布。

ドップして一つのファイルを転送するソフトを作りました。 こんなのどうしているのかと疑問を持つ人もいると思いますが 業務上、こんなソフトが要るという会社などもいるのではないかと 思いで作りました。 ダウンロードはこちらから https://zip358.com/tool/DropFTP.zip ソースコードは下記になります。 ※FTP部分はWinSCPのライブラリを使用しています。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinSCP;
namespace dropFTP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void upbtn_Click(object sender, EventArgs e)
        {
            String err = "";
            if (hostText.Text == "") {
                err += "ホスト名が設定されていませんn";
            }
            if (idText.Text == "")
            {
                err += "IDが設定されていませんn";
            }
            if (passText.Text == "")
            {
                err += "passが設定されていませんn";
            }
            if (remText.Text == "")
            {
                err += "アップロード場所が設定されていませんn";
            }
            if (uplab.Text == "")
            {
                err += "アップロードファイルが設定されていませんn";
            }
            var RadioGroup = groupFTP.Controls.OfType<RadioButton>().SingleOrDefault(rb => rb.Checked == true);
            if (RadioGroup == null) {
                err += "アップロード環境が設定されていませんn";
            }
            if (err != "")
            {
                MessageBox.Show(err);
            }
            else {
                if (RadioGroup.Text == "FTP") {
                    upFTP();
                }
                if (RadioGroup.Text == "SFTP")
                {
                    upSFTP();
                }
            }
        }
            private int upFTP() {
            try
            {
                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Ftp,
                    HostName = hostText.Text,
                    UserName = idText.Text,
                    Password = passText.Text,
                    PortNumber =int.Parse(portText.Text)
                };
                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);
                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
                    TransferOperationResult transferResult;
                    if (remText.Text.EndsWith("/"))
                    {
                        transferResult = session.PutFiles(@uplab.Text, remText.Text, false, transferOptions);
                    }
                    else
                    {
                        transferResult = session.PutFiles(@uplab.Text, remText.Text + "/", false, transferOptions);
                    }
                    // Throw on any error
                    transferResult.Check();
                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        MessageBox.Show("アップロードしました");
                    }
                }
                return 0;
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: {0}" + e);
                return 1;
            }
        }
        private int upSFTP()
            {
                try
                {
                    // Setup session options
                    SessionOptions sessionOptions = new SessionOptions
                    {
                        Protocol = Protocol.Sftp,
                        HostName = hostText.Text,
                        UserName = idText.Text,
                        Password = passText.Text,
                        PortNumber = int.Parse(portText.Text),
                        GiveUpSecurityAndAcceptAnySshHostKey = true
                    };
                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);
                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;
                    TransferOperationResult transferResult;
                    if (remText.Text.EndsWith("/")) {
                        transferResult = session.PutFiles(@uplab.Text, remText.Text, false, transferOptions);
                    } else {
                        transferResult = session.PutFiles(@uplab.Text, remText.Text + "/", false, transferOptions);
                    }
                        // Throw on any error
                        transferResult.Check();
                        // Print results
                        foreach (TransferEventArgs transfer in transferResult.Transfers)
                        {
                        MessageBox.Show("アップロードしました");
                        }
                    }
                    return 0;
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error: {0}" + e);
                    return 1;
                }
            }
            private void Form1_DragDrop(object sender, DragEventArgs e) {
            //e.Effect = DragDropEffects.Copy;
            string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            uplab.Text = fileName[0];
        }
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
        private void radioSFTP_CheckedChanged(object sender, EventArgs e)
        {
            portText.Text = "22";
        }
        private void radioFTP_CheckedChanged(object sender, EventArgs e)
        {
            portText.Text = "21";
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;
        }
    }
}







    いろいろエディタやIDEを試してみてこれが良いかなと。前のページ

    ブロックエディタとクラッシックエディタ次のページ

    関連記事

    1. 記録

      昨日のアクセス数。

      昨日のアクセス数は雀の涙ほどのアクセス数しかなく残念感じになってい…

    2. 記録

      #サンプルコード 極力おなじコードを書かないようにする方法。

      極力おなじコードを書かないようにする方法。同じオブジェクトを複数配置…

    3. 記録

      この頃、超暑かったですが。

      この頃、猛暑が続きましたが暑いぐらいに来週あたりから徐々に温度…

    4. 記録

      高知のソフトウェア会社、デジトップが破産したで思うこと。

      高知のソフトウェア会社、デジトップが破産したで思うこと。こういうの見…

    5. 記録

      Netflixで「6アンダーグラウンド」を観ました。

      Netflixで「6アンダーグラウンド」を観ました。だいぶ前に途中ま…

    6. 記録

      データベースの事を語ってくれたある企業さん

      データベースの事を語ってくれたある企業さんの話は結構ためになりました…

    2018年12月
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  

    カテゴリー

    アーカイブ

    PAGE TOP