Swapnil Gupta
Swapnil Gupta

Reputation: 8941

How to List Directory Contents with FTP in C#?

How to List Directory Contents with FTP in C# ?

I am using below code to List Directory Contents with FTP it is returning result in XML format ,but i want only the name of directory not the whole content.

How i Can do that ?

public class WebRequestGetExample
{
    public static void Main ()
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential ("anonymous","[email protected]");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);

        reader.Close();
        response.Close();
    }
}

MSDN

Upvotes: 50

Views: 105463

Answers (7)

BenSabry
BenSabry

Reputation: 503

Simplest and most Efficient way to Get FTP Directory Contents:

var contents = GetFtpDirectoryContents(new Uri("ftpDirectoryUri"), new NetworkCredential("userName", "password"));

    public static List<string> GetFtpDirectoryContents(Uri requestUri, NetworkCredential networkCredential)
    {
        var directoryContents = new List<string>(); //Create empty list to fill it later.
        //Create ftpWebRequest object with given options to get the Directory Contents. 
        var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods.Ftp.ListDirectory);
        try
        {
            using (var ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse()) //Excute the ftpWebRequest and Get It's Response.
            using (var streamReader = new StreamReader(ftpWebResponse.GetResponseStream())) //Get list of the Directory Contentss as Stream.
            {
                var line = string.Empty; //Initial default value for line.
                do
                {
                    line = streamReader.ReadLine(); //Read current line of Stream.
                    directoryContents.Add(line); //Add current line to Directory Contentss List.
                } while (!string.IsNullOrEmpty(line)); //Keep reading while the line has value.
            }
        }
        catch (Exception) { } //Do nothing incase of Exception occurred.
        return directoryContents; //Return all list of Directory Contentss: Files/Sub Directories.
    }

    public static FtpWebRequest GetFtpWebRequest(Uri requestUri, NetworkCredential networkCredential, string method = null)
    {
        var ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri); //Create FtpWebRequest with given Request Uri.
        ftpWebRequest.Credentials = networkCredential; //Set the Credentials of current FtpWebRequest.

        if (!string.IsNullOrEmpty(method))
            ftpWebRequest.Method = method; //Set the Method of FtpWebRequest incase it has a value.
        return ftpWebRequest; //Return the configured FtpWebRequest.
    }

Upvotes: 1

Pabitra Dash
Pabitra Dash

Reputation: 1513

There is a method GetDirectoryInformation() in following link which fetches files and directories recursively from a FTP directory.

Getting files from FTP directory recursively

Upvotes: 0

DarkFurious
DarkFurious

Reputation: 1

If you want to list the name of the files that are inside de directory, you have to put (reqFTP.Proxy = null;) before you invoke (reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;).

Hope this can help you!

Upvotes: -1

mint
mint

Reputation: 3433

Try this:

FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
ftpRequest.Credentials =new NetworkCredential("anonymous","[email protected]");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());

List<string> directories = new List<string>();

string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
    directories.Add(line);
    line = streamReader.ReadLine();
}

streamReader.Close();

It gave me a list of directories... all listed in the directories string list... tell me if that is what you needed

Upvotes: 70

Konektiv
Konektiv

Reputation: 11

Some proxies reformat the directory listing, so it's quite difficult to parse a directory listing reliably unless you can guarantee that the proxy doesn't change

Upvotes: 1

Iain Ward
Iain Ward

Reputation: 9936

You need ListDirectory that lists the directory contents

EDIT: Or you can use this Chilkat library that wraps it up nicely for you

Upvotes: 1

leppie
leppie

Reputation: 117220

You are probably looking for PrintWorkingDirectory

Upvotes: 1

Related Questions