private bool DeleteDir(string name, FTPConnection ftpConnection) { try { ftpConnection.ChangeCurrentWorkingDirectory(name); string result = ftpConnection.GetFileList(); var list = ParseFileList(ftpConnection, result); foreach (var file in list) { if (!file.IsDirectory) { ftpConnection.DeleteFile(file.Name); } else { var ret = DeleteDir(name + "/" + file.Name, ftpConnection); } } ftpConnection.DeleteDir(name); return(true); } catch (FTPResponseException ex) { FTPErrorNotifications?.Invoke(ex); } return(false); }
public Task <List <RemoteFile> > GetFileList() { return(Task.Run(() => { FTPConnection ftpConnection = null; try { ftpConnection = CreateConnection(Server); Utils.WriteDebugInfo(ftpConnection.connectionName, "用于获取文件列表的连接已建立。"); ftpConnection.ChangeCurrentWorkingDirectory(CurrentRemotePath); FTPInfoNotifier?.Invoke("获取文件列表..."); string result = ftpConnection.GetFileList(true); var list = ParseFileListWithFileInfo(result); if (list == null) { // fallback to NLST result = ftpConnection.GetFileList(false); list = ParseFileList(ftpConnection, result); } FTPInfoNotifier?.Invoke("FTP 服务就绪,文件列表刷新于 " + DateTime.Now + ",共" + list.Count + "项"); ftpConnection.Close(); Utils.WriteDebugInfo(ftpConnection.connectionName, "操作已成功完成,连接主动断开。"); return list; } catch (FTPResponseException ex) { FTPErrorNotifications?.Invoke(ex); ftpConnection?.Close(); if (ftpConnection != null) { Utils.WriteDebugInfo(ftpConnection.connectionName, "由于发生了不可恢复的异常,连接已断开。" + " FTPResponseException: " + ex.Message); } } return null; })); }