Detecting an Invalid Filename

System.IO.Path.InvalidPathChars is an array of invalid characters (good choice of name really) - you could always do an .IndexOf on these characters against a possible filename to check for the existence of invalid characters.

Other than that checking the length against the maximum allowed length (255 characters IIRC) is probably all you would need to do.
 
Depending on what you are doing, you might want to use System.IO.Path to extract the containing directory and then System.IO.Directory.Exists (I think such a function exists) to verify that the path exists.
 
If you're creating a new file than you should check that the Directory exist using System.IO.Directory.Exists like marble_eater suggested. Then you have to check the filename for invalid characters like PlausiblyDamp said.

But if the file already exist then I'd suggest directly using System.IO.File.Exists to check the file.

Hope it helps.
 
The file doesn't exist yet, I just want to check if the name is valid. I was just checking if there was some method in the System.IO namespace, seems kind of weird that there is none. One more thing, I think the char array I want to use is InvalidFileNameChars (I think the InvalidPathChars is for when you are parsing an entire path, is this correct?)
 
Possibly this function may be useful. (Not at compiler, but I think it will work.)
C#:
public Boolean IsFileNameValid(String sFileName)
{
    foreach(char c in System.IO.Path.InvalidPathChars)
    {
        if(sFileName.IndexOf(c) > 0)
            return false;
    }
    return true;
}
Visual Basic:
Public Function IsFileNameValid(ByVal sFileName As String) As Boolean 
 For Each c As Char In System.IO.Path.InvalidPathChars 
   If sFileName.IndexOf(c) > 0 Then 
     Return False 
   End If 
 Next 
 Return True 
End Function
 
I think the code should be (!= -1) because IndexOf() returns -1 if its not in the array (0 is a valid position).
 
The property System.IO.Path.InvalidPathChars doesn't include characters such as '/' or '\'. Adding to that, the property itself si considered obsolete. .Net suggest using the method System.IO.Path.GetInvalidPathChars().

Anyway to check valid file name we should use System.IO.Path.GetInvalidFileNameChars(). The method proposed by Nate is good enough. Still we need to replace the InvalidPathChars with GetInvalidFileNameChars.

I believe this wraps up the problem.
 
I suggested InvalidPathChars so as not to confuse anyone on .Net 1 - the warning will be enough to point any .Net 2 developers to the correct method.

Rather than calling .IndexOf in a loop you could just call .IndexOfAny and pass in the array of characters returned be InvalidPathCharacters (or GetInvalidFileNameChars).
 
System.IO methods do not allow stream syntax and do not check for reserved names. If you want to do these things you have to do it yourself.
 
Lazy way:

Try to create the file. If it fails, report that the file could not be created. If you are using a try ... catch block, you can even display why.

I can remember creating file names with weird stuff like "one©file.txt" back in the DOS days. Most PC users did not know how to create the "©" character from the keyboard, so the file was somewhat safe. And Win95 would not even display the file in Explorer.

But I digress.
 
Your lazy way needs two IO operation. Creating the test file and the deleting the text file. I'd never recommend this. Not even when I'm being lazy. :D Besides we cannot assume a file has an invalid name if it fails to be created. An example would be exceptions resulting from security issues.

CMIIW.
 
Back
Top