Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Collections;
- using System.Net;
- namespace FTP_Client
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void attachButton_Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Multiselect = true;
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- foreach (string file in ofd.FileNames)
- {
- ListViewItem item = new ListViewItem(file);
- FileInfo info = new FileInfo(file);
- fileListView.Items.Add(item);
- item.SubItems.Add(string.Format("{0:N0}KB", info.Length / 1024));
- }
- }
- }
- private void uploadFileButton_Click(object sender, EventArgs e)
- {
- string host = hostTextBox.Text;
- string username = usernameTextBox.Text;
- string password = passwordTextBox.Text;
- List<string> filesForUpload = new List<string>();
- foreach (ListViewItem item in fileListView.Items)
- {
- filesForUpload.Add(item.Text);
- }
- if (!host.StartsWith("ftp://"))
- {
- host = "ftp://" + host;
- }
- FtpUpload(host, username,
- password, filesForUpload);
- MessageBox.Show("File uploaded");
- }
- private void FtpUpload(string host, string username,
- string password, ICollection<string> attachments)
- {
- foreach (string file in attachments)
- {
- try
- {
- Uri uri = new Uri(host);
- WebClient client = new WebClient();
- client.Credentials = new NetworkCredential(username, password);
- FileInfo info = new FileInfo(file);
- string destFileName = host + "/" + info.Name;
- byte[] response = client.UploadFile(destFileName, file);
- if (response.Length > 0)
- {
- MessageBox.Show("Response: {0}", Encoding.ASCII.GetString(response));
- }
- }
- catch (WebException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment