Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Net;
- using Newtonsoft.Json;
- namespace Repository
- {
- /// <summary>
- /// ToFtp is used to upload a local file to remote FTP.
- /// </summary>
- class ToFtp
- {
- /// <summary>
- /// Path to FTP configuration files
- /// </summary>
- private string _configurationPath = @"C:\Users\Public\Documents";
- private Credentials _creds = new Credentials();
- /// <summary>
- /// Initializer with hostname/ip, username and password as parameters
- /// </summary>
- /// <param name="ftpHost">hostname or IP, use "ftp://" as prefix</param>
- /// <param name="user">ftp user</param>
- /// <param name="pwd">ftp password</param>
- public ToFtp(string ftpHost, string user, string pwd)
- {
- _creds.FtpHost = ftpHost;
- _creds.UserName = user;
- _creds.PassWord = pwd;
- }
- /// <summary>
- /// Initializer which reads the FTP credentials from configuration file.
- /// </summary>
- /// <param name="fileName"></param>
- public ToFtp(string fileName)
- {
- ReadConfig(Path.Combine(_configurationPath, fileName));
- }
- public struct Credentials
- {
- public string FtpHost;
- public string UserName;
- public string PassWord;
- }
- /// <summary>
- /// Method for upload of local file to remote FTP
- /// </summary>
- /// <param name="path">Local path</param>
- /// <param name="filename">Local filename</param>
- public void Upload(string path, string filename)
- {
- string fileToUpload = Path.Combine(path, filename);
- try
- {
- var ftpWebRequest = (FtpWebRequest)WebRequest.Create(_creds.FtpHost + filename);
- ftpWebRequest.Credentials = new NetworkCredential(_creds.UserName, _creds.PassWord);
- ftpWebRequest.UseBinary = true;
- ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
- ftpWebRequest.Proxy = null;
- FileStream fs = File.OpenRead(fileToUpload);
- byte[] buffer = new byte[fs.Length];
- fs.Read(buffer, 0, buffer.Length);
- fs.Close();
- Stream ftpStream = ftpWebRequest.GetRequestStream();
- ftpStream.Write(buffer, 0, buffer.Length);
- ftpStream.Close();
- var ftpWebResponse = (FtpWebResponse) ftpWebRequest.GetResponse();
- Console.WriteLine(ftpWebResponse.StatusDescription);
- }
- catch (WebException e)
- {
- Console.WriteLine(e);
- }
- }
- /// <summary>
- /// This method will save the FTP credentials, serialized as Json
- /// </summary>
- /// <param name="fileName">Filename to save FTP credentials as</param>
- /// <returns>bool</returns>
- public bool SaveConfig(string fileName)
- {
- try
- {
- File.Delete(Path.Combine(_configurationPath, fileName));
- File.WriteAllText(Path.Combine(_configurationPath, fileName), JsonConvert.SerializeObject(_creds, Formatting.Indented));
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// This method is used when ToFTP is initialized to read FTP credentials from file
- /// </summary>
- /// <param name="path">This is the full path and filename to configuration file</param>
- /// <returns>bool</returns>
- private bool ReadConfig(string path)
- {
- try
- {
- _creds = JsonConvert.DeserializeObject<Credentials>(File.ReadAllText(path));
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement