Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace ORFTPUpload.BusinessClass
- {
- class ftp
- {
- private string host = null;
- private string user = null;
- private string pass = null;
- private FtpWebRequest ftpRequest = null;
- private FtpWebResponse ftpResponse = null;
- private Stream ftpStream = null;
- private int bufferSize = 2048;
- /* Construct Object */
- public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }
- /* Download File */
- public void download(string remoteFile, string localFile)
- {
- try
- {
- /* Create an FTP Request */
- ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
- /* Log in to the FTP Server with the User Name and Password Provided */
- ftpRequest.Credentials = new NetworkCredential(user, pass);
- /* When in doubt, use these options */
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = true;
- ftpRequest.KeepAlive = true;
- /* Specify the Type of FTP Request */
- ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
- /* Establish Return Communication with the FTP Server */
- ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
- /* Get the FTP Server's Response Stream */
- ftpStream = ftpResponse.GetResponseStream();
- /* Open a File Stream to Write the Downloaded File */
- FileStream localFileStream = new FileStream(localFile, FileMode.Create);
- /* Buffer for the Downloaded Data */
- byte[] byteBuffer = new byte[bufferSize];
- int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
- /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
- try
- {
- while (bytesRead > 0)
- {
- localFileStream.Write(byteBuffer, 0, bytesRead);
- bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
- }
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- /* Resource Cleanup */
- localFileStream.Close();
- ftpStream.Close();
- ftpResponse.Close();
- ftpRequest = null;
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- return;
- }
- /* Upload File */
- public void upload(string remoteFile, string localFile)
- {
- try
- {
- /* Create an FTP Request */
- ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
- /* Log in to the FTP Server with the User Name and Password Provided */
- ftpRequest.Credentials = new NetworkCredential(user, pass);
- /* When in doubt, use these options */
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = true;
- ftpRequest.KeepAlive = true;
- /* Specify the Type of FTP Request */
- ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
- /* Establish Return Communication with the FTP Server */
- ftpStream = ftpRequest.GetRequestStream();
- /* Open a File Stream to Read the File for Upload */
- FileStream localFileStream = new FileStream(localFile, FileMode.Open);
- /* Buffer for the Downloaded Data */
- byte[] byteBuffer = new byte[bufferSize];
- int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
- /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
- try
- {
- while (bytesSent != 0)
- {
- ftpStream.Write(byteBuffer, 0, bytesSent);
- bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
- }
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- /* Resource Cleanup */
- localFileStream.Close();
- ftpStream.Close();
- ftpRequest = null;
- }
- catch (Exception ex) { MessageBox.Show(ex.ToString()); }
- return;
- }
- /* Delete File */
- public void delete(string deleteFile)
- {
- try
- {
- /* Create an FTP Request */
- ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
- /* Log in to the FTP Server with the User Name and Password Provided */
- ftpRequest.Credentials = new NetworkCredential(user, pass);
- /* When in doubt, use these options */
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = true;
- ftpRequest.KeepAlive = true;
- /* Specify the Type of FTP Request */
- ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
- /* Establish Return Communication with the FTP Server */
- ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
- /* Resource Cleanup */
- ftpResponse.Close();
- ftpRequest = null;
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- return;
- }
- /* Rename File */
- public void rename(string currentFileNameAndPath, string newFileName)
- {
- try
- {
- /* Create an FTP Request */
- ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + currentFileNameAndPath);
- /* Log in to the FTP Server with the User Name and Password Provided */
- ftpRequest.Credentials = new NetworkCredential(user, pass);
- /* When in doubt, use these options */
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = true;
- ftpRequest.KeepAlive = true;
- /* Specify the Type of FTP Request */
- ftpRequest.Method = WebRequestMethods.Ftp.Rename;
- /* Rename the File */
- ftpRequest.RenameTo = newFileName;
- /* Establish Return Communication with the FTP Server */
- ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
- /* Resource Cleanup */
- ftpResponse.Close();
- ftpRequest = null;
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- return;
- }
- /* Create a New Directory on the FTP Server */
- public void createDirectory(string newDirectory)
- {
- try
- {
- /* Create an FTP Request */
- ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
- /* Log in to the FTP Server with the User Name and Password Provided */
- ftpRequest.Credentials = new NetworkCredential(user, pass);
- /* When in doubt, use these options */
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = true;
- ftpRequest.KeepAlive = true;
- /* Specify the Type of FTP Request */
- ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
- /* Establish Return Communication with the FTP Server */
- ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
- /* Resource Cleanup */
- ftpResponse.Close();
- ftpRequest = null;
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- return;
- }
- /* Get the Date/Time a File was Created */
- public string getFileCreatedDateTime(string fileName)
- {
- try
- {
- /* Create an FTP Request */
- ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
- /* Log in to the FTP Server with the User Name and Password Provided */
- ftpRequest.Credentials = new NetworkCredential(user, pass);
- /* When in doubt, use these options */
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = true;
- ftpRequest.KeepAlive = true;
- /* Specify the Type of FTP Request */
- ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
- /* Establish Return Communication with the FTP Server */
- ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
- /* Establish Return Communication with the FTP Server */
- ftpStream = ftpResponse.GetResponseStream();
- /* Get the FTP Server's Response Stream */
- StreamReader ftpReader = new StreamReader(ftpStream);
- /* Store the Raw Response */
- string fileInfo = null;
- /* Read the Full Response Stream */
- try { fileInfo = ftpReader.ReadToEnd(); }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- /* Resource Cleanup */
- ftpReader.Close();
- ftpStream.Close();
- ftpResponse.Close();
- ftpRequest = null;
- /* Return File Created Date Time */
- return fileInfo;
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- /* Return an Empty string Array if an Exception Occurs */
- return "";
- }
- /* Get the Size of a File */
- public string getFileSize(string fileName)
- {
- try
- {
- /* Create an FTP Request */
- ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
- /* Log in to the FTP Server with the User Name and Password Provided */
- ftpRequest.Credentials = new NetworkCredential(user, pass);
- /* When in doubt, use these options */
- ftpRequest.UseBinary = true;
- ftpRequest.UsePassive = true;
- ftpRequest.KeepAlive = true;
- /* Specify the Type of FTP Request */
- ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
- /* Establish Return Communication with the FTP Server */
- ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
- /* Establish Return Communication with the FTP Server */
- ftpStream = ftpResponse.GetResponseStream();
- /* Get the FTP Server's Response Stream */
- StreamReader ftpReader = new StreamReader(ftpStream);
- /* Store the Raw Response */
- string fileInfo = null;
- /* Read the Full Response Stream */
- try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- /* Resource Cleanup */
- ftpReader.Close();
- ftpStream.Close();
- ftpResponse.Close();
- ftpRequest = null;
- /* Return File Size */
- return fileInfo;
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- /* Return an Empty string Array if an Exception Occurs */
- return "";
- }
- /* List Directory Contents File/Folder Name Only */
- public List<string> getdirectoryContentSimple(string directory)
- {
- try
- {
- List<string> filesfromFTP = new List<string>();
- string line = string.Empty;
- string data = string.Empty;
- FtpWebRequest request = (FtpWebRequest)WebRequest.Create(host + "/" + directory);
- request.Method = WebRequestMethods.Ftp.ListDirectory;
- // This example assumes the FTP site uses anonymous logon.
- request.Credentials = new NetworkCredential(user, pass);
- FtpWebResponse response = (FtpWebResponse)request.GetResponse();
- // Stream responseStream = response.GetResponseStream();
- //StreamReader reader = new StreamReader(responseStream);
- using (Stream responseStream = response.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(responseStream))
- {
- line = reader.ReadLine();
- while (line != null)
- {
- filesfromFTP.Add(line);
- line = reader.ReadLine();
- }
- }
- }
- foreach (var filename in filesfromFTP)
- {
- data += filename + ", ";
- }
- Console.WriteLine(data);
- Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
- return filesfromFTP;
- }
- catch (Exception ex)
- {
- MessageBox.Show("Cannot get ftp server listing. Contact admin");
- return null;
- }
- }
- //get detailed content of directory
- public List<string> getdirectoryContentDetailed(string directory)
- {
- try
- {
- List<string> filesfromFTP = new List<string>();
- string line = string.Empty;
- string data = string.Empty;
- FtpWebRequest request = (FtpWebRequest)WebRequest.Create(host + "/" + directory);
- request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
- // This example assumes the FTP site uses anonymous logon.
- request.Credentials = new NetworkCredential(user, pass);
- FtpWebResponse response = (FtpWebResponse)request.GetResponse();
- // Stream responseStream = response.GetResponseStream();
- //StreamReader reader = new StreamReader(responseStream);
- using (Stream responseStream = response.GetResponseStream())
- {
- using (StreamReader reader = new StreamReader(responseStream))
- {
- line = reader.ReadLine();
- while (line != null)
- {
- filesfromFTP.Add(line);
- line = reader.ReadLine();
- }
- }
- }
- foreach (var filename in filesfromFTP)
- {
- data += filename + ", ";
- }
- Console.WriteLine(data);
- Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
- return filesfromFTP;
- }
- catch (Exception ex)
- {
- MessageBox.Show("Cannot get detail ftp server listing. Contact admin");
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement