ftp directory listing timeout. Huge number of subdirs - c#

Is there any way to deal with situation when you need to get list of all directories on a FTP server, where the number of directories is so big that it takes too long to get it and operation fails with timeout?
I wonder if there are some libraries that let you do that somehow?

Try something like this
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Credentials = new NetworkCredential("anonymous","yourName#SomeDomain.com");//replace with your Creds
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
// also add some code that will Dispose of the StreamReader object
// something like ((IDisposable)streanReader).Dispose();
// Dispose of the List<string> as well
line = null;

Related

Equivalent command or single line code like mget command in unix in C# [duplicate]

General Info
I'm still in the process of learning C#. To help myself out, I'm trying to create a program that will automatically synchronise all of my local projects with a folder on my FTP server. This so that whether I'm at school or at home, I always have the same projects available to me.
I know there are programs like Dropbox that already do this for me, but I figured creating something like that myself will teach me a lot along the way.
The problem
My first step towards my goal was to just download all files, subdirectories and subfiles from my FTP server. I've managed to download all files from a directory with the code below. However, my code only lists the folder names and the files in the main directory. Subfolders and subfiles are never returned and never downloaded. Aside from that, the server returns a 550 error because I'm trying to download the folders as if they are files. I've been on this for 4+ hours now, but I just can't find anything on how to fix these problems and make it work. Therefor I'm hoping you guys will help me out :)
Code
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
request.KeepAlive = false;
request.UsePassive = false;
response = request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
downloadFiles = null;
return downloadFiles;
}
}
private void Download(string file)
{
try
{
string uri = url + "/" + file;
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url + "/" + file);
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
request.KeepAlive = false;
request.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writeStream = new FileStream(localDestnDir + "\\" + file, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
writeStream.Close();
response.Close();
}
catch (WebException wEx)
{
MessageBox.Show(wEx.Message, "Download Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Download Error");
}
}
The FtpWebRequest does not have any explicit support for recursive file operations (including downloads). You have to implement the recursion yourself:
List the remote directory
Iterate the entries, downloading files and recursing into subdirectories (listing them again, etc.)
Tricky part is to identify files from subdirectories. There's no way to do that in a portable way with the FtpWebRequest. The FtpWebRequest unfortunately does not support the MLSD command, which is the only portable way to retrieve directory listing with file attributes in FTP protocol. See also Checking if object on FTP server is file or directory.
Your options are:
Do an operation on a file name that is certain to fail for file and succeeds for directories (or vice versa). I.e. you can try to download the "name". If that succeeds, it's a file, if that fails, it's a directory.
You may be lucky and in your specific case, you can tell a file from a directory by a file name (i.e. all your files have an extension, while subdirectories do not)
You use a long directory listing (LIST command = ListDirectoryDetails method) and try to parse a server-specific listing. Many FTP servers use *nix-style listing, where you identify a directory by the d at the very beginning of the entry. But many servers use a different format. The following example uses this approach (assuming the *nix format)
void DownloadFtpDirectory(
string url, NetworkCredential credentials, string localPath)
{
FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
listRequest.Credentials = credentials;
List<string> lines = new List<string>();
using (var listResponse = (FtpWebResponse)listRequest.GetResponse())
using (Stream listStream = listResponse.GetResponseStream())
using (var 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 localFilePath = Path.Combine(localPath, name);
string fileUrl = url + name;
if (permissions[0] == 'd')
{
if (!Directory.Exists(localFilePath))
{
Directory.CreateDirectory(localFilePath);
}
DownloadFtpDirectory(fileUrl + "/", credentials, localFilePath);
}
else
{
FtpWebRequest downloadRequest =
(FtpWebRequest)WebRequest.Create(fileUrl);
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
downloadRequest.Credentials = credentials;
using (FtpWebResponse downloadResponse =
(FtpWebResponse)downloadRequest.GetResponse())
using (Stream sourceStream = downloadResponse.GetResponseStream())
using (Stream targetStream = File.Create(localFilePath))
{
byte[] buffer = new byte[10240];
int read;
while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
{
targetStream.Write(buffer, 0, read);
}
}
}
}
}
Use the function like:
NetworkCredential credentials = new NetworkCredential("user", "mypassword");
string url = "ftp://ftp.example.com/directory/to/download/";
DownloadFtpDirectory(url, credentials, #"C:\target\directory");
If you want to avoid troubles with parsing the server-specific directory listing formats, use a 3rd party library that supports the MLSD command and/or parsing various LIST listing formats; and recursive downloads.
For example with WinSCP .NET assembly you can download whole directory with a single call to the Session.GetFiles:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "ftp.example.com",
UserName = "user",
Password = "mypassword",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Download files
session.GetFiles("/directory/to/download/*", #"C:\target\directory\*").Check();
}
Internally, WinSCP uses the MLSD command, if supported by the server. If not, it uses the LIST command and supports dozens of different listing formats.
The Session.GetFiles method is recursive by default.
In most cases, Session.GetFilesToDirectory is a more straightforward equivalent to Session.GetFiles:
session.GetFiles("/directory/to/download", #"C:\target\directory").Check();
(I'm the author of WinSCP)

Get files list from Folder through FTP using c# [duplicate]

This question already has an answer here:
C# Download all files and subdirectories through FTP
(1 answer)
Closed 5 years ago.
I want to fetch the files from folder through FTP using c#, I have folder name called MyFolder, inside of this folder i have multiple folder, i need to fetch each file from all this folders which is inside the my MyFolder.Below code which i am getting all directories,Now i need to get each file.
Eg:httpdocs/Myfolder/newfolder/newfile.txt
/newfile1.txt
/newfile2.txt
httpdocs/Myfolder/newfolder1/newfile.txt
httpdocs/Myfolder/newfolder2/newfile.txt
FtpWebRequest ftpRequest =(FtpWebRequest)WebRequest.Create("ftp://www.xxxxxxx.com/httpdocs/MyFolder");
ftpRequest.Credentials = new NetworkCredential("xxxxx", "xxxxxx");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
}
Have you had a look at the MSDN Documentation?
https://msdn.microsoft.com/de-de/library/ms229711(v=vs.110).aspx
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe#contoso.com");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
}
Edit: Already on StackOverflow Have a look here.

In C#, is it possible to check files in certain FTP folder and upload the ones that aren't there?

I want to write a C# that:
Access the FTP with username and password
Goes to a certain folder
Sees what files aren't there from my PC folder and upload them.
My idea:
I have a folder called "mods" in my PC and another folder called "mods" in the FTP, so instead of opening the "FileZilla" software I want to write a C# that connects to the FTP and check what files aren't there.
Thank you so much!
you can list your file inftp server like this and do a comparaiosn with list file of mod dir
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Credentials =new NetworkCredential("anonymous","janeDoe#contoso.com");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
There's no magic way, with a pure .NET framework (its FtpWebRequest class). You have to code it.
List the FTP folder
List the local folder
Find the missing files
Upload them one by one
void SynchronizeLocalAndFtpDirectory(
string localPath, string remoteUri, NetworkCredential credentials)
{
List<string> remoteFiles = new List<string>();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(remoteUri);
request.Credentials = credentials;
request.Method = WebRequestMethods.Ftp.ListDirectory;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
while (!reader.EndOfStream)
{
remoteFiles.Add(reader.ReadLine());
}
}
IEnumerable<string> localFiles =
Directory.GetFiles(localPath).Select(path => Path.GetFileName(path));
IEnumerable<string> missingFiles = localFiles.Except(remoteFiles);
foreach (string filename in missingFiles)
{
Console.WriteLine("Uploading missing file {0}", filename);
string remoteFileUri = remoteUri + filename;
string localFilePath = Path.Combine(localPath, filename);
FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(remoteFileUri);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
uploadRequest.Credentials = credentials;
using (Stream targetStream = uploadRequest.GetRequestStream())
using (Stream sourceStream = File.OpenRead(localFilePath))
{
byte[] buffer = new byte[10240];
int read;
while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
{
targetStream.Write(buffer, 0, read);
}
}
}
}
Use the function like:
SynchronizeLocalAndFtpDirectory(
#"C:\local\mods", "ftp://ftp.example.com/remote/mods/",
new NetworkCredential("username", "password"));
(Do not forget the trailing slash in the URI).
Or use 3rd party FTP library that supports synchronization.
For example with the WinSCP .NET assembly, this is as easy as a single call to Session.SynchronizeDirectories.
And it will not only upload missing file, but also update out-of-the-date files and optionally delete orphan files.
void SynchronizeLocalAndFtpDirectory(
string localPath, string remotePath, SessionOptions sessionOptions)
{
using (Session session = new Session())
{
session.Open(sessionOptions);
session.SynchronizeDirectories(
SynchronizationMode.Remote, localPath, remotePath, false).Check();
}
}
Use it like:
SessionOptions sessionOptions = new SessionOptions()
{
Protocol = Protocol.Ftp,
HostName = "ftp.example.com",
UserName = "username",
Password = "password",
};
SynchronizeLocalAndFtpDirectory(#"C:\local\mods", "/remote/mods", sessionOptions);
(I'm the author of WinSCP)

Cannot create new directory on ftp server

I am trying to create a directory on my FTP server. I had already created ContractorDoc directory on server and want to create new directory NewDirectory in it.
try
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://abc.xyz.com/ContractorDoc/NewDirectory");
// Step 2 - Configure the connection request
request.Credentials = new NetworkCredential("uname", "passsword");
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.MakeDirectory;
// Step 3 - Call GetResponse() method to actually attempt to create the directory
FtpWebResponse makeDirectoryResponse = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
String status = ((FtpWebResponse)ex.Response).StatusDescription;
}
I get an exception:
550 Cannot create a file when that file already exists.
Am I missing something?
I think he's got to make sure there folder.
There is no error in the code. Maybe there's a problem with the server.
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://abc.xyz.com/ContractorDoc");
ftpRequest.Credentials =new NetworkCredential("uname","password");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();

C# WebException Error: 530 (not logged in)

I need some help with some code that is not working for some reason. I'm making a method that gets a list of files in a FTP directory. Every time I debug the app, a WebException is thrown with the StatusCode of 530 (not logged in). Keep in mind that I am 100% positive the address, username, and password are correct. Here's the method:
public static List<string> GetFileList(string Directory)
{
List<string> Files = new List<string>();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ServerInfo.Root + Directory));
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Username);
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //Error occurs here
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string CurrentLine = reader.ReadLine();
while (!string.IsNullOrEmpty(CurrentLine))
{
Files.Add(CurrentLine);
CurrentLine = reader.ReadLine();
}
reader.Close();
response.Close();
return Files;
}
This is the value of ServerInfo.Root: "ftp://192.xxx.4.xx:21/MPDS" (partially censored for privacy)
I have used MessageBoxes to ensure the complete URI is correct, and it is.
I've been struggling with this problem for a long time now, so I hope you can help me fix it.
Thanks in advance!
You can try this code with some corrections:
public static List<string> GetFileList(string Directory)
{
List<string> Files = new List<string>();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ServerInfo.Root + Directory));
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Username); // Is this correct?
// request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Password); // Or may be this one?
request.UseBinary = false;
request.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string CurrentLine = reader.ReadLine();
while (!string.IsNullOrEmpty(CurrentLine))
{
Files.Add(CurrentLine);
CurrentLine = reader.ReadLine();
}
reader.Close();
response.Close();
return Files;
}

Categories

Resources