如何使用FTPClient递归删除文件? [英] How to delete file recursively using FTPClient?

查看:467
本文介绍了如何使用FTPClient递归删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FTPClient如何以递归方式删除包含文件的文件夹?

FTPClient How to recursively delete a folder with the files within?

推荐答案

以下是获取所有文件夹和文件夹的源代码。来自FTP的递归文件

现在您需要根据您的要求修改此代码。



Here is the source code that will get all the folders & file recursively from FTP
Now you need to modify this code as per your requirement.

public static void GetFTPFilesPath(string ftpAddress, string UserName, string Password)
        {
           
            FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpAddress);

            try
            {
                reqFTP.UsePassive = true;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = false;
                reqFTP.Credentials = new NetworkCredential(UserName, Password);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

                Stream responseStream = response.GetResponseStream();
                List<string> files = new List<string>();
                StreamReader reader = new StreamReader(responseStream);
                while (!reader.EndOfStream)
                    files.Add(reader.ReadLine());
                reader.Close();
                responseStream.Dispose();
                
                //Loop through the resulting file names.
                string ftpPath = string.Empty;
                foreach (var fileName in files)
                {
                    var parentDirectory = "";
                    PeriodicalFile lstFile = new PeriodicalFile();
                    //If the filename has an extension, then it actually is 
                    //a file            
                    if (fileName.Contains(".zip"))
                    {
                        ftpPath = ftpAddress + fileName;
                        
                    }
                    else
                    {
                        //If the filename has no extension, then it is just a folder. 
                        //Run this method again as a recursion of the original:
                        parentDirectory += fileName + "/";
                        try
                        {
                            GetFTPFilesPath(ftpAddress + "/" + parentDirectory, UserName, Password);
                        }
                        catch (Exception ex)
                        {
                            ErrorLog.WriteLog("ftpFileProcessing", "GetFTPFilesPath(Else)", ex.Message, ex.StackTrace, "");
                        }
                    }
                }
                
            }
            

            catch (Exception excpt)
            {

                reqFTP.Abort();
                ErrorLog.WriteLog("ftpFileProcessing", "GetFTPFilesPath", excpt.Message, excpt.StackTrace, "");

            }           



        }


试试这个,



http://msdn.microsoft .com / zh-cn / library / system.net.ftpwebrequest.aspx [ ^ ]


这篇关于如何使用FTPClient递归删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆