Easiest way to check if an arbitrary String is a valid filename
In my application the user can enter a filename. Before processing I'd like to check if the input String is a valid filename on Windows Vista.
Whats the easiest way to do that?
By valid I'm reffering to legal and non-existing
Check whether filename.IndexOfAny(Path.GetInvalidFileNameChars()) < 0
and !File.Exists(Path.Combine(someFolder, filename))
Check against GetInvalidFileNameChars()
:
var isValid = !string.IsNullOrEmpty(fileName) &&
fileName.IndexOfAny(Path.GetInvalidFileNameChars()) < 0 &&
!File.Exists(Path.Combine(sourceFolder, fileName));