public struct FTPCredentials { public string FTPServer { get; set; } public string UserName { get; set; } public string Password { get; set; } } public class FTP { public string[] GetFileList(FTPCredentials Credentials) { string[] retVal = null; StringBuilder result = new StringBuilder(); WebResponse response = null; StreamReader reader = null; try { FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(Credentials.FTPServer)); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(Credentials.UserName, Credentials.Password); reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; reqFTP.Proxy = null; reqFTP.KeepAlive = false; reqFTP.UsePassive = false; response = reqFTP.GetResponse(); reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append("\n"); line = reader.ReadLine(); } // to remove the trailing '\n' 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(); } return null; } return retVal; } }and I use it like this:
FTPCredentials credentials = new FTPCredentials { FTPServer = "ftp://ftp.maroisconsulting.com", UserName = "FTPTest", Password = "testftp" }; FTP ftp = new FTP(); string[] fileInfo = ftp.GetFileList(credentials);It's not done. Still need to convert the results into an array. But all I get from this is the root folder. The FTP site and settings are valid if anyone wants to try it. There's a folder there called Test with 2 files in it.