HayCZ

Untitled

Apr 8th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.IO;
  10.  
  11.  
  12.  
  13. namespace FTPUploader
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         bool secure = false;
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void uploadFile(string FTPAddress, string filePath, string username, string password)
  24.         {
  25.             FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + Path.GetFileName(filePath));
  26.             if (secure == true)
  27.             {
  28.                 request.EnableSsl = true;
  29.             }
  30.             else
  31.             {
  32.                 request.EnableSsl = false;
  33.             }
  34.             request.Method = WebRequestMethods.Ftp.UploadFile;
  35.             request.Credentials = new NetworkCredential(username, password);
  36.             request.UsePassive = true;
  37.             request.UseBinary = true;
  38.             request.KeepAlive = false;
  39.  
  40.             //Načtení souborů
  41.             FileStream stream = File.OpenRead(filePath);
  42.             byte[] buffer = new byte[stream.Length];
  43.  
  44.             stream.Read(buffer, 0, buffer.Length);
  45.             stream.Close();
  46.  
  47.             //Nahrání souboru
  48.             Stream reqStream = request.GetRequestStream();
  49.             reqStream.Write(buffer, 0, buffer.Length);
  50.             reqStream.Close();
  51.         }
  52.         private void allFiles(object sender, EventArgs e)
  53.         {
  54.             try
  55.             {
  56.                 DirectoryInfo d = new DirectoryInfo(txtFilePath.Text);//Cesta do složky
  57.                 FileInfo[] Files = d.GetFiles(); //Načtení souborů v dir
  58.                 int i = 0;
  59.                 foreach (FileInfo file in Files)
  60.                 {
  61.                     i++;
  62.                     Application.DoEvents();
  63.                     uploadFile(txtFTPAddress.Text, txtFilePath.Text + "/" + file.Name, txtUsername.Text, txtPassword.Text);
  64.                 }
  65.                 notifyIcon1.ShowBalloonTip(100, "Upload FTP", "Upload byl úspěšně dokončen! Bylo nahráno: " + i.ToString(), ToolTipIcon.Info);
  66.             }
  67.             catch(Exception s)
  68.             {
  69.                 MessageBox.Show(s.ToString());
  70.             }
  71.  
  72.  
  73.         }
  74.  
  75.  
  76.         private void txtFTPAddress_Leave(object sender, EventArgs e)
  77.         {
  78.             if (!txtFTPAddress.Text.StartsWith("ftp://"))
  79.                 txtFTPAddress.Text = "ftp://" + txtFTPAddress.Text;
  80.         }
  81.  
  82.         private void button1_Click(object sender, EventArgs e)
  83.         {
  84.             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  85.             {
  86.                 txtFilePath.Text = folderBrowserDialog1.SelectedPath.ToString();
  87.             }
  88.         }
  89.         private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
  90.         {
  91.             this.WindowState = FormWindowState.Normal;
  92.             this.ShowInTaskbar = true;
  93.             notifyIcon1.Visible = false;
  94.         }
  95.  
  96.         private void showToolStripMenuItem_Click(object sender, EventArgs e)
  97.         {
  98.             showForm();
  99.         }
  100.  
  101.         private void Form1_Move(object sender, EventArgs e)
  102.         {
  103.             if (this.WindowState == FormWindowState.Minimized )
  104.             {
  105.                 this.Hide();
  106.                 notifyIcon1.ShowBalloonTip(100, "UploadToFTP", "Aplikace je spuštěna na pozadí.",ToolTipIcon.Info);
  107.             }
  108.         }
  109.  
  110.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  111.         {
  112.             allFiles(sender, e);
  113.         }
  114.         private void showForm()
  115.         {
  116.             this.Show();
  117.             this.WindowState = FormWindowState.Normal;
  118.         }
  119.  
  120.         private void btnUpload_KeyDown(object sender, KeyEventArgs e)
  121.         {
  122.             if (e.Control && e.KeyCode.ToString() == "U")
  123.             {
  124.                 allFiles(sender, e);
  125.             }
  126.         }
  127.  
  128.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  129.         {
  130.             this.Close();
  131.         }
  132.  
  133.         private void checkBox1_CheckedChanged(object sender, EventArgs e)
  134.         {
  135.             secure = checkBox1.Checked;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment