Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions in C# for file name validation

Tags:

c#

regex

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.
}
like image 385
Game_Overture Avatar asked Sep 19 '08 06:09

Game_Overture


People also ask

Are there regular expressions in C?

A regular expression is a sequence of characters used to match a pattern to a string. The expression can be used for searching text and validating input. Remember, a regular expression is not the property of a particular language. POSIX is a well-known library used for regular expressions in C.

What is regular expression with example?

Solution: As we know, any number of a's means a* any number of b's means b*, any number of c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So the regular expression could be: R = a* b* c*

What is regular expression types?

There are also two types of regular expressions: the "Basic" regular expression, and the "extended" regular expression. A few utilities like awk and egrep use the extended expression. Most use the "basic" regular expression.

What Is syntax for regular expression?

(dot) Matches any single character, except a new line. Note: Regular expressions in Content Compliance policies are case sensitive. Note: Regular expressions in Content Compliance policies are case sensitive.


2 Answers

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;
}
like image 144
Isak Savo Avatar answered Oct 02 '22 17:10

Isak Savo


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?)

like image 6
Greg Beech Avatar answered Oct 02 '22 16:10

Greg Beech