Partilhar via


An explanation of GetInvalidPathChars in System.IO, and a nod to GetInvalidFileNameChars [Kit George]

Something that a lot of people note with the Path class is that it has a method called 'GetInvalidPathChars', but the list doesn't return two key values which obviously, are not allowed to be used as part of file names: '*' and '?'.

The reason is that this method is intended mostly to be used by people working with our other IO APIs, where we accept a searchstring (an example is Directory.GetFiles(string path, string pattern)). The searchstring allows you to use '*' and '?' as part of the searchpattern, supporting a rudimentary searching mechanism, much to what you might expect through the command-window. This allows you to write code like so:

// searchstring here is supposedly, from some user input box

bool searchStringIsInvalid searchString.IndexOfAny(Path.GetInvalidPathChars());

So the existing method actually has a specific intent a very good usage pattern. However, many people still want the list of invalidchars, but they also want that list to contain '*' and '?', so that they can determine if a filename or directoryname provided in an input box, is valid. The solution? Keep an eye out for the new method in Whidbey on Path called 'GetInvalidFileNameChars'.

It's often those small features that can help just polish up the solution.

Comments

  • Anonymous
    January 11, 2005
    I think you mean InvalidPathChars field. GetInvalidPathChars does not exist (well at least in 1.1).
  • Anonymous
    January 11, 2005
    Sweet. I've run across this one before, looking forward to a built-in method. Will GetInvalidFileNameChars also proscribe the use of "" and "/", which are of course perfectly reasonable for paths?
  • Anonymous
    January 12, 2005
    The comment has been removed
  • Anonymous
    January 13, 2005
    The comment has been removed
  • Anonymous
    January 13, 2005
    I forgot:-

    path
    path
    ..path
    .path
    c:path

    etc.
  • Anonymous
    January 14, 2005
    The comment has been removed