Bayeni
Bayeni

Reputation: 1046

Using Character Encoding with streamreader

My program connects to an ftp server and list all the file that are in the specific folder c:\ClientFiles... The issue I'm having is that the files name have some funny character like – i.e. Billing–File.csv, but code removes replace these characters with a dash "-". When I try downloading the files its not found.

I've tried all the encoding types that are in the class Encoding but not is able to accommodate these character.

Please see my code listing the files.

        UriBuilder ub;
        if (rootnode.Path != String.Empty) ub = new UriBuilder("ftp", rootnode.Server, rootnode.Port, rootnode.Path);
        else ub = new UriBuilder("ftp", rootnode.Server, rootnode.Port);
        String uristring = ub.Uri.OriginalString;
        req = (FtpWebRequest)FtpWebRequest.Create(ub.Uri);
        req.Credentials = ftpcred;
        req.UsePassive = pasv;
        req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;            
        try
        {
            rsp = (FtpWebResponse)req.GetResponse();
            StreamReader rsprdr = new StreamReader(rsp.GetResponseStream(), Encoding.UTF8); //this is where the problem is.

Your help or advise will be highly appreciated

Upvotes: 0

Views: 162

Answers (2)

Graffito
Graffito

Reputation: 1718

Try:

StreamReader rsprdr = new StreamReader(rsp.GetResponseStream(), Encoding.GetEncodings(1251)) ;

You may also try "iso-8859-1" instead of 1251

Upvotes: 1

GvS
GvS

Reputation: 52528

Not every encoding has a class in the encoding namespace. You can get a list of all encodings know in your system by using:

 Encoding.GetEncodings()

(MSDN info for GetEncodings).

If you know what the name of the file should be, you can iterate through the list and see what encodings result in the correct filename.

Upvotes: 1

Related Questions