I am facing problems to manage files in a FTP when they have special characters. For example filenames with ó
or similars.
I give you an example. First I want to list and process each file of a folder in the FTP:
ftp = CType(FtpWebRequest.Create(sFtpPath), FtpWebRequest)
ftp.Method = WebRequestMethods.Ftp.ListDirectory
reader = New StreamReader(ftp.GetResponse().GetResponseStream())
files = reader.ReadToEnd.Split(New String() {NewLine}, StringSplitOptions.RemoveEmptyEntries)
reader.Close()
But this is giving me problems when the file contains special characters because the string I have for the file doesn't "exists" on the FTP, for example:
For Each sFich As String In files
ftp = CType(FtpWebRequest.Create(sFtpPath & "/" & sFich), FtpWebRequest)
ftp.Method = WebRequestMethods.Ftp.DownloadFile
reader = New StreamReader(ftp.GetResponse().GetResponseStream())
'...
Next
For example, the file EXAMPLE_aró
on the FTP, here is retrieved as EXAMPLE_ar□
, so when I try to download the file it says that doesnt exist.
How can I deal with that?
Judging from the answers to this question, you may need to specify the encoding of the StreamReader
you're using to get the file listing. The default is UTF8, you can use other encodings like so:
reader = New StreamReader(ftp.GetResponse().GetResponseStream(), System.Text.Encoding.Unicode)
When I run into encoding issues, I usually copy and paste the garbled string into an editor that can show the hex values for the characters, and then figure out the encoding from there.
The string you have above contains the character 00F3. Your profile says you are in Spain. Putting those two together, with the help of Google I am guessing the FTP server is using the Spanish flavor of EBCDIC, which is code page 1145. So try this:
reader = New StreamReader(ftp.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding(1145))
Edit: Turns out F3 is the acute o in most encodings, including extended ASCII, the EBCDIC one just came up in my search.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With