C# Recursive FTP Download: Conquering Subdirectories and Files
Building a robust file synchronization tool often involves navigating the complexities of recursive FTP downloads. A common challenge is handling both files and subdirectories, especially when encountering the frustrating 550 error when treating directories as files. This article presents a solution using recursive programming in C#.
The Recursive Approach
The core solution lies in recursive function design. This allows the code to systematically traverse nested directory structures, downloading files from each level. Since FtpWebRequest
doesn't directly support recursion, we must distinguish between files and directories. One method involves attempting a download; failure indicates a directory. Alternatively, relying on file extensions can achieve the same.
Recursive Function Implementation
The following C# function recursively downloads an FTP directory's contents:
<code class="language-csharp">private 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') // Directory { Directory.CreateDirectory(localFilePath); DownloadFtpDirectory(fileUrl + "/", credentials, localFilePath); } else // File { DownloadFtpFile(fileUrl, localFilePath); } } } private void DownloadFtpFile(string url, string localPath) { // Code to download the file }</code>
Usage Example:
To use this function:
<code class="language-csharp">NetworkCredential credentials = new NetworkCredential("user", "password"); string ftpUrl = "ftp://ftp.example.com/directory/to/download/"; DownloadFtpDirectory(ftpUrl, credentials, @"C:\local\target\directory\");</code>
Enhancements and Considerations:
MLSD
or using consistent directory listings, consider using the WinSCP .NET assembly for simpler recursive downloads.This improved approach provides a more robust and efficient solution for recursively downloading files and subdirectories via FTP in C#. Remember to replace "user"
, "password"
, and the FTP URL with your actual credentials and target location. The DownloadFtpFile
function needs to be implemented to handle the actual file download process.
The above is the detailed content of How to Recursively Download Files and Subdirectories via FTP in C#?. For more information, please follow other related articles on the PHP Chinese website!