I had created folders in FTP server with year, month and date that is after getting logged in to the server we can see a folder created on year when I click on that year it shows month and when I click on month it shows date. Now I need to delete this folder.
Below is my code to delete folder in FTP server
FtpWebResponse responseFileDelete = (FtpWebResponse)ftpRequest.GetResponse();
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
Can you please help me out to delete a folder.
The URL you assemble for the DeleteFile call is wrong.
With:
path = "ftp://ftp.example.com/" + "/" + ff;
string server = "ftp://ftp.example.com/";
The ftpURL + "/" + ftpDirectory is ftp://ftp.example.com/ftp://ftp.example.com//dir while you want ftp://ftp.example.com//dir or maybe ftp://ftp.example.com/dir.
Use just the ftpDirectory
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpDirectory);
You cannot delete a folder with the WebRequestMethods.Ftp.DeleteFile anyway. You have to use the WebRequestMethods.Ftp.RemoveDirectory.
ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
But note that even the .RemoveDirectory can remove an empty directory only.
You have to recursively delete files and subfolders of the folder first and only then you can delete the folder itself.
Implementing a recursion using FtpWebRequest is not easy, particularly because it does not support the MLSD command (what is the only reliable way to distinguish files from folders). For details, see my answer to C# Download all files and subdirectories through FTP.
Alternatively, use another FTP library that supports recursive operations.
For example with WinSCP .NET assembly, you can use Session.RemoveFiles to delete a folder with its content in a single call:
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);
}
(I'm the author of WinSCP)
Related
I have to check whether an FTP Server allows me to delete a file or not. Without deleting an existing file or sending a file and trying to delete that file.
For now, I use the 'Send a file and try to Delete it' dummy solution, but sometimes I don't have write permission.
I tried the code below using Chilkat library, but as I know, there are only Read, Write and Execute attributes, and the Delete attribute doesn't exist.
var ftp = new Chilkat.Ftp2();
ftp.Hostname = "127.0.0.1";
ftp.Username = "test";
ftp.Password = "test";
ftp.AuthTls = false;
ftp.PassiveUseHostAddr = true;
ftp.Connect();
// To get file permissions in UNIX format, disallow MSLD:
ftp.AllowMlsd = false;
if (ftp.GetDirCount() > 0)
{
textBox1.AppendText("The permissions format is: " + ftp.GetPermType(0));
textBox1.AppendText("\r\n");
}
for(var i = 0; i < ftp.GetDirCount();++i)
{
// Display the permissions and filename
textBox1.AppendText(ftp.GetPermissions(i) + " " + ftp.GetFilename(i));
textBox1.AppendText("\r\n");
}
ftp.Disconnect();
So, according to my explanation above, Is it possible to determine whether the FTP server has Delete File permission or not? If yes,
There's no API in FTP protocol to test for permissions.
You can try to interpret the permissions you get from the FTP directory listing.
But for example on *nix systems, the listing never gives you enough information for such decision.
Btw, on *nix servers, you have permissions to delete a file, if you have write permissions to its parent directory. What is the same permissions for for creating a new file. So normally, if you can create a file, you can delete a file. But FTP servers commonly impose another permissions on top of the system permissions. And those usually include separate permissions for creating and deleting files.
I need list ftp site directory with username and password, and I can access it in FileZilla.
The Directory structure in FileZilla is like below,
/
FtpDir
Input
Input
Archive
Output
Output
Archive
However, when accessing it in FtpWebRequest class in C#, it gives exception "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)". The code is,
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.xx.x.xx.xx/FtpDir/Input/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(username, password);
I have accessed another Ftp site successfully, the uri is like ftp://ftp.somecompany.com/FtpDir/
and I can access ftp://ftp.somecompany.com/FtpDir/ from file explorer as well.
But I cannot access ftp://ftp.xx.x.xx.xx/FtpDir/Input/ from file explorer.
What is the problem?
Thanks
request.UsePassive = false; solved the problem.
So this is my code, i basically copied and pasted from SharpSSH website.
Sftp oSftp = new Tamir.SharpSsh.Sftp(_ftpURL, _UserName, _Password);
oSftp.Connect(_Port);
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);
oSftp.Close();
When i run this, i get a "first chance exception" on oSftp.Connect(_Port)
A first chance exception of type 'Tamir.SharpSsh.jsch.JSchException' occurred in Tamir.SharpSSH.dll
Does anyone experienced with SharpSSH have any idea why this is happening? I have also tried uploading files to the server using Rubex but it gave me the same error.
First you need to pass only folder path of server and do not specify file name in that path.
oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory);
and also make sure you have added below packages from nuget
Install-Package DiffieHellman
Install-Package Org.Mentalis.Security
just check your path of file and path where you want to upload on sftp.
example:
Sftp sftp=new Sftp("host","username","pass");
sftp.Connect();
sftp.Put("path of your local file","path to upload file on sftp server");
sftp.Close();
I am trying to moving a file from one folder to another using FtpWebRequest but i keep getting error 550. This is my code;
var requestMove = (FtpWebRequest)WebRequest.Create(Helper.PathFtp + Helper.NewFolder + file);
requestMove.Method = WebRequestMethods.Ftp.Rename;
requestMove.Credentials = networkCredential;
requestMove.RenameTo = "../" + Helper.OldFolder + file;
requestMove.GetResponse();
I can list, upload, download and delete files but moving/renaming is hopeless. I have read several posts both on stackoverflow and other sites and have tried things like setting Proxy to null and adding special characters to paths but I cant find a solution that works.
The path I use in WebRequest.Create is correct as I can delete it so it must be the RenameTo I got an issue with. Any ideas?
Error 550 means access denied. If the ftp user has sufficient rights, a program (e.g. antivirus, windows thumbnail generator etc) could have the file opened and deny your move request.
You need to contact the server administrator to get around the problem.
I am using the following code to download file from FTP.
NetworkCredential credential = new NetworkCredential(Properties.Settings.Default.FTPUserName, Properties.Settings.Default.FTPPassword);
string inputfilepath = Path.Combine(Properties.Settings.Default.LocalDownloadFolder, file);
string ftpfullpath = Properties.Settings.Default.FTPSite + Properties.Settings.Default.FTPFolder + file;
WebClient request1 = new WebClient();
request1.Credentials = credential;
request1.DownloadFile(ftpfullpath, inputfilepath);
Values of the first two vaiables is:
E:\FTPDownloads\CardholderManagementReport_1030_2012-12-11.xls
ftp://abc.com/AKSHAY/CardholderManagementReport_1030_2012-12-11.xls
It shows error as :
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
EDIT:
I can see that the file is indeed present there, credentials are ok and I can download it using FileZilla
The error 550 returned by the FTP server indicates that the user you are using to try and access the file with does not have permission to access the file.
Either use some other set of credentials that has access to the file or change the permissions on the file to allow access.
This makes it work.
ftp://abc.com/%2f/AKSHAY/CardholderManagementReport_1030_2012-12-11.xls
Explaination is also there.