Как FTPClient удалить каталог?
-
24-10-2019 - |
Вопрос
Я хочу удалить папку в FTP.
Можно FTPClient
объект удалить его?
Решение
Ftpwebrequest обеспечивает действие Delete. Вот кусок кода, чтобы достичь этого:
FtpWebRequest reqFTP = FtpWebRequest.Create(uri);
// Credentials and login handling...
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
string result = string.Empty;
FtpWebResponse response = reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
Это должно работать над файлами и каталогами. Действительно, пожалуйста, проверьте, что у вас есть правильные разрешения.
Кроме того, вы не можете удалить папки, пока они не пусты. Вы должны пересекать их рекурсивно, чтобы удалить контент раньше.
Исключения, брошенные из -за правильных проблем с разрешениями, не всегда очень ясны ...
Другие советы
Чтобы удалить пустой каталог, используйте RemoveDirectory
«Метод» FtpWebRequest
:
void DeleteFtpDirectory(string url, NetworkCredential credentials)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Ftp.RemoveDirectory;
request.Credentials = credentials;
request.GetResponse().Close();
}
Используйте его как:
string url = "ftp://ftp.example.com/directory/todelete";
NetworkCredential credentials = new NetworkCredential("username", "password");
DeleteFtpDirectory(url, credentials);
Хотя это становится намного сложнее, если вам нужно удалить непустые каталог. Нет поддержки рекурсивных операций в FtpWebRequest
класс (или любая другая реализация FTP в .NET Framework). Вы должны реализовать рекурсию самостоятельно:
- Перечислите удаленный каталог
- Относит записи, удаление файлов и повторение в подкатарии (перечисление их снова и т. Д.)
Хитрой является определение файлов из подкаталогов. Нет никакого способа сделать это портативным образом с FtpWebRequest
. Анкет А FtpWebRequest
К сожалению не поддерживает MLSD
Команда, которая является единственным портативным способом извлечения списка каталогов с помощью атрибутов файла в протоколе FTP. Смотрите также Проверка, является ли объект на FTP -сервере файл или каталог.
Ваши варианты:
- Сделайте операцию на имени файла, которая наверняка не удастся для файла и преуспевает в каталогах (или наоборот). Т.е. вы можете попробовать скачать «имя». Если это удастся, это файл, если это не удается, это каталог. Но это может стать проблемой производительности, когда у вас есть большое количество записей.
- Вам может повезло, и в вашем конкретном случае вы можете сообщить файл из каталога по имени файла (т.е. все ваши файлы имеют расширение, в то время как подкаталы нет)
- Вы используете длинный список каталогов (
LIST
Команда =ListDirectoryDetails
Метод) и попробуйте проанализировать серверный список. Многие серверы FTP используют *nix-стиль, где вы идентифицируете каталог поd
в самом начале записи. Но многие серверы используют другой формат. В следующем примере используется этот подход (при условии, что формат *NIX). - В этом конкретном случае вы можете просто попытаться удалить запись в качестве файла. Если удаление не удается, попробуйте перечислить запись в качестве каталога. Если список преуспевает, вы предполагаете, что это папка и соответственно. К сожалению, некоторые серверы не ошибаются, когда вы пытаетесь перечислить файл. Они просто вернут список с одной записью для файла.
static void DeleteFtpDirectory(string url, NetworkCredential credentials)
{
FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
listRequest.Credentials = credentials;
List<string> lines = new List<string>();
using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
using (Stream listStream = listResponse.GetResponseStream())
using (StreamReader listReader = new StreamReader(listStream))
{
while (!listReader.EndOfStream)
{
lines.Add(listReader.ReadLine());
}
}
foreach (string line in lines)
{
string[] tokens =
line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
string name = tokens[8];
string permissions = tokens[0];
string fileUrl = url + name;
if (permissions[0] == 'd')
{
DeleteFtpDirectory(fileUrl + "/", credentials);
}
else
{
FtpWebRequest deleteRequest = (FtpWebRequest)WebRequest.Create(fileUrl);
deleteRequest.Method = WebRequestMethods.Ftp.DeleteFile;
deleteRequest.Credentials = credentials;
deleteRequest.GetResponse();
}
}
FtpWebRequest removeRequest = (FtpWebRequest)WebRequest.Create(url);
removeRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
removeRequest.Credentials = credentials;
removeRequest.GetResponse();
}
Используйте его так же, как и предыдущая (плоская) реализация.
Хотя Microsoft не рекомендует FtpWebRequest
Для новой разработки.
Или используйте стороннюю библиотеку, которая поддерживает рекурсивные операции.
Например Winscp .net Assembly Вы можете удалить весь каталог с одним вызовом Session.RemoveFiles
:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Delete folder
session.RemoveFiles("/directory/todelete").Check();
}
Внутренне WinSCP использует MLSD
Команда, если поддерживается сервером. Если нет, он использует LIST
командование и поддерживает десятки различных форматов листинга.
(Я автор Winscp)
Единственный способ, которым я обнаружил, что работаю на «webrequestmethods.ftp.deletefile», и это даст исключение с папками или папками с файлами, поэтому я создал новый метод интеренала для рекулриктории для рекурсивного
public void delete(string deleteFile)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Resource Cleanup */
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) {
//Console.WriteLine(ex.ToString());
try
{
deleteDirectory(deleteFile);
}
catch { }
}
return;
}
и удаление каталога
/* Delete Directory*/
private void deleteDirectory(string directoryName)
{
try
{
//Check files inside
var direcotryChildren = directoryListSimple(directoryName);
if (direcotryChildren.Any() && (!string.IsNullOrWhiteSpace(direcotryChildren[0])))
{
foreach (var child in direcotryChildren)
{
delete(directoryName + "/" + child);
}
}
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + directoryName);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Resource Cleanup */
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
Список режиссерских детей
/* List Directory Contents File/Folder Name Only */
public string[] directoryListSimple(string directory)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Establish Return Communication with the FTP Server */
ftpStream = ftpResponse.GetResponseStream();
/* Get the FTP Server's Response Stream */
StreamReader ftpReader = new StreamReader(ftpStream);
/* Store the Raw Response */
string directoryRaw = null;
/* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Resource Cleanup */
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
/* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Return an Empty string Array if an Exception Occurs */
return new string[] { "" };
}
/* List Directory Contents in Detail (Name, Size, Created, etc.) */
public string[] directoryListDetailed(string directory)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Establish Return Communication with the FTP Server */
ftpStream = ftpResponse.GetResponseStream();
/* Get the FTP Server's Response Stream */
StreamReader ftpReader = new StreamReader(ftpStream);
/* Store the Raw Response */
string directoryRaw = null;
/* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Resource Cleanup */
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
/* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Return an Empty string Array if an Exception Occurs */
return new string[] { "" };
}
Важная точка
Как указано выше..
Вы не можете удалить папки, пока они не пусты. Вы должны пересекать их рекурсивно, чтобы удалить контент раньше.
public void Deletedir(string remoteFolder)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/"+ remoteFolder);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;***strong text***
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Get the FTP Server's Response Stream */
ftpStream = ftpResponse.GetResponseStream();
/* Open a File Stream to Write the Downloaded File */
}
catch { }
}
Это код, который вы можете использовать. И вот как вы можете использовать его, например, на нажатии кнопки.
private void button5_Click(object sender, EventArgs e)
{
ftp ftpClient = new ftp(@"SERVICE PROVIDER", "USERNAME", "PASSWORD");
ftpClient.Deletedir("DIRECTORY U WANT TO DELETE");
}
И просто помните, что ваша папка должна быть пустой.