My paths are UTF-16 strings. Most of them only use the ASCII set, so a filename like test would be stored as
T \x00 E \x00 S \x00 T \x00
I use Encoding.Unicode.GetString(bytes) to read the string and it works fine (when I print them out to console or to a form control it appears as I expect), but when I want to actually create a file with the given filename using the following code
BinaryWriter outFile = new BinaryWriter(File.OpenWrite(path));
I get an exception
Unhandled Exception: System.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path)
at System.IO.Path.GetFileName(String path)
It’s probably because there are null chars in there (maybe it stores the original byte array internally), but I’m not sure how to deal with it. Not all of the strings are ASCII though, and some of the characters use two-bytes.
UPDATE:
Turns out the illegal bytes were simply null bytes that were padded to the string. However, I can’t simply strip all trailing null bytes, but I don’t know the length of the string either. How would I strip null bytes from a string where each character is stored in n bytes?
From the MSDN on `Path.GetInvalidPathChars’
You could use
Path.GetInvalidPathCharsas a filter. Copy your input string to an output string while filtering any characters that match a character fromPath.CheckInvalidPathChars.Here’s an example I cooked up:
Note that most symbols are filtered out, but the tildes are not, since they are valid path characters.