Hi..

I need to list the directory contets in an FTP folder.

Here my requirment is listing the ditectory contents into an array list of directory items.

Directory item contains Name, Full Path, Size and Type (File or Subdirectory).

Can any one help.

Thank you,

Sravanthi Ch

This is the way i do it:

  public static List<string> GetFtpDirectoryDetails(NetworkCredential cred, string strDir)
  {
     List<string> lst_strFiles = new List<string>();
     try
     {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strDir);
        request.Credentials = cred;
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        //
        StreamReader file = new StreamReader(response.GetResponseStream());

        while (!file.EndOfStream)
        {
           lst_strFiles.Add(file.ReadLine());
        }

        file.Close();
        response.Close();
     }
     catch (Exception exc)
     {
        lst_strFiles.Add(exc.Message);
     }

     return lst_strFiles;
  }

This will gives the list of files and directories at a time..But i need the directory structure itself

Have you looked at (then) WebRequestMethods.Ftp.ListDirectory?

Or maybe you're looking for subdirectories.
Anyway, something here should work.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace FtpDir
{
   public class CFtpDir
   {
      public static List<string> GetFtpDirectory(NetworkCredential cred, string strDir)
      {
         List<string> lst_strFiles = new List<string>();
         try
         {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strDir);
            request.Credentials = cred;
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            //
            StreamReader file = new StreamReader(response.GetResponseStream());

            while (!file.EndOfStream)
            {
               lst_strFiles.Add(file.ReadLine());
            }

            file.Close();
            response.Close();
         }
         catch (Exception exc)
         {
            lst_strFiles.Add(exc.Message);
         }

         lst_strFiles.Sort();
         return lst_strFiles;
      }

      public static List<string> GetFtpDirectoryDetails(NetworkCredential cred, string strDir)
      {
         List<string> lst_strFiles = new List<string>();
         try
         {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strDir);
            request.Credentials = cred;
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            //
            StreamReader file = new StreamReader(response.GetResponseStream());

            while (!file.EndOfStream)
            {
               lst_strFiles.Add(file.ReadLine());
            }

            file.Close();
            response.Close();
         }
         catch (Exception exc)
         {
            lst_strFiles.Add(exc.Message);
         }

         return lst_strFiles;
      }

      public static List<string> GetSubDirectories(NetworkCredential cred, string strDir)
      {
         Regex rxDirectory = new Regex("^d.* ftp ftp *0 ");
         List<string> lst_strDirectories = new List<string>();

         GetFtpDirectoryDetails(cred, strDir).ForEach(s =>
         {
            if (rxDirectory.IsMatch(s))
            {
               lst_strDirectories.Add(LastColumn(s));
            }
         });

         return lst_strDirectories;
      }

      private static string LastColumn(string strData)
      {
         string[] arr_strData = strData.Split(' ');
         return arr_strData[arr_strData.Length - 1];
      }

      public static double GetFtpFileSize(NetworkCredential cred, string strUri)
      {
         double dblRetVal = 0;
         try
         {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strUri);
            request.Credentials = cred;
            request.Method = WebRequestMethods.Ftp.GetFileSize;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            dblRetVal = response.ContentLength;
            //
            response.Close();
         }
         catch (Exception)
         {
            dblRetVal = -1;
         }

         return dblRetVal;
      }

      public static bool DeleteFilesOnServer(NetworkCredential cred, List<string> lst_strFiles, ref string strError)
      {
         bool blnRetVal = true;

         try
         {
            foreach (string strUri in lst_strFiles)
            {
               FtpWebRequest request = (FtpWebRequest)WebRequest.Create(strUri);
               request.Credentials = cred;
               request.Method = WebRequestMethods.Ftp.DeleteFile;
               FtpWebResponse response = (FtpWebResponse)request.GetResponse();
               response.Close();
            }
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.