0

I'm using this code to read files from ftp server:

static List<String> GetDirectory(String DirectoryName)
    {
        try
        {
            var ConnectionString = String.Format("{0}/{1}", Connection.GetFTPConfig.FTPSourceAddress, DirectoryName);
            var request = (FtpWebRequest)WebRequest.Create(ConnectionString);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            request.Credentials = new NetworkCredential(Connection.GetFTPConfig.FTPSourceUsername, Connection.GetFTPConfig.FTPSourcePassword);

            var Directories = new List<String>();
            var response = (FtpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                string line = streamReader.ReadLine();
                while (!string.IsNullOrEmpty(line))
                {
                    Directories.Add(line);
                    line = streamReader.ReadLine();
                }
            }

            response.Close();
            return Directories;
        }
        catch (Exception ex)
        {
            return new List<String>();
        }
    }

DirectoryName is the address of my folder, and I want to read all files in this folder. This code always works fine, But when DirectoryName contains "#", this line string line = streamReader.ReadLine(); returns null.

I checked the streamReader. Before ReadLine(), the value of streamReader.EndOfStream is True. But after that, the value of streamReader.EndOfStream changes to:
'streamReader.EndOfStream' threw an exception of type 'System.ObjectDisposedException'

When I change the folder name it works fine. For example, my folder name is "2014-06-18_06-58-47-529_14#42411-R" and the file name in the folder is "2014-06-18_06-58-47-529_14#42411-R.jpg". When I change the # in the folder name, to "2014-06-18_06-58-47-529_14142411-R", it works fine and return the list of files in the folder! What's the problem?

Soheila Hg
  • 569
  • 2
  • 7
  • 25
  • 2
    I very much doubt that this has anything to do with `StreamReader`. I suspect this is more a matter of the FTP server not finding the files. I suggest you look at what's being returned on the wire, e.g. with Wireshark. – Jon Skeet Jun 18 '14 at 10:30
  • @sloth I waste so much time to search for this problem but never seen this question before. thank you. – Soheila Hg Jun 18 '14 at 10:42
  • @SoheilaHg First google hit for `c# ftp special character name` – sloth Jun 18 '14 at 10:45
  • @sloth it's never came to my mind to google `special character name`. I always searched for `# in file name`. – Soheila Hg Jun 18 '14 at 10:51

0 Answers0