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?