File.Exists and the accentuation

Asked

Viewed 163 times

4

I have a system developed in Webforms (Eca!) that checks the existence of an image to then display it.

Everything is working correctly, however, when the image path has accents, the method File.Exists seems to be returning false.

What can I do to get around it?

Example:

 string img1 = Server.MapPath("\Fotos\Imagem.jpg");
 string img2 = Server.MapPath("\Fotos\Imagem-com-acentuação.jpg");

Console.WriteLine(File.Exists(img1)); // true
Console.WriteLine(File.Exists(img2)); // false

Update: I wrote and tested the existence of a manually accented file and it worked. I begin to suspect that the problem is in assembling the file path, which was done as follows:

string imgUrl = "~\\Fotos\\Fotos_" + Codigo + "\\" + e.Row.Cells[20].Text;
  • 3

    tries to use Htmlencode: string img2 = Server.MapPath(Server.HtmlEncode("\\Fotos\\Imagem-com-acentuação.jpg"));

  • @Rovannlinhalis there, let me test here. If the Text is sending the encoded values, the problem is probably the same.

  • @Rovannlinhalis OPA! That’s right, kkkkk. The path was being written like this: Fotos\Fotos_98\Tiãozão.jpg. I’ll change it here, I found a bad idea of who made take the right value of HTML :\

  • Please answer the question :p

  • 1

    Well, actually, wouldn’t it be the other way around? I think I should use "Decode"?

  • I already improve the answer...busy here =] I’m glad you solved =]

Show 1 more comment

2 answers

2


It is possible that the file name string is being encoded in HTML, you can reverse this by using HtmlEncode and HtmlDecode:

string arquivo = "Imagem-com-acentuação.jpg";

string codificada = Server.HtmlEncode(arquivo); //Imagem-com-acentuação.jpg

string decodificada = Server.HtmlDecode(codificada); //Imagem-com-acentuação.jpg

As you explained in the comments, the file name is already being passed encoded, so your code would look like this:

string img2 = Server.MapPath(Server.HtmlDecode(codificada));

0

I’ve seen a code here never class that lists a directory with a comment to "handle paths with accents". When I saw your question I remembered this and went after. This is the code (I wrote some path to illustrate):

var path = @"c:\teste\senão\término\um path com textão.txt";
var pathD = path.Normalize(System.Text.NormalizationForm.FormD);

If you look at the value of the two variables, it appears to be the same, to be tested with path == pathD false return.

I went to read more about the method Normalize and from what I saw, it changes something in the binary representation of the string, changing Unicode characters. The visualization is the same, but the comparison (including the File.Exists) mute.

Here works well with File.Exists. For example I made a fiddle: https://dotnetfiddle.net/8PcNcp

  • Thank you for the answer, but in fact, as described in the comments, the problem was originated by the fact that the image path is being mounted on top of a value extracted from HTML (which comes encoded).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.