Frage

I have a problem with using Directory.Exists() on a string that contains an accented character.

This is the directory path: D:\ést_test\scenery. It is coming in as a simple string in a file that I am parsing:

[Area.121]
Title=ést_test
local=D:\AITests\ést_test
Layer=121
Active=FALSE
Required=FALSE

My code is taking the local value and adding \scenery to it. I need to test that this exists (which it does) and am simply using:

if (!Directory.Exists(area.Path))
            {
                // some handling code
                area.AreaIsValid = false;
            }

This returns false. It seems that the string handling that I am doing is replacing the accented character. The text visualizer in VS2012 is showing this (directoryManager is just a wrap around System.IO.Directory):

enter image description here

And the warning message as displayed is showing this:

enter image description here

So it seems that the accented character is not being recognized. Searching for this issue does turn up but mostly about removing or replacing the accented character. I am currently using 'normal' string handling. I tried using FileInfo but the path seems to get mangled anyway.

So my first question is how do I get the path stored into a string so that it will pass the Directory.Exists test?

This raises a wider question of non latin characters in path names. I have users all over the world so I can see arabic. Russian, Chinese and so on in paths. How can I handle all of these?

War es hilfreich?

Lösung

The problem is almost certainly that you're loading the file with the wrong encoding. The fact that it's a filename is irrelevant - the screenshots show that you've lost the relevant data before you call Directory.Exists.

You should make sure you know the file encoding (e.g. UTF-8, Cp1252 etc) and then pass that in as an argument into however you're loading the file (e.g. File.ReadAllText). If this isn't enough information to get you going, you'll need to tell us more about the file (to work out what encoding it's in) and more about your code (how you're reading it).

Once you've managed to load the correct data, I'd hope that the file aspect just handles itself automatically.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top