本文介绍了如何使用FTPClient递归删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
FTPClient如何以递归方式删除包含文件的文件夹?
FTPClient How to recursively delete a folder with the files within?
推荐答案
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, "");
}
}
这篇关于如何使用FTPClient递归删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!