Guest User

Untitled

a guest
Aug 25th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 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.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Net;
  12.  
  13. namespace FTP_Client
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void attachButton_Click(object sender, EventArgs e)
  23.         {
  24.             OpenFileDialog ofd = new OpenFileDialog();
  25.             ofd.Multiselect = true;
  26.             if (ofd.ShowDialog() == DialogResult.OK)
  27.             {
  28.                 foreach (string file in ofd.FileNames)
  29.                 {
  30.                     ListViewItem item = new ListViewItem(file);
  31.                     FileInfo info = new FileInfo(file);
  32.                     fileListView.Items.Add(item);
  33.                     item.SubItems.Add(string.Format("{0:N0}KB", info.Length / 1024));
  34.                 }
  35.             }
  36.         }
  37.  
  38.         private void uploadFileButton_Click(object sender, EventArgs e)
  39.         {
  40.             string host = hostTextBox.Text;
  41.             string username = usernameTextBox.Text;
  42.             string password = passwordTextBox.Text;
  43.             List<string> filesForUpload = new List<string>();
  44.             foreach (ListViewItem item in fileListView.Items)
  45.             {
  46.                 filesForUpload.Add(item.Text);
  47.             }
  48.             if (!host.StartsWith("ftp://"))
  49.             {
  50.                 host = "ftp://" + host;
  51.             }
  52.             FtpUpload(host, username,
  53.                 password, filesForUpload);
  54.             MessageBox.Show("File uploaded");
  55.         }
  56.         private void FtpUpload(string host, string username,
  57.             string password, ICollection<string> attachments)
  58.         {
  59.             foreach (string file in attachments)
  60.             {
  61.                 try
  62.                 {
  63.                     Uri uri = new Uri(host);
  64.                     WebClient client = new WebClient();
  65.                     client.Credentials = new NetworkCredential(username, password);
  66.                     FileInfo info = new FileInfo(file);
  67.                     string destFileName = host + "/" + info.Name;
  68.                     byte[] response = client.UploadFile(destFileName, file);
  69.                     if (response.Length > 0)
  70.                     {
  71.                         MessageBox.Show("Response: {0}", Encoding.ASCII.GetString(response));
  72.                     }
  73.                 }
  74.                 catch (WebException ex)
  75.                 {
  76.                     MessageBox.Show(ex.Message);
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }
Add Comment
Please, Sign In to add comment