Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.22 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows.Forms;
  4. using System.Net;// need to added
  5. using System.Threading; // need to added
  6. using System.IO;// need to added
  7.  
  8. namespace WindowsFormsApp2
  9. {
  10.  
  11.  
  12.  
  13.     public partial class FTP : Form
  14.     {
  15.         public string Username;
  16.         public string Filename;
  17.         public string Fullname;
  18.         public string Server;
  19.         public string Password;
  20.         public string path;
  21.         public string localdest;
  22.  
  23.         public FTP()
  24.         {
  25.             InitializeComponent();
  26.             if (checkdown.Checked == true)
  27.             {
  28.                 checkup.Enabled = false;
  29.             }
  30.          }
  31.  
  32.  
  33.         private void label1_Click(object sender, EventArgs e){}
  34.  
  35.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  36.         {
  37.  
  38.             if (checkdown.Checked == true)
  39.             {
  40.                 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", Server, Filename)));
  41.                    
  42.                 request.Credentials = new NetworkCredential(Username, Password);
  43.                 request.Method = WebRequestMethods.Ftp.DownloadFile;  //Download Method
  44.  
  45.  
  46.                 //Get some data form the source file like the zise and the TimeStamp. every data you request need to be a different request and response
  47.                 FtpWebRequest request1 = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", Server, Filename)));
  48.                 request1.Credentials = new NetworkCredential(Username, Password);
  49.                 request1.Method = WebRequestMethods.Ftp.GetFileSize;  //GetFileze Method
  50.                 FtpWebResponse response = (FtpWebResponse)request1.GetResponse();
  51.                 double total = response.ContentLength;
  52.                 response.Close();
  53.  
  54.                 FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", Server, Filename)));
  55.                 request2.Credentials = new NetworkCredential(Username, Password);
  56.                 request2.Method = WebRequestMethods.Ftp.GetDateTimestamp; //GetTimestamp Method
  57.                 FtpWebResponse response2 = (FtpWebResponse)request2.GetResponse();
  58.                 DateTime modify = response2.LastModified;
  59.                 response2.Close();
  60.  
  61.                
  62.                 Stream ftpstream = request.GetResponse().GetResponseStream();
  63.                 FileStream fs = new FileStream(localdest, FileMode.Create);
  64.  
  65.                 // Method to calculate and show the progress.
  66.                 byte[] buffer = new byte[1024];
  67.                 int byteRead = 0;
  68.                 double read = 0;
  69.                 do
  70.                 {
  71.                     byteRead = ftpstream.Read(buffer, 0, 1024);
  72.                     fs.Write(buffer, 0, byteRead);
  73.                     read += (double)byteRead;
  74.                     double percentage = read / total * 100;
  75.                     backgroundWorker1.ReportProgress((int)percentage);
  76.                 }
  77.                 while (byteRead != 0);
  78.                 ftpstream.Close();
  79.                 fs.Close();
  80.                
  81.  
  82.             }
  83.             else {
  84.  
  85.                 //Upload Method.
  86.                 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", Server, Filename)));
  87.                 request.Method = WebRequestMethods.Ftp.UploadFile;
  88.                 request.Credentials = new NetworkCredential(Username, Password);
  89.                 Stream ftpstream = request.GetRequestStream();
  90.                 FileStream fs = File.OpenRead(Fullname);
  91.  
  92.                 // Method to calculate and show the progress.
  93.                 byte[] buffer = new byte[1024];
  94.                 double total = (double)fs.Length;
  95.                 int byteRead = 0;
  96.                 double read = 0;
  97.                 do
  98.                 {
  99.                         byteRead = fs.Read(buffer, 0, 1024);
  100.                         ftpstream.Write(buffer, 0, byteRead);
  101.                         read += (double)byteRead;
  102.                         double percentage = read / total * 100;
  103.                         backgroundWorker1.ReportProgress((int)percentage);
  104.                 }
  105.                 while (byteRead != 0);
  106.                 fs.Close();
  107.                 ftpstream.Close();
  108.  
  109.             }
  110.         }
  111.  
  112.         //Background
  113.         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  114.         {
  115.  
  116.             if (checkdown.Checked == true)
  117.             {
  118.                 label4.Text = $"Downloaded {e.ProgressPercentage}%";
  119.                 label4.Update();
  120.                 progressBar1.Value = e.ProgressPercentage;
  121.                 progressBar1.Update();
  122.             }
  123.  
  124.             if (checkup.Checked == true)
  125.             {
  126.                 label4.Text = $"Uploaded {e.ProgressPercentage}%";
  127.                 label4.Update();
  128.                 progressBar1.Value = e.ProgressPercentage;
  129.                 progressBar1.Update();
  130.             }
  131.         }
  132.  
  133.         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  134.         {
  135.             if (checkdown.Checked == true)
  136.             {
  137.                 label4.Text = "Download Complete!";
  138.                 MessageBox.Show("Download Complete!");
  139.             }
  140.  
  141.             if (checkup.Checked == true)
  142.             {
  143.                 label4.Text = "Upload Complete!";
  144.                 MessageBox.Show("Upload Complete!");
  145.             }
  146.                
  147.         }
  148.  
  149.         private void Button1_Click(object sender, EventArgs e)
  150.         {
  151.             if (checkup.Checked == true)
  152.             {
  153.  
  154.                 using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = true, ValidateNames = true, Filter = "All Files|*.*" })
  155.                 {
  156.                     if (ofd.ShowDialog() == DialogResult.OK)
  157.                     {
  158.                         FileInfo fi = new FileInfo(ofd.FileName);
  159.                         Username = textBox1.Text;
  160.                         Password = textBox2.Text;
  161.                         Server = textBox3.Text;
  162.                         Filename = fi.Name;
  163.                         Fullname = fi.FullName;
  164.                     }
  165.                 }
  166.             }
  167.  
  168.  
  169.             if (checkdown.Checked == true)
  170.             {
  171.                 Username = textBox1.Text;
  172.                 Password = textBox2.Text;
  173.                 Server = textBox3.Text;
  174.                 Filename = textBox4.Text;
  175.                 path = @"C:\Users\jredondo\Documents\";
  176.                 localdest = path + @"" + Filename;
  177.                 Fullname = Server + @"/" + Filename;
  178.             }
  179.  
  180.             //Start the Background and wait a little to start it.
  181.             Thread.Sleep(1000);
  182.             backgroundWorker1.RunWorkerAsync();  //the most important command to start the background worker
  183.             Thread.Sleep(1000);
  184.         }
  185.  
  186.             private void textBox1_TextChanged(object sender, EventArgs e)
  187.         {
  188.  
  189.         }
  190.  
  191.         private void button2_Click(object sender, EventArgs e)
  192.         {
  193.             Application.Exit();
  194.         }
  195.  
  196.         private void checkBox1_CheckedChanged(object sender, EventArgs e)
  197.         {
  198.             if (checkdown.Checked == false)
  199.             {
  200.                 checkup.Enabled = true;
  201.                 checkup.Checked = true;
  202.                 button1.Text = @"Upload";
  203.                 label5.Enabled = false;
  204.                 textBox4.Enabled = false;
  205.                 checkdown.Enabled = false;
  206.                 label4.Text = @"Uploaded 0%";
  207.             }
  208.          
  209.         }
  210.  
  211.        private void checkup_CheckedChanged(object sender, EventArgs e)
  212.         {
  213.             if (checkup.Checked == false)
  214.             {
  215.                 checkdown.Enabled = true;
  216.                 checkdown.Checked = true;
  217.                 button1.Text = @"Download";
  218.                 label5.Enabled = true;
  219.                 textBox4.Enabled = true;
  220.                 checkup.Enabled = false;
  221.                 label4.Text = @"Downloaded 0%";
  222.             }
  223.            
  224.         }
  225.  
  226.         private void Form1_Load(object sender, EventArgs e)
  227.         {
  228.  
  229.         }
  230.  
  231.         private void textBox3_TextChanged(object sender, EventArgs e)
  232.         {
  233.  
  234.         }
  235.  
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement