tags:

views:

13349

answers:

6

What is a good regular expression that can validate a text string to make sure it is a valid Windows filename? (AKA not have \/:*?"<>| characters).

I'd like to use it like the following:

// Return true if string is invalid.
if (Regex.IsMatch(szFileName, "<your regex string>"))
{
    // Tell user to reformat their filename.
}
A: 

Why not using the System.IO.FileInfo class, together with the DirectoryInfo class you have a set of usefull methods.

Drejc
+26  A: 

As answered already, GetInvalidFileNameChars should do it for you, and you don't even need the overhead of regular expressions:

if (proposedFilename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
{
  MessageBox.Show("The filename is invalid");
  return;
}
Isak Savo
simple right parenthesy placement mishap in the if statement, but awesome thanks!
Balk
So "C::::::::" is a valid filename?
Andrew
Andrew: no, this is just a rough basic check for characters.. There are lots of other reasons accessing a file with `proposedFilename` may fail: file (doesn't) exist, read-only, path too long, directory doesn't exist, name is a dos device files (like 'nul' or 'prn') etc.
Isak Savo
A: 

I am no regular expressions fundi however I use Regular Expression Library (http://regexlib.com) for all my regular expressions needs. Sometimes it is faster then making your own.

Diago
+3  A: 

This isn't as simple as just checking whether the file name contains any of System.IO.Path.GetInvalidFileNameChars (as mentioned in a couple of other answers already).

For example what if somebody enters a name that contains no invalid chars but is 300 characters long (i.e. greater than MAX_PATH) - this won't work with any of the .NET file APIs, and only has limited support in the rest of windows using the \?\ path syntax. You need context as to how long the rest of the path is to determine how long the file name can be. You can find more information about this type of thing here.

Ultimately all your checks can reliably do is prove that a file name is not valid, or give you a reasonable estimate as to whether it is valid. It's virtually impossible to prove that the file name is valid without actually trying to use it. (And even then you have issues like what if it already exists? It may be a valid file name, but is it valid in your scenario to have a duplicate name?)

Greg Beech
A: 

Path.GetInvalidFileNameChars - Is not a good way. Try this:

if(@"C:\A.txt".IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
{
  MessageBox.Show("The filename is invalid");
  return;
}
_FRED_
It depends on what you want to achive. I came here from a google query and I want to check only for valid file names (not a full path) and I'm pretty sure you can't have a file called "c:\a.txt" inside a folder ;)
SchlaWiener
Yes, for checking only name of the file Path.GetInvalidFileNameChars() acceptable, but Naming Conventions (http://msdn.microsoft.com/en-us/library/aa365247.aspx#naming_conventions) have a lot of interesting rules.
_FRED_
A: 

As FRED points out it fails. To keep on the current track you would need to do the following

string proposedFilename = @"C:\A.txt";
if (proposedFilename.Substring(proposedFilename.LastIndexOf('\\') + 1).Split(System.IO.Path.GetInvalidFileNameChars()).Length > 1)
{ MessageBox.Show("The filename is invalid"); return; }

Or how about...

string proposedFilename = @"C:\A.txt";
try
 {
     System.IO.Path.GetFileName(proposedFilename);
     MessageBox.Show("The filename is invalid");
     return true;
 }
 catch
 {
     MessageBox.Show("The filename is invalid");
     return false;
 }
Norman Skinner