2

I am trying to get a list of files from FTP. but instead I am getting the name of the directory where the files are saved

    List<string> file_names = new List<string>();
    try
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Host + "Data");
        request.Method = WebRequestMethods.Ftp.ListDirectory;

        request.Credentials = new NetworkCredential(UserID, Password);

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

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

        print(files); // prints "Data" instead of list of file names

        file_names = files.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();


        Debug.Log($"Directory List Complete, status {response.StatusDescription}");

        reader.Close();
        response.Close();
    }
    catch(WebException e)
    {
        print(e.Message + "\tCould not get files from server");
    }

What am I doing wrong ?

CrashCZ
  • 31
  • 4

1 Answers1

1

I found this third party FTP library which makes it much more simple.

List<string> files = new List<string>();
    FtpClient client = new FtpClient("192.168.0.125", 2221, new NetworkCredential(UserID, Password));
    //client.Credentials = new NetworkCredential(UserID, Password);
    client.Connect();
    foreach(FtpListItem item in client.GetListing("/Data"))
    {
        if (item.Type == FtpFileSystemObjectType.File)
        {
            files.Add(item.Name);
            print(item.Name);
        }
            
    }
CrashCZ
  • 31
  • 4