Skip to content

Instantly share code, notes, and snippets.

@maxkoshevoi
Created October 13, 2019 22:23
Show Gist options
  • Save maxkoshevoi/2c662aa63646ef3fa4a068d9f30bea7c to your computer and use it in GitHub Desktop.
Save maxkoshevoi/2c662aa63646ef3fa4a068d9f30bea7c to your computer and use it in GitHub Desktop.
Working with FTP using .Net
using System;
using System.IO;
using System.Net;
class Ftp
{
private readonly string server, login, password;
public Ftp(string server, string login, string password)
{
this.server = server;
this.login = login;
this.password = password;
}
public void UploadFile(string uploadToFilePath, string filePath, bool throwErrorrs = true)
{
DeleteFile(uploadToFilePath, false);
uploadToFilePath = GetPath(uploadToFilePath);
try
{
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(login, password);
client.UploadFile(uploadToFilePath, "STOR", filePath);
}
}
catch
{
if (throwErrorrs)
{
throw;
}
}
}
public FtpWebResponse DeleteFile(string filePath, bool throwErrorrs = true)
{
filePath = GetPath(filePath);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(filePath);
request.Credentials = new NetworkCredential(login, password);
request.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse)request.GetResponse();
}
catch
{
if (throwErrorrs)
{
throw;
}
}
return response;
}
public FtpWebResponse CreateDirectory(string newDirPath, bool throwErrorrs = true) => CreateDirectory(newDirPath, throwErrorrs, true);
private FtpWebResponse CreateDirectory(string newDirPath, bool throwErrorrs = true, bool recursive = true)
{
string[] dirs = newDirPath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
if (dirs.Length > 1 && recursive)
{
FtpWebResponse result = null;
string path = "";
foreach (string dir in dirs)
{
path = Path.Combine(path, dir);
result = CreateDirectory(path, true, false);
}
return result;
}
newDirPath = GetPath(newDirPath);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(newDirPath);
request.Credentials = new NetworkCredential(login, password);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse)request.GetResponse();
}
catch
{
if (throwErrorrs)
{
throw;
}
}
return response;
}
public FtpWebResponse RemoveDirectory(string dirPath, bool throwErrorrs = true)
{
dirPath = GetPath(dirPath);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);
request.Credentials = new NetworkCredential(login, password);
request.Method = WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse)request.GetResponse();
}
catch
{
if (throwErrorrs)
{
throw;
}
}
return response;
}
public void UploadDirectory(string uploadToDirPath, string sourceDirPath, bool throwErrorrs = true)
{
string[] files = Directory.GetFiles(sourceDirPath, "*.*");
string[] subDirs = Directory.GetDirectories(sourceDirPath);
foreach (string file in files)
{
UploadFile(Path.Combine(uploadToDirPath, Path.GetFileName(file)), file, throwErrorrs);
}
foreach (string subDir in subDirs)
{
//CreateDirectory(Path.Combine(uploadToDirPath, Path.GetFileName(subDir)), false, false);
UploadDirectory(Path.Combine(uploadToDirPath, Path.GetFileName(subDir)), subDir, throwErrorrs);
}
}
private string GetPath(string relativePath)
{
string absolutePath = Path.Combine(server, relativePath).Replace('\\', '/');
return absolutePath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment