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.Net;
- using System.IO;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public delegate void DelegateUpdateProgressBar(int max);
- public event DelegateUpdateProgressBar EventUpdateProgressBar;
- public Form1()
- {
- InitializeComponent();
- EventUpdateProgressBar += new DelegateUpdateProgressBar(UpdateProgressBarMethode);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- CenterToScreen();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string _host = HostBox.Text;
- string _port = PortBox.Text;
- string _username = UsernameBox.Text;
- string _password = PasswordBox.Text;
- string _filepath = FilepathBox.Text;
- ResponseBox.Text += _filepath + ": " + FTPUpload(_host, _port, _username, _password, _filepath) + Environment.NewLine;
- }
- public void UpdateProgressBarMethode(int max)
- {
- progressBar1.Maximum = max;
- progressBar1.Step = 1;
- progressBar1.PerformStep();
- }
- private string FTPUpload(string _host, string _port, string _username, string _password, string _filepath)
- {
- string _rvalue = "";
- //check if file exists on local machine
- if (File.Exists(_filepath) == true)
- {
- try
- {
- //create file information object
- FileInfo myFile = new FileInfo(_filepath);
- //create a byte array as a buffer that contains the file content
- FileStream fs = myFile.OpenRead();
- byte[] file_buffer = new byte[fs.Length];
- fs.Read(file_buffer, 0, file_buffer.Length);
- fs.Close();
- //create uniform ressouce identifier
- Uri _uri = new Uri("ftp://" + _host + ":" + _port + "/" + myFile.Name);
- //create the ftp object
- FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(_uri);
- ftp.Credentials = new NetworkCredential(_username, _password);
- ftp.Method = WebRequestMethods.Ftp.UploadFile;
- //check connection
- FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
- _rvalue = response.StatusDescription;
- //upload the buffer array by using the ftp upload stream
- Stream upload_stream = ftp.GetRequestStream();
- int max = file_buffer.Length;
- int start = 0;
- //upload the stream
- while (start != max)
- {
- upload_stream.Write(file_buffer, start, 1);
- start++;
- EventUpdateProgressBar(max);
- Application.DoEvents();
- }
- //close the stream
- upload_stream.Close();
- }
- catch (Exception e)
- {
- _rvalue = e.Message;
- }
- }
- else
- {
- _rvalue = "file not found!";
- }
- return _rvalue;
- }
- }
- }
Add Comment
Please, Sign In to add comment