递归删除给定 FTP 路径中的所有文件和文件夹

本文关键字:文件 文件夹 删除 FTP 路径 递归 | 更新日期: 2023-09-27 18:30:23

我正在尝试创建一个程序,该程序将递归删除给定FTP路径中的所有文件和文件夹。如果给定路径中只有几个文件或目录,我的程序运行良好。但是,出于某种原因,如果一个目录中有多个文件,它只会挂在ReadLine()行上,最终出现IO错误。这是我的代码:

class Program
{
    private const string FtpSite = "myftpsite.com";
    private const string FtpUserName = "myusername";
    private const string FtpPassword = "mypassword";
    static void Main(string[] args)
    {
        DeleteFilesAndFolders("/");
    }
    private static void DeleteFilesAndFolders(string path)
    {
        if (path != null && (path.StartsWith(@"''") || path.StartsWith("//")))
            path = path.Remove(0, 1);
        List<FileObject> files = DirectoryListing(path);
        foreach (FileObject file in files.Where(file => !file.IsDirectory))
        {
            DeleteFile(path, file.FileName);
        }
        foreach (FileObject file in files.Where(file => file.IsDirectory))
        {
            DeleteFilesAndFolders(path + "/" + file.FileName);
            DeleteFolder(path + "/" + file.FileName);
        }
    }
    private static void DeleteFile(string path, string file)
    {
        var clsRequest = (FtpWebRequest)WebRequest.Create("ftp://" + FtpSite + path + "/" + file);
        clsRequest.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
        clsRequest.Method = WebRequestMethods.Ftp.DeleteFile;
        using (var response = (FtpWebResponse) clsRequest.GetResponse())
        {
            using (Stream datastream = response.GetResponseStream())
            {
                if (datastream == null)
                    return;
                using (var sr = new StreamReader(datastream))
                {
                    sr.ReadToEnd();
                    sr.Close();
                }
                datastream.Close();
                response.Close();
            }
        }
    }
    private static void DeleteFolder(string path)
    {
        var clsRequest = (FtpWebRequest)WebRequest.Create("ftp://" + FtpSite + path);
        clsRequest.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
        clsRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
        using (var response = (FtpWebResponse)clsRequest.GetResponse())
        {
            using (Stream datastream = response.GetResponseStream())
            {
                if (datastream == null)
                    return;
                using (var sr = new StreamReader(datastream))
                {
                    sr.ReadToEnd();
                    sr.Close();
                }
                datastream.Close();
                response.Close();
            }
        }
    }
    private static List<FileObject> DirectoryListing(string path)
    {
        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 request = (FtpWebRequest)WebRequest.Create("ftp://" + FtpSite + path);
        request.Credentials = new NetworkCredential(FtpUserName, FtpPassword);
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        var result = new List<FileObject>();
        using (var response = (FtpWebResponse) request.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream == null)
                    return null;
                using (var reader = new StreamReader(responseStream))
                {
                    while (!reader.EndOfStream)
                    {
                        string r = reader.ReadLine();
                        if (string.IsNullOrWhiteSpace(r))
                            continue;
                        var reg = regex.Match(r);
                        var c = new FileObject
                        {
                            FileName = reg.Groups[6].Value,
                            IsDirectory = reg.Groups[1].Value.ToLower() == "d"
                        };
                        result.Add(c);
                    }
                    reader.Close();
                }
                response.Close();
            }
        }
        return result;
    }
}

这是我的简单容器FileObject类:

internal class FileObject
{
    public bool IsDirectory { get; set; }
    public string FileName { get; set; }
}

递归删除给定 FTP 路径中的所有文件和文件夹

我的问题似乎通过使用主动模式而不是被动模式来解决。我将其添加到我的代码中:

request.UsePassive = false;

这似乎解决了我的问题。我在大量具有许多子目录和子子目录的文件中对其进行了测试,现在它似乎完美无缺。