Access and list files on an FTP server

Asked

Viewed 47 times

1

Hello, I would like to access and list folder/PDF’s files on an FTP server using an App, I use fragment and the version of Android and 4 until the current 9. What I researched so far I could not solve this problem, I found nothing concrete.

Someone could give me the way?

1 answer

0

Try this if it doesn’t work I’ll send another (String Path,String user,String pass):

static List<DirectoryItem> GetDirectoryInformation(string address, string username, string password) {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            List<DirectoryItem> returnValue = new List<DirectoryItem>();
            string[] list = null;

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                list = reader.ReadToEnd().Split(new string[] { "\r\n"},StringSplitOptions.RemoveEmptyEntries);
            }

            foreach (string line in list) {

                string data = line;
                string date = data.Substring(0, 17);
                DateTime dateTime = DateTime.Parse(date);
                data = data.Remove(0, 24);
                string dir = data.Substring(0, 5);
                bool isDirectory = dir.Equals("<dir>", StringComparison.InvariantCultureIgnoreCase);
                data = data.Remove(0, 5);
                data = data.Remove(0, 10);

                string name = data;

                DirectoryItem item = new DirectoryItem();
                item.BaseUri = new Uri(address);
                item.DateCreated = dateTime;
                item.IsDirectory = isDirectory;
                item.Name = name;

                Debug.WriteLine(item.AbsolutePath);
                item.Items = item.IsDirectory ? GetDirectoryInformation(item.AbsolutePath, username, password) : null;

                returnValue.Add(item);
            }

            return returnValue;
        }
  • Where is this List<DirectoryItem> ?

  • I didn’t get @Gonçalo Garrido, I don’t know if it’s because I’m using a fragment, I can’t import the classes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.