Guest User

Untitled

a guest
Dec 9th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 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.Net;
  10. using System.IO;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public delegate void DelegateUpdateProgressBar(int max);
  17.  
  18.         public event DelegateUpdateProgressBar EventUpdateProgressBar;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.             EventUpdateProgressBar += new DelegateUpdateProgressBar(UpdateProgressBarMethode);
  24.         }
  25.  
  26.         private void Form1_Load(object sender, EventArgs e)
  27.         {
  28.             CenterToScreen();
  29.         }
  30.  
  31.         private void button1_Click(object sender, EventArgs e)
  32.         {
  33.             string _host = HostBox.Text;
  34.             string _port = PortBox.Text;
  35.             string _username = UsernameBox.Text;
  36.             string _password = PasswordBox.Text;
  37.             string _filepath = FilepathBox.Text;
  38.             ResponseBox.Text += _filepath + ": " + FTPUpload(_host, _port, _username, _password, _filepath) + Environment.NewLine;
  39.         }
  40.  
  41.         public void UpdateProgressBarMethode(int max)
  42.         {
  43.             progressBar1.Maximum = max;
  44.             progressBar1.Step = 1;
  45.             progressBar1.PerformStep();
  46.         }
  47.  
  48.         private string FTPUpload(string _host, string _port, string _username, string _password, string _filepath)
  49.         {
  50.             string _rvalue = "";
  51.             //check if file exists on local machine
  52.             if (File.Exists(_filepath) == true)
  53.             {
  54.                 try
  55.                 {
  56.                     //create file information object
  57.                     FileInfo myFile = new FileInfo(_filepath);
  58.  
  59.                     //create a byte array as a buffer that contains the file content
  60.                     FileStream fs = myFile.OpenRead();
  61.                     byte[] file_buffer = new byte[fs.Length];
  62.                     fs.Read(file_buffer, 0, file_buffer.Length);
  63.                     fs.Close();
  64.  
  65.                     //create uniform ressouce identifier
  66.                     Uri _uri = new Uri("ftp://" + _host + ":" + _port + "/" + myFile.Name);
  67.  
  68.                     //create the ftp object
  69.                     FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(_uri);
  70.                     ftp.Credentials = new NetworkCredential(_username, _password);
  71.                     ftp.Method = WebRequestMethods.Ftp.UploadFile;
  72.  
  73.                     //check connection
  74.                     FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  75.                     _rvalue = response.StatusDescription;
  76.  
  77.                     //upload the buffer array by using the ftp upload stream
  78.                     Stream upload_stream = ftp.GetRequestStream();
  79.                     int max = file_buffer.Length;
  80.                     int start = 0;
  81.  
  82.                     //upload the stream
  83.                     while (start != max)
  84.                     {
  85.                         upload_stream.Write(file_buffer, start, 1);
  86.                         start++;
  87.                         EventUpdateProgressBar(max);
  88.                         Application.DoEvents();
  89.                     }
  90.  
  91.                     //close the stream
  92.                     upload_stream.Close();
  93.                 }
  94.                 catch (Exception e)
  95.                 {
  96.                     _rvalue = e.Message;
  97.                 }
  98.             }
  99.             else
  100.             {
  101.                 _rvalue = "file not found!";
  102.             }
  103.             return _rvalue;
  104.         }
  105.     }
  106. }
Add Comment
Please, Sign In to add comment