Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:17:57+00:00 2026-05-29T09:17:57+00:00

Hi and thanks for looking! Background I need to pull the file locations (path

  • 0

Hi and thanks for looking!

Background

I need to pull the file locations (path and filename) for all files at a given FTP address.

For files on a mapped network or local drive, this code would work:

foreach(string fileName f in Directory.GetFiles("C\\:SomeDirectory"), "*.*",
                                     SearchOption.AllDirectories)
{
    //do stuff with each file found
}

But this will NOT work over an FTP connection. I have already found this MS documentation which covers the establishing of an FTPWebRequest, but it does not show me how to loop through each file found (in all nested directories as well).

I am using C# within a forms app.

Question

How do I accomplish this:

foreach(string fileName f in Directory.GetFiles("C\\:SomeDirectory"), "*.*",
                                         SearchOption.AllDirectories)
    {
        //do stuff with each file found
    }

With an FTP connection?

Many thanks!!

UPDATE / Final Answer

Special thanks to @sunk for getting this going. I made a minor tweek to his code that makes it fully recursive so that it can drill into nested folders. Here is the final code:

       //A list that holds all file locations in all folders of a given FTP address:
        List<string> fnl= new List<string>(); 

        //A string to hold the base FTP address:
        string ftpBase = "ftp://[SOME FTP ADDRESS]";

        //A button-click event.  Can be a stand alone method as well
        private void GetFileLocations(object sender, EventArgs e)
        {
            //Get the file names from the FTP location:
            DownloadFileNames(ftpBase);

            //Once 'DownloadFileNames' has run, we have populated 'fnl'
            foreach(var f in fnl)
            {
                //do stuff
            }        
        }

        //Gets all files in a given FTP address.  RECURSIVE METHOD:
        public void DownloadFileNames(string ftpAddress)
        {
            string uri = ftpAddress;
            FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
            reqFTP.Credentials = new NetworkCredential("pella", "PellaWA01!");
            reqFTP.EnableSsl = false;
            reqFTP.KeepAlive = false;
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = true;
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream responseStream = response.GetResponseStream();
            List<string> files = new List<string>();
            StreamReader reader = new StreamReader(responseStream);
            while (!reader.EndOfStream)
                files.Add(reader.ReadLine());
            reader.Close();
            responseStream.Dispose();

            //Loop through the resulting file names.
            foreach (var fileName in files)
            {
                var parentDirectory = "";

                //If the filename has an extension, then it actually is 
                //a file and should be added to 'fnl'.            
                if (fileName.IndexOf(".") > 0)
                {
                    fnl.Add(ftpAddress.Replace("ftp://pella.upload.akamai.com/140607/pella/",              "http://media.pella.com/") + fileName);
                }
                else
                {
                //If the filename has no extension, then it is just a folder. 
                //Run this method again as a recursion of the original:
                    parentDirectory += fileName + "/";
                    try
                    {
                        DownloadFileNames(ftpAddress + parentDirectory);
                    }
                    catch (Exception)
                    {
                        //throw;
                    }
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:17:58+00:00Added an answer on May 29, 2026 at 9:17 am

    First of all, you have to get the files name local on your machine using FTPWebRequest.

    WebRequestMethods.Ftp.ListDirectory;
    

    then use foreach {};

    Here is the code:-

    public List<string> DownloadFileNames()
            {
                    string uri = "ftp://" + ftpServerIP + "/";
                    FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    reqFTP.EnableSsl = true;
                    reqFTP.KeepAlive = false;
                    reqFTP.UseBinary = true;
                    reqFTP.UsePassive = Settings.UsePassive;
                    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    List<string> files = new List<string>();
                    StreamReader reader = new StreamReader(responseStream);
                    while (!reader.EndOfStream)
                        files.Add(reader.ReadLine());
                    reader.Close();
                    responseStream.Dispose();
                    return files;
            }
    

    Now you have the List:-

    List<string> FileNameList = DownloadFileNames();
    foreach (var fileName in FileNameList)
    {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for looking. All sincerely helpful answers are voted up. I use a password
Thanks for looking, all helpful answers are voted up. This is my markup. I'm
Hi and thanks for looking! Background I am working on a developer tool for
Hi and thanks for looking! Background I have a workflow that constructs a set
Hi and thanks for looking! Background I have a computing task that requires either
Hello and thanks for looking! Background I currently have a C# method for looping
I need to read line by line from text file (log files from server)
Thanks for looking on this problem. I have a page that is totally valid
Thanks for looking at this question. I wanted to know how can I use
Looking at what's running and nothing jumps out. Thanks!

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.