0

I need read file in ftp, but this file start with a "." (hidden Files), for exemple .teste.txt.

I tried read this file using this code:

 FtpWebRequest reqFTP;
 reqFTP = (FtpWebRequest)WebRequest.Create("ftp://" + strFTP + ":" + strPorta + strDiretorio);
 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
 reqFTP.Credentials = new NetworkCredential(strUser, strPass);

 response = (FtpWebResponse)reqFTP.GetResponse();

 reader = new StreamReader(response.GetResponseStream());
 string line = reader.ReadLine();

2 Answers2

0

in this case I put " -al" in the end ftp url, using this code:

 var reqFTP = (FtpWebRequest)WebRequest.Create("ftp://" + strFTP + ":" + strPorta + strDiretorio + " -al");

reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Credentials = new NetworkCredential(strUser, strPass);

response = (FtpWebResponse)reqFTP.GetResponse();

reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
  • The standard way to use `-l` is to use `ListDirectoryDetails`, instead of `ListDirectory`. Then you only need to add the `-a`. – Martin Prikryl Apr 20 '20 at 05:52
-1

Your problem may not be about the file containing a "." or not, by the looks of the code I bet the problem is the lack of a bar "/" between strPorta and strDiretorio.

You can also check if the concatenated string used to create the WebRequest has any kind of typo regarding special characters, if so, try to escape tehm with a "\" like when you use a new line in text "\n".

EDIT

After reading the comment It occurred me that you are in fact trying to list the so called hidden files. In UNIX based systems the "." before the filename is used to flag that file as hidden. The problem here is that the Object that you are using to connect in the FTP does not have the ability to show/list hidden files as seem in this other thread:

FtpWebRequest ListDirectory does not return hidden files

I recommend that you use the solution proposed by the user Jothi Prakash Anandan and try other Libraries if you really need the hidden files.

Balbinator
  • 220
  • 2
  • 9
  • In this case i can connect in ftp, this ftp have many files, but only files start with "." i cant get. For exemplo: i have 2 files, file 1 test.txt and file 2 .test.txt, i can only get test.txt. tks for your help – Gabriel Rodrigues Apr 15 '20 at 13:31