Example #1
0
        /// <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 += "/";
            }
        }
Example #2
0
        /// <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);
            }
        }
Example #3
0
 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;
 }
Example #4
0
        /// <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);
                }
        }
Example #5
0
        /// <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); }
        }
Example #6
0
        /// <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);
            }
        }
Example #7
0
        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);
        }