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