I am trying to grab a handle to a file that has unicode characters in the filename.
For example, I have a file called c:\testø.txt
. If I try new FileInfo("c:\testø.txt")
I get an Illegal characters exception.
Trying again with an escape sequence: new FileInfo("c:\test\u00f8.txt")
and it works! Yay!
So I've got a method to escape non-ASCII characters:
static string EscapeNonAsciiCharacters(string value)
{
StringBuilder sb = new StringBuilder();
foreach (char c in value)
{
if (c > 127)
{
// This character is too big for ASCII
string encodedValue = "\\u" + ((int)c).ToString("x4");
sb.Append(encodedValue);
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
But when I take the output from this method the escape characters seem to be incorrect.
EscapeNonAsciiCharacters("c:\testø.txt") ## => "c:\test\\u00f8.txt"
When I pass that output to the FileInfo
constructor, I get the illegal chars exception again. However, the \
in c:\
seems to be unaltered. When I look at how this character is represented within the StringBuilder in the static method, I see: {c: est\u00f8.txt}
which leads me to believe that the first backslash is being escaped differently.
How can I properly append the characters escaped by the loop in EscapeNonAsciiCharacters
so I don't get the double escape character in my output?