Example #1
0
        public void DeleteDirectory(FtpDirectory directory)
        {
            if (directory.Folders == null && directory.Files == null) return;

            //delete files on this directory first
            if (directory.Files != null)
            {
                foreach (var ftpFile in directory.Files)
                {
                    DeleteFile(ftpFile.FullPath);
                }
            }
            if (directory.Folders == null) return;
            foreach (var folder in directory.Folders)
            {
                DeleteDirectory(folder);
                DeleteEmptyDirectory(folder.FullPath);
            }
        }
Example #2
0
        private FtpDirectory GetDirectoryListing(string path, DoWorkEventArgs e)
        {
            var res = new FtpDirectory();
            var request = (FtpWebRequest)WebRequest.Create("ftp://" + _ftpConfiguration.Url + "/" + path);
            request.Credentials = new NetworkCredential(_ftpConfiguration.UserName, _ftpConfiguration.Password);

            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            List<string> result = new List<string>();

            using (var response = (FtpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        while (!reader.EndOfStream)
                        {
                            result.Add(reader.ReadLine());
                        }

                        reader.Close();
                        response.Close();
                    }
                }
            }
            foreach (var item in result)
            {
                if (_bw.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                var regex = new Regex(@"^([d-])([rwxt-]{3}){3}\s+\d{1,}\s+.*?(\d{1,})\s+(\w+\s+\d{1,2}\s+(?:\d{4})?)(\d{1,2}:\d{2})?\s+(.+?)\s?$",
                    RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

                var match = regex.Match(item);
                if (match.Success)
                {
                    var type = match.Groups[1].Value;
                    var name = match.Groups[6].Value;
                    if (name == "." || name == "..") continue;
                    if (type == "d")
                    {
                        if (res.Folders == null) res.Folders = new List<FtpDirectory>();
                        _bw.ReportProgress(0, "Reading " + path + "/" + name);
                        var subFolder = GetDirectoryListing(path + "/" + name, e);
                        if (subFolder.Folders != null)
                        {
                            _bw.ReportProgress(0, "Found " + subFolder.Folders.Count + " folders");
                        }
                        if (subFolder.Files != null)
                        {
                            _bw.ReportProgress(0, "Found " + subFolder.Files.Count + " files");
                        }
                        subFolder.Name = name;
                        subFolder.FullPath = path + "/" + name;
                        res.Folders.Add(subFolder);
                    }
                    else
                    {
                        if (res.Files == null) res.Files = new List<FtpFile>();
                        res.Files.Add(new FtpFile() { Name = name, FullPath = path + "/" + name });
                        //_bw.ReportProgress(0, "Found " + path + "/" + name);
                    }
                }
            }
            e.Result = res;
            return res;
        }
Example #3
0
        private void DeleteDirectory(FtpDirectory directory, DoWorkEventArgs e)
        {
            if (directory.Folders == null && directory.Files == null) return;

            //delete files on this directory first
            if (directory.Files != null)
            {
                foreach (var ftpFile in directory.Files)
                {
                    if(_bw.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }
                    DeleteFile(ftpFile.FullPath);
                    _bw.ReportProgress(0, ftpFile.FullPath + " deleted");
                }
            }
            if (directory.Folders == null) return;
            foreach (var folder in directory.Folders)
            {
                if (_bw.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                DeleteDirectory(folder, e);

                if (_bw.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                DeleteEmptyDirectory(folder.FullPath);
                _bw.ReportProgress(0, folder.FullPath + " deleted");

            }
        }