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

时间:2014-11-13 21:50:48

标签: c# ftp

我正在尝试创建一个程序,它将递归删除给定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; }
}

1 个答案:

答案 0 :(得分:3)

我的问题似乎是通过使用Active模式而不是Passive来解决的。我把它添加到我的代码中:

request.UsePassive = false;

这似乎解决了我的问题。我在一大批具有大量子目录和子子目录的文件上测试了它,现在它似乎完美无瑕。