Consider following code:
public static bool isValidPath(string path) {
return !Path.GetInvalidPathChars().Any(path.Contains);
}
var isValid = isValidPath("C:\Users\Public\Documents\"");
Yes, the path contains a double quote at the end: C:UsersPublicDocuments"
My code returns true
for isValid
.
But when I later try to create the path, I get an System.IO.IOException
exception because the path is not correct.
Why does Path.GetInvalidPathChars()
not return an "
? I tried different ideas as well, like creating a DirectoryInfo
object, but I get no exception. Path.GetFullPath
returns the path with the double quote as well.
I don’t want to create the path to check if it’s valid, as it’s creation is much further down the pipe.
I’m on Windows, and my app has to work on Unix as well