C# WebClient Download file from FTP - c#

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.

Related

Copy File to remote windows server using .net core

I have put endpoint that accept the zip file. The endpoint is working fine to get the zip file. Now from that endpoint after I get that file I am trying to copy that file to a different remote Server.
I try below code to connect to remote Server using the below code by referring to micrsoft docs https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.runimpersonated?view=net-6.0:
bool returnValue = LogonUser(#"ACC\Test.test", "acc.local", "password",
9, 0,
out safeAccessTokenHandle);
if (returnValue)
{
WindowsIdentity.RunImpersonated(safeAccessTokenHandle, () =>
{
//Destination remote server folder to upload file
var pathToUploadFile = #"\\152.158.100.45\D\FileUpload";
string fileName;
fileName = file.FileName;
var path = Path.Combine(pathToUploadFile, fileName);
using (var stream = new FileStream(path, FileMode.Create))
{
//save a zip file to folder
file.CopyTo(stream);
}
});
}
In this code I get error of username and password is incorrect in this line of code:
var stream = new FileStream(path, FileMode.Create)
I am not even sure if the LogonUser method connects to a remote server. So is there is better way to do this copy file to remote server Please give an idea to implement in .net core 6.0 C#. Any help is really appreciated. Thanks
Finally I am able to solve the issue by implementing the Win32 API called WNetUseConnection solutions that was mentioned on below questions:
Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials

How to list ftp directories by using FtpWebRequest in C#?

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.

Deleting the folder and sub folders in FTP server

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)

Download File via Tamir.SharpSSH.Sftp.Get()

I'm trying to download a file using the Tamir SSH library. I'm able to connect to the remote FTP site, upload files to the site, but I'm getting exceptions when trying to download. I've given IIS_IUSRS full control of the local directory as well as ASPNET. I've tested an I'm able to create a text file in the same local directory I'm trying to download to. Any ideas?
string SFTP_HOST = ConfigurationManager.AppSettings["AccentivFtpHost"];
string SFTP_USERNAME = ConfigurationManager.AppSettings["AccentivFtpUsername"];
string SFTP_PASSWORD = ConfigurationManager.AppSettings["AccentivFtpPassword"];
Sftp client = new Sftp(SFTP_HOST, SFTP_USERNAME, SFTP_PASSWORD);
client.Connect(22);
client.Get("test.txt", "c:\\test.txt");
You probably lack a '/' character in the file directory. You may need to put it either in the Get function call before the "test.txt" like "/test.txt" or at the end of your AccentivFtpHost value in the app config file.

How to download file using web service in C#?

How to download a file via web service ? And how the client application accept this?
I write down the code as below in the client app, it throws an exception "Access Denied"
wsDownload.wsDownloadFile downFile = new wsDownload.wsDownloadFile();
byte[] file = downFile.DownloadFile(strFileName, "", "", "");
MemoryStream mStream = new MemoryStream(file);
any response is appreciated.
"Access denied" probably just means the local web service user doesn't have read access to the local copy of the file.
Ron
It's probably a matter of ntfs permissions on the service side. You should grant read permissions on the folder that contains the file you are downloading to the user account under which is running the WS, usually IIS_WPG.

Categories