Parsing FtpWebRequest ListDirectoryDetails line

Not sure if you still need this, but this is the solution i came up with: Regex regex = new Regex ( @”^([d-])([rwxt-]{3}){3}\s+\d{1,}\s+.*?(\d{1,})\s+(\w+\s+\d{1,2}\s+(?:\d{4})?)(\d{1,2}:\d{2})?\s+(.+?)\s?$”, RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace ); Match Groups: object type: d : directory – : file Array[3] of permissions (rwx-) File Size Last Modified Date Last Modified Time File/Directory Name

C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response

For the first (DOS/Windows) listing this code will do: FtpWebRequest request = (FtpWebRequest)WebRequest.Create(“ftp://ftp.example.com/”); request.Credentials = new NetworkCredential(“user”, “password”); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()); string pattern = @”^(\d+-\d+-\d+\s+\d+:\d+(?:AM|PM))\s+(<DIR>|\d+)\s+(.+)$”; Regex regex = new Regex(pattern); IFormatProvider culture = CultureInfo.GetCultureInfo(“en-us”); while (!reader.EndOfStream) { string line = reader.ReadLine(); Match match = regex.Match(line); string s = match.Groups[1].Value; DateTime … Read more

Upload and download a file to/from FTP server in C#/.NET

Upload The most trivial way to upload a binary file to an FTP server using .NET framework is using WebClient.UploadFile: WebClient client = new WebClient(); client.Credentials = new NetworkCredential(“username”, “password”); client.UploadFile( “ftp://ftp.example.com/remote/path/file.zip”, @”C:\local\path\file.zip”); If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, transfer resuming, etc), use FtpWebRequest. … Read more

C# Download all files and subdirectories through FTP

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 … Read more

Output log using FtpWebRequest

You can do this using Network Tracing. To set it up, create (or modify, if you already have one) App.config file, so that it looks like this (if you already have the file, you will need to add the settings to it): <?xml version=”1.0″ encoding=”utf-8″ ?> <configuration> <system.diagnostics> <sources> <source name=”System.Net” tracemode=”protocolonly” maxdatasize=”1024″> <listeners> <add … Read more