Validating file names

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Are there any methods that indicate whether a string can be used as a full path for a file? For example, what I'm looking for is a method that would test for things like correct file name format, invalid characters, total number of characters (to make sure the path is not too long), existing local drive letters, etc. Note that the file does not have to exist. Even a method that only tests some of these items would be useful. The best solution that I can come up with is to attempt to create a FileInfo in a Try-Catch-End Try statement. If an exception is thrown then the specified path is invalid. Of course this isn't very good because it requires an exception to be thrown (nor does it test everything that I would like to test)

Thanks for any help
Lance
 
Hi Lance,

With this you should have to find your solution I think.
(I did not really use it as you would, but I think it should be posible with
that)

http://msdn.microsoft.com/library/d.../cpref/html/frlrfsystemiopathmemberstopic.asp

I hope this helps?

Cor
Are there any methods that indicate whether a string can be used as a full
path for a file? For example, what I'm looking for is a method that would
test for things like correct file name format, invalid characters, total
number of characters (to make sure the path is not too long), existing local
drive letters, etc. Note that the file does not have to exist. Even a
method that only tests some of these items would be useful. The best
solution that I can come up with is to attempt to create a FileInfo in a
Try-Catch-End Try statement. If an exception is thrown then the specified
path is invalid. Of course this isn't very good because it requires an
exception to be thrown (nor does it test everything that I would like to
test).
 
Lance,
Short of actually trying to create or open the file, I do not know of any
function to validate a file name.

A quick search of MSDN did not offer any real help either.

Part of the problem you are going to have with said function: is that your
A: running FAT16 may (will) have different rules, then your C: running
FAT32, from your D: running CDFS, from your E: running NTFS, from your F:
running Novel, from your G: running Linux file system, from your H: running
IFS, from your.... (I'm sure there are more types of file systems
supported!)

Also remember a number of functions accept a UNC path, a URI path, or even a
URL path to "open" the file.

NOTE: I am not saying one does not exist! I am saying that I do not know of
one. I am also saying if you create one, remember that different files
systems (readily available as network drives) have different rules for file
names.

I would consider using a System.Test.RegularExpressions.RegEx to create a
pattern that would match most "commonly used" file names and limit names to
that subset. However more then likely I would simply attempt to create/open
file file and handle any exceptions that were thrown.

Hope this helps
Jay

Lance said:
Are there any methods that indicate whether a string can be used as a full
path for a file? For example, what I'm looking for is a method that would
test for things like correct file name format, invalid characters, total
number of characters (to make sure the path is not too long), existing local
drive letters, etc. Note that the file does not have to exist. Even a
method that only tests some of these items would be useful. The best
solution that I can come up with is to attempt to create a FileInfo in a
Try-Catch-End Try statement. If an exception is thrown then the specified
path is invalid. Of course this isn't very good because it requires an
exception to be thrown (nor does it test everything that I would like to
test).
 
Back
Top