/// <summary> /// Create directory /// </summary> /// <param name="pathToCreate"></param> public void CreateDir(FTPPath pathToCreate) { FtpWebRequest reqFTP = null; Stream ftpStream = null; FTPPath currentDir = new FTPPath(); foreach (string subDir in pathToCreate.Vals) { try { if (currentDir != "") { currentDir.Vals.Add(subDir); } else { currentDir = subDir; } reqFTP = GetRequest(WebRequestMethods.Ftp.MakeDirectory, currentDir); reqFTP.UseBinary = true; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); ftpStream = response.GetResponseStream(); ftpStream.Close(); response.Close(); } catch { } currentDir += "/"; } }
/// <summary> /// Get list of al content in a folder /// </summary> /// <param name="sub"></param> /// <returns></returns> public List <string> AllContents(params string[] Listpaths) { FTPPath paths = (Listpaths); FtpWebRequest request = GetRequest(WebRequestMethods.Ftp.ListDirectory, paths); try { var res = new List <string>(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); var tuttiassieme = reader.ReadToEnd(); var cartelle = tuttiassieme.Split('\r', '\n').Where(rr => !string.IsNullOrWhiteSpace(rr)); foreach (var item in cartelle) { var path = item.Split('/', '\\').LastOrDefault(); if (path != null && path.Replace(".", "").Count() > 0) { res.Add(path); } } reader.Close(); response.Close(); return(res); } catch { return(null); } }
public FTPClient(FTPPath FTP_FOLDER, string FTP_User, string FTP_Pwd) { FTP_FOLDER = ((string)FTP_FOLDER).TrimEnd('/', '\\'); FTP_FOLDER += "/"; FTP_FOLDER = FTPPath.TrimStart(FTP_FOLDER, "ftp://", "ftp."); MAINFOLDER = "ftp://ftp." + FTP_FOLDER; User = FTP_User; Pwd = FTP_Pwd; }
/// <summary> /// Download file /// </summary> /// <param name="path"></param> /// <param name="local"></param> public void Download(FTPPath path, string local) { FtpWebRequest request = GetRequest(WebRequestMethods.Ftp.DownloadFile, path); using (Stream ftpStream = request.GetResponse().GetResponseStream()) using (Stream fileStream = File.Create(local)) { ftpStream.CopyTo(fileStream); } }
/// <summary> /// Delete /// </summary> /// <param name="filep"></param> /// <returns></returns> public bool DeleteDIR(FTPPath filep) { FtpWebRequest request = GetRequest(WebRequestMethods.Ftp.RemoveDirectory, filep); try { using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { return(response.StatusDescription.StartsWith("250 ")); } } catch { return(false); } }
/// <summary> /// Upload file /// </summary> /// <param name="local"></param> /// <param name="path"></param> public void Upload(string local, FTPPath path) { var cartella = Path.GetDirectoryName(path); CreateDir(cartella); using (WebClient client = new WebClient()) { client.Credentials = NETCREDENTIAL; client.UploadFile(Validator.CombineURL(MAINFOLDER, path), WebRequestMethods.Ftp.UploadFile, local); } }
private FtpWebRequest GetRequest(string method, string folder = "") { folder = FTPPath.TrimStart(folder, MAINFOLDER); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Validator.CombineURL(MAINFOLDER, folder)); request.Credentials = NETCREDENTIAL; request.Method = method; return(request); }