In C# check that filename is *possibly* valid (not that it exists)
c#, file, validation
Solution
Just do;
System.IO.FileInfo fi = null;
try {
fi = new System.IO.FileInfo(fileName);
}
catch (ArgumentException) { }
catch (System.IO.PathTooLongException) { }
catch (NotSupportedException) { }
if (ReferenceEquals(fi, null)) {
// file name is not valid
} else {
// file name is valid... May check for existence by calling fi.Exists.
}
For creating a `FileInfo` instance the file does not need to exist.
Problem
Is there a method in the System.IO namespace that checks the validity of a filename? For example, `C:\foo\bar` would validate and `:"~-*` would not Or a little trickier, `X:\foo\bar` would validate is there is an `X:` drive on the system, but wouldn't otherwise. I suppose I could write such a method myself, but I'm more interested in a built-in one.