[英]Deleting the folder and sub folders in FTP server
我已經在FTP服務器中創建了文件夾,其中包含登錄到服務器的年份,月份和日期,我們可以看到當年點擊它顯示月份時創建的文件夾,當我點擊月份時它顯示日期。 現在我需要刪除此文件夾。
下面是我在FTP服務器中刪除文件夾的代碼
FtpWebResponse responseFileDelete = (FtpWebResponse)ftpRequest.GetResponse();
System.dll中出現未處理的“System.Net.WebException”類型異常
附加信息:遠程服務器返回錯誤:(550)文件不可用(例如,找不到文件,沒有訪問權限)。
你能幫我刪除一個文件夾嗎?
您為DeleteFile
調用組裝的URL是錯誤的。
附:
path = "ftp://ftp.example.com/" + "/" + ff; string server = "ftp://ftp.example.com/";
ftpURL + "/" + ftpDirectory
是ftp://ftp.example.com/ftp://ftp.example.com//dir
而你想要ftp://ftp.example.com//dir
或者ftp://ftp.example.com/dir
。
只使用ftpDirectory
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpDirectory);
無論如何,您無法使用WebRequestMethods.Ftp.DeleteFile
刪除文件夾。 您必須使用WebRequestMethods.Ftp.RemoveDirectory
。
ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
但請注意,即使.RemoveDirectory
也只能刪除空目錄。
您必須先遞歸刪除文件夾的文件和子文件夾,然后才能刪除文件夾本身。
使用FtpWebRequest
實現遞歸並不容易,特別是因為它不支持MLSD
命令(區分文件和文件夾的唯一可靠方法是什么)。 有關詳細信息,請參閱我對C#的回答通過FTP下載所有文件和子目錄 。
或者,使用另一個支持遞歸操作的FTP庫。
例如,使用WinSCP .NET程序集 ,您可以使用Session.RemoveFiles
在單個調用中刪除包含其內容的文件夾:
SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = "ftp.example.com", UserName = "username", Password = "mypassword", }; using (Session session = new Session()) { session.Open(sessionOptions); session.RemoveFiles("/" + ff); }
(我是WinSCP的作者)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.