Click here to Skip to main content
15,911,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone! I need to get only name of file (not folder) from fpt server! But I can't do it! Please help me! Thanks!
C#
FtpWebRequest request = (FtpWebRequest)
                WebRequest.Create(ftpurl);

            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.Credentials = new NetworkCredential(username,
                password);
            try
            {
                FtpWebResponse response = (FtpWebResponse)
                    request.GetResponse();

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

                string line=reader.ReadLine();
                while (!string.IsNullOrEmpty(line))
                {
                    Console.WriteLine(line);
                    line = reader.ReadLine();
                }

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

Try using:
C#
using (Stream responseStream = response.GetResponseStream())
{
  using (StreamReader reader = new StreamReader(responseStream))
  {
    string line=reader.ReadLine();
    while (!string.IsNullOrEmpty(line))
    {
      line = System.IO.Path.GetFileName(line);
      Console.WriteLine(line);
      line = reader.ReadLine();
    }
  }
}

Caveat: I haven't tried this!
 
Share this answer
 
use request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

How to: List Directory Contents with FTP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900