Advertisement
Guest User

simple ftp Transferring modified

a guest
Dec 14th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9.  
  10. namespace ORFTPUpload.BusinessClass
  11. {
  12.     class ftp
  13.     {
  14.         private string host = null;
  15.         private string user = null;
  16.         private string pass = null;
  17.         private FtpWebRequest ftpRequest = null;
  18.         private FtpWebResponse ftpResponse = null;
  19.         private Stream ftpStream = null;
  20.         private int bufferSize = 2048;
  21.  
  22.         /* Construct Object */
  23.         public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }
  24.  
  25.         /* Download File */
  26.         public void download(string remoteFile, string localFile)
  27.         {
  28.             try
  29.             {
  30.                 /* Create an FTP Request */
  31.                 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
  32.                 /* Log in to the FTP Server with the User Name and Password Provided */
  33.                 ftpRequest.Credentials = new NetworkCredential(user, pass);
  34.                 /* When in doubt, use these options */
  35.                 ftpRequest.UseBinary = true;
  36.                 ftpRequest.UsePassive = true;
  37.                 ftpRequest.KeepAlive = true;
  38.                 /* Specify the Type of FTP Request */
  39.                 ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  40.                 /* Establish Return Communication with the FTP Server */
  41.                 ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  42.                 /* Get the FTP Server's Response Stream */
  43.                 ftpStream = ftpResponse.GetResponseStream();
  44.                 /* Open a File Stream to Write the Downloaded File */
  45.                 FileStream localFileStream = new FileStream(localFile, FileMode.Create);
  46.                 /* Buffer for the Downloaded Data */
  47.                 byte[] byteBuffer = new byte[bufferSize];
  48.                 int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  49.                 /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
  50.                 try
  51.                 {
  52.                     while (bytesRead > 0)
  53.                     {
  54.                         localFileStream.Write(byteBuffer, 0, bytesRead);
  55.                         bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  56.                     }
  57.                 }
  58.                 catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  59.                 /* Resource Cleanup */
  60.                 localFileStream.Close();
  61.                 ftpStream.Close();
  62.                 ftpResponse.Close();
  63.                 ftpRequest = null;
  64.             }
  65.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  66.             return;
  67.         }
  68.  
  69.         /* Upload File */
  70.         public void upload(string remoteFile, string localFile)
  71.         {
  72.             try
  73.             {
  74.                 /* Create an FTP Request */
  75.                 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
  76.                 /* Log in to the FTP Server with the User Name and Password Provided */
  77.                 ftpRequest.Credentials = new NetworkCredential(user, pass);
  78.                 /* When in doubt, use these options */
  79.                 ftpRequest.UseBinary = true;
  80.                 ftpRequest.UsePassive = true;
  81.                 ftpRequest.KeepAlive = true;
  82.                 /* Specify the Type of FTP Request */
  83.                 ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
  84.                 /* Establish Return Communication with the FTP Server */
  85.                 ftpStream = ftpRequest.GetRequestStream();
  86.                 /* Open a File Stream to Read the File for Upload */
  87.                 FileStream localFileStream = new FileStream(localFile, FileMode.Open);
  88.                 /* Buffer for the Downloaded Data */
  89.                 byte[] byteBuffer = new byte[bufferSize];
  90.                 int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
  91.                 /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
  92.                 try
  93.                 {
  94.                     while (bytesSent != 0)
  95.                     {
  96.                         ftpStream.Write(byteBuffer, 0, bytesSent);
  97.                         bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
  98.                     }
  99.                 }
  100.                 catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  101.                 /* Resource Cleanup */
  102.                 localFileStream.Close();
  103.                 ftpStream.Close();
  104.                 ftpRequest = null;
  105.             }
  106.             catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  107.             return;
  108.         }
  109.  
  110.         /* Delete File */
  111.         public void delete(string deleteFile)
  112.         {
  113.             try
  114.             {
  115.                 /* Create an FTP Request */
  116.                 ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
  117.                 /* Log in to the FTP Server with the User Name and Password Provided */
  118.                 ftpRequest.Credentials = new NetworkCredential(user, pass);
  119.                 /* When in doubt, use these options */
  120.                 ftpRequest.UseBinary = true;
  121.                 ftpRequest.UsePassive = true;
  122.                 ftpRequest.KeepAlive = true;
  123.                 /* Specify the Type of FTP Request */
  124.                 ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
  125.                 /* Establish Return Communication with the FTP Server */
  126.                 ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  127.                 /* Resource Cleanup */
  128.                 ftpResponse.Close();
  129.                 ftpRequest = null;
  130.             }
  131.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  132.             return;
  133.         }
  134.  
  135.         /* Rename File */
  136.         public void rename(string currentFileNameAndPath, string newFileName)
  137.         {
  138.             try
  139.             {
  140.                 /* Create an FTP Request */
  141.                 ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + currentFileNameAndPath);
  142.                 /* Log in to the FTP Server with the User Name and Password Provided */
  143.                 ftpRequest.Credentials = new NetworkCredential(user, pass);
  144.                 /* When in doubt, use these options */
  145.                 ftpRequest.UseBinary = true;
  146.                 ftpRequest.UsePassive = true;
  147.                 ftpRequest.KeepAlive = true;
  148.                 /* Specify the Type of FTP Request */
  149.                 ftpRequest.Method = WebRequestMethods.Ftp.Rename;
  150.                 /* Rename the File */
  151.                 ftpRequest.RenameTo = newFileName;
  152.                 /* Establish Return Communication with the FTP Server */
  153.                 ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  154.                 /* Resource Cleanup */
  155.                 ftpResponse.Close();
  156.                 ftpRequest = null;
  157.             }
  158.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  159.             return;
  160.         }
  161.  
  162.         /* Create a New Directory on the FTP Server */
  163.         public void createDirectory(string newDirectory)
  164.         {
  165.             try
  166.             {
  167.                 /* Create an FTP Request */
  168.                 ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
  169.                 /* Log in to the FTP Server with the User Name and Password Provided */
  170.                 ftpRequest.Credentials = new NetworkCredential(user, pass);
  171.                 /* When in doubt, use these options */
  172.                 ftpRequest.UseBinary = true;
  173.                 ftpRequest.UsePassive = true;
  174.                 ftpRequest.KeepAlive = true;
  175.                 /* Specify the Type of FTP Request */
  176.                 ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
  177.                 /* Establish Return Communication with the FTP Server */
  178.                 ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  179.                 /* Resource Cleanup */
  180.                 ftpResponse.Close();
  181.                 ftpRequest = null;
  182.             }
  183.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  184.             return;
  185.         }
  186.  
  187.         /* Get the Date/Time a File was Created */
  188.         public string getFileCreatedDateTime(string fileName)
  189.         {
  190.             try
  191.             {
  192.                 /* Create an FTP Request */
  193.                 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
  194.                 /* Log in to the FTP Server with the User Name and Password Provided */
  195.                 ftpRequest.Credentials = new NetworkCredential(user, pass);
  196.                 /* When in doubt, use these options */
  197.                 ftpRequest.UseBinary = true;
  198.                 ftpRequest.UsePassive = true;
  199.                 ftpRequest.KeepAlive = true;
  200.                 /* Specify the Type of FTP Request */
  201.                 ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
  202.                 /* Establish Return Communication with the FTP Server */
  203.                 ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  204.                 /* Establish Return Communication with the FTP Server */
  205.                 ftpStream = ftpResponse.GetResponseStream();
  206.                 /* Get the FTP Server's Response Stream */
  207.                 StreamReader ftpReader = new StreamReader(ftpStream);
  208.                 /* Store the Raw Response */
  209.                 string fileInfo = null;
  210.                 /* Read the Full Response Stream */
  211.                 try { fileInfo = ftpReader.ReadToEnd(); }
  212.                 catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  213.                 /* Resource Cleanup */
  214.                 ftpReader.Close();
  215.                 ftpStream.Close();
  216.                 ftpResponse.Close();
  217.                 ftpRequest = null;
  218.                 /* Return File Created Date Time */
  219.                 return fileInfo;
  220.             }
  221.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  222.             /* Return an Empty string Array if an Exception Occurs */
  223.             return "";
  224.         }
  225.  
  226.         /* Get the Size of a File */
  227.         public string getFileSize(string fileName)
  228.         {
  229.             try
  230.             {
  231.                 /* Create an FTP Request */
  232.                 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
  233.                 /* Log in to the FTP Server with the User Name and Password Provided */
  234.                 ftpRequest.Credentials = new NetworkCredential(user, pass);
  235.                 /* When in doubt, use these options */
  236.                 ftpRequest.UseBinary = true;
  237.                 ftpRequest.UsePassive = true;
  238.                 ftpRequest.KeepAlive = true;
  239.                 /* Specify the Type of FTP Request */
  240.                 ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
  241.                 /* Establish Return Communication with the FTP Server */
  242.                 ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  243.                 /* Establish Return Communication with the FTP Server */
  244.                 ftpStream = ftpResponse.GetResponseStream();
  245.                 /* Get the FTP Server's Response Stream */
  246.                 StreamReader ftpReader = new StreamReader(ftpStream);
  247.                 /* Store the Raw Response */
  248.                 string fileInfo = null;
  249.                 /* Read the Full Response Stream */
  250.                 try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } }
  251.                 catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  252.                 /* Resource Cleanup */
  253.                 ftpReader.Close();
  254.                 ftpStream.Close();
  255.                 ftpResponse.Close();
  256.                 ftpRequest = null;
  257.                 /* Return File Size */
  258.                 return fileInfo;
  259.             }
  260.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  261.             /* Return an Empty string Array if an Exception Occurs */
  262.             return "";
  263.         }
  264.  
  265.         /* List Directory Contents File/Folder Name Only */
  266.         public List<string> getdirectoryContentSimple(string directory)
  267.         {
  268.             try
  269.             {
  270.                 List<string> filesfromFTP = new List<string>();
  271.  
  272.                 string line = string.Empty;
  273.                 string data = string.Empty;
  274.  
  275.                 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(host + "/" + directory);
  276.                 request.Method = WebRequestMethods.Ftp.ListDirectory;
  277.  
  278.                 // This example assumes the FTP site uses anonymous logon.  
  279.                 request.Credentials = new NetworkCredential(user, pass);
  280.  
  281.                 FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  282.  
  283.                 //    Stream responseStream = response.GetResponseStream();
  284.                 //StreamReader reader = new StreamReader(responseStream);
  285.                 using (Stream responseStream = response.GetResponseStream())
  286.                 {
  287.                     using (StreamReader reader = new StreamReader(responseStream))
  288.                     {
  289.                         line = reader.ReadLine();
  290.  
  291.                         while (line != null)
  292.                         {
  293.                             filesfromFTP.Add(line);
  294.                             line = reader.ReadLine();
  295.                         }
  296.                     }
  297.                 }
  298.  
  299.  
  300.                 foreach (var filename in filesfromFTP)
  301.                 {
  302.                     data += filename + ", ";
  303.                 }
  304.                 Console.WriteLine(data);
  305.  
  306.                 Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
  307.  
  308.  
  309.                 return filesfromFTP;
  310.  
  311.             }
  312.  
  313.             catch (Exception ex)
  314.             {
  315.                 MessageBox.Show("Cannot get ftp server listing. Contact admin");
  316.  
  317.                 return null;
  318.             }
  319.         }
  320.  
  321.         //get detailed content of directory
  322.         public List<string> getdirectoryContentDetailed(string directory)
  323.         {
  324.             try
  325.             {
  326.                 List<string> filesfromFTP = new List<string>();
  327.  
  328.                 string line = string.Empty;
  329.                 string data = string.Empty;
  330.  
  331.                 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(host + "/" + directory);
  332.                 request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  333.  
  334.                 // This example assumes the FTP site uses anonymous logon.  
  335.                 request.Credentials = new NetworkCredential(user, pass);
  336.  
  337.                 FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  338.  
  339.                 //    Stream responseStream = response.GetResponseStream();
  340.                 //StreamReader reader = new StreamReader(responseStream);
  341.                 using (Stream responseStream = response.GetResponseStream())
  342.                 {
  343.                     using (StreamReader reader = new StreamReader(responseStream))
  344.                     {
  345.                         line = reader.ReadLine();
  346.  
  347.                         while (line != null)
  348.                         {
  349.                             filesfromFTP.Add(line);
  350.                             line = reader.ReadLine();
  351.                         }
  352.                     }
  353.                 }
  354.  
  355.                 foreach (var filename in filesfromFTP)
  356.                 {
  357.                     data += filename + ", ";
  358.                 }
  359.                 Console.WriteLine(data);
  360.  
  361.                 Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
  362.  
  363.  
  364.                 return filesfromFTP;
  365.  
  366.             }
  367.  
  368.             catch (Exception ex)
  369.             {
  370.                 MessageBox.Show("Cannot get detail ftp server listing. Contact admin");
  371.  
  372.                 return null;
  373.             }
  374.         }
  375.     }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement