How to obtain invalid file name characters

  • Thread starter Thread starter Nathan
  • Start date Start date
N

Nathan

Hi,

How does one obtain a platform's invalid file name characters from the
..NET framework? I have noticed that invalid path characters can be
obtained from System.IO.Path.InvalidPathChars but this list does not
include items that would be illegal for file names (for example, a
colon: would be invalid in a MS Windows environment).

Please note that catching an exception from Path.GetFileName() would
allow me to test the validity of a file name but would not allow me to
include the invalid characters in a message to the user.

Many thanks.
Nathan
 
Illegal file name characters are system dependent. The best you can do is
exactly what you are doing,
 
John Saunders said:
Nathan,

I don't quite know the answer, but I have a comment about the question
itself. :-)

Any technique you use should take into appropriate consideration the chance
that the set of invalid characters will change during the lifetime of your
program. Not that your program wouldn't always be able to call something
like a hypothetical System.IO.Path.InvalidFilenameCharacters. But if you
intend to display this list to users as a means of educating them, consider
that the list may change in the future. In fact, we already have already
experienced Windows version changes where a certain set of characters would
have been valid yesterday, but today they've upgraded and the same volume
now allows different characters!

Also, if networked volumes are allowed, it may well be the case that
different volumes have different lists of invalid characters. Consider that
the volumes may, in general, be hosted on different platforms...

Now, most applications don't have to worry about this sort of thing (or
don't think they have to). I should warn you that this comment is coming to
you from someone with experience in writing and using cross-platform file
sharing servers! But, these days things move so fast, that even a
"Windows-only" organization could well find itself in a cross-platform
situation - where the two platforms are simply different versions of
Windows.
Thank you for your post. I was especially interested in your comments
regarding illegal file name characters in a cross-platform file
sharing situation. This concept may come into play in later
iterations of the software that we are developing.
Based on the posts that I have received I believe that we will avoid
displaying invalid characters.
Thanks again.
Nathan
 
Nathan,
As the others have stated, it is system dependent. ;-)

In addition to Path.InvalidPathChars, I would check:

Path.PathSeparator
Path.VolumeSeparatorChar
Path.AltDirectorySeparatorChar
Path.DirectorySeparatorChar

The colon happens to be the VolumeSeparatorChar.

Hope this helps
Jay
 
Back
Top