How do I check if a given string is a legal/valid file name under Windows?
Asked Answered
C

27

181

I want to include a batch file rename functionality in my application. A user can type a destination filename pattern and (after replacing some wildcards in the pattern) I need to check if it's going to be a legal filename under Windows. I've tried to use regular expression like [a-zA-Z0-9_]+ but it doesn't include many national-specific characters from various languages (e.g. umlauts and so on). What is the best way to do such a check?

Canonist answered 15/9, 2008 at 13:17 Comment(1)
I suggest using a static compiled Regex if you are going to use any of the answers with Regex..Soprano
S
101

You can get a list of invalid characters from Path.GetInvalidPathChars and GetInvalidFileNameChars.

UPD: See Steve Cooper's suggestion on how to use these in a regular expression.

UPD2: Note that according to the Remarks section in MSDN "The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names." The answer provided by sixlettervaliables goes into more details.

Suburbia answered 15/9, 2008 at 13:22 Comment(3)
This does not answer the question; there are many strings consisting only of valid characters (e.g. "....", "CON", strings hundreds of chars long) that are not valid filenames.Haploid
Anyone else disappointed that MS doesn't provide system level function/API for this capability instead of each developer has to cook his/her own solution? Wondering if there's a very good reason for this or just an oversight on MS part.Hendricks
@High Arch: See answer for question "In C# check that filename is possibly valid (not that it exists)". (Although some clever guys closed that question in favour of this one...)Sera
S
143

From MSDN's "Naming a File or Directory," here are the general conventions for what a legal file name is under Windows:

You may use any character in the current code page (Unicode/ANSI above 127), except:

  • < > : " / \ | ? *
  • Characters whose integer representations are 0-31 (less than ASCII space)
  • Any other character that the target file system does not allow (say, trailing periods or spaces)
  • Any of the DOS names: CON, PRN, AUX, NUL, COM0, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT0, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9 (and avoid AUX.txt, etc)
  • The file name is all periods

Some optional things to check:

  • File paths (including the file name) may not have more than 260 characters (that don't use the \?\ prefix)
  • Unicode file paths (including the file name) with more than 32,000 characters when using \?\ (note that prefix may expand directory components and cause it to overflow the 32,000 limit)
Subaqueous answered 15/9, 2008 at 13:30 Comment(13)
+1 for including reserved filenames - those were missed in previous answers.Lacteous
"AUX" is a perfectly usable filename if you use the "\\?\" syntax. Of course, programs that don't use that syntax have real problems dealing with it... (Tested on XP)Sailing
The correct regex for all these conditions mentioned above is as below:Regex unspupportedRegex = new Regex("(^(PRN|AUX|NUL|CON|COM[1-9]|LPT[1-9]|(\\.+)$)(\\..*)?$)|(([\\x00-\\x1f\\\\?*:\";|/<>])+)|(([\\. ]+)", RegexOptions.IgnoreCase);Evelinaeveline
@Evelinaeveline I think you've got an extra opening bracket in that Regex. "(^(PRN|AUX|NUL|CON|COM[1-9]|LPT[1-9]|(\\.+)$)(\\..*)?$)|(([\\x00-\\x1f\\\\?*:\";‌​|/<>])+)|([\\. ]+)" worked for me.Leonleona
Wilky: your regex will also remove "." within the filename which are perfectly valid.Guild
This is better: (^(PRN|AUX|NUL|CON|COM[1-9]|LPT[1-9]|(\\.+)$)(\\..*)?$)|(([\\x00-\\x1f\\\\?*:\"​|/<>‌​])+)|(^([\\.]+))Guild
All regexes above reject filenames that begin with '.', which is allowed by the OS.Tunisia
Depends on how you define "allowed". Windows allows filenames that begin with a dot but Explorer does not let you name a file as such, unless if also has an extension. For example, .foo is not allowed, but .foo.bar is.Laurelaureano
I read the same article mentioned in this answer and found through experimentation that COM0 and LPT0 are also not allowed. @Tunisia this one works with filenames that begin with '.': ^(?!^(?:PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d)(?:\..+)?$)(?:\.*?(?!\.))[^\x00-\x1f\\?*:\";|\/<>]+(?<![\s.])$Manstopper
Is there a library which handles all of these cases?Merv
@papaiatis -- "CLOCK$" works just fine for me. Windows 7.Beach
BTW "the file name is all periods" rule is already contained in "trailing periods or spaces rule"Pawl
None of those regexes work properly.Infield
S
101

You can get a list of invalid characters from Path.GetInvalidPathChars and GetInvalidFileNameChars.

UPD: See Steve Cooper's suggestion on how to use these in a regular expression.

UPD2: Note that according to the Remarks section in MSDN "The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names." The answer provided by sixlettervaliables goes into more details.

Suburbia answered 15/9, 2008 at 13:22 Comment(3)
This does not answer the question; there are many strings consisting only of valid characters (e.g. "....", "CON", strings hundreds of chars long) that are not valid filenames.Haploid
Anyone else disappointed that MS doesn't provide system level function/API for this capability instead of each developer has to cook his/her own solution? Wondering if there's a very good reason for this or just an oversight on MS part.Hendricks
@High Arch: See answer for question "In C# check that filename is possibly valid (not that it exists)". (Although some clever guys closed that question in favour of this one...)Sera
B
68

For .Net Frameworks prior to 3.5 this should work:

Regular expression matching should get you some of the way. Here's a snippet using the System.IO.Path.InvalidPathChars constant;

bool IsValidFilename(string testName)
{
    Regex containsABadCharacter = new Regex("[" 
          + Regex.Escape(System.IO.Path.InvalidPathChars) + "]");
    if (containsABadCharacter.IsMatch(testName)) { return false; };

    // other checks for UNC, drive-path format, etc

    return true;
}

For .Net Frameworks after 3.0 this should work:

http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars(v=vs.90).aspx

Regular expression matching should get you some of the way. Here's a snippet using the System.IO.Path.GetInvalidPathChars() constant;

bool IsValidFilename(string testName)
{
    Regex containsABadCharacter = new Regex("["
          + Regex.Escape(new string(System.IO.Path.GetInvalidPathChars())) + "]");
    if (containsABadCharacter.IsMatch(testName)) { return false; };

    // other checks for UNC, drive-path format, etc

    return true;
}

Once you know that, you should also check for different formats, eg c:\my\drive and \\server\share\dir\file.ext

Breccia answered 15/9, 2008 at 13:26 Comment(6)
doesn't this only test the path, not the filename?Suburbia
string strTheseAreInvalidFileNameChars = new string( System.IO.Path.GetInvalidFileNameChars() ) ; Regex regFixFileName = new Regex("[" + Regex.Escape(strTheseAreInvalidFileNameChars ) + "]");Ailing
A little research from people would work wonders. I've updated the post to reflect the changes.Simply
2nd piece of code doesn't compile. "Cannot convert from char[] to stringAthalie
+1 for the code, but please replace Path.GetInvalidPathChars() with Path.GetInvalidFileNameChars() as Path.GetInvalidPathChars() is obsolete nowCaen
@AshkanMobayenKhiabani: InvalidPathChars is obsolete but GetInvalidPathChars does not.Traffic
H
26

Try to use it, and trap for the error. The allowed set may change across file systems, or across different versions of Windows. In other words, if you want know if Windows likes the name, hand it the name and let it tell you.

Havildar answered 15/9, 2008 at 14:0 Comment(5)
This seems to be the only one that tests against all constraints. Why are the other answers being chosen over this?Neuter
@Neuter because it doesn't always work. For example, trying to access CON will often succeed, even though it's not a real file.Brassica
It's always better to avoid the memory overhead of throwing an Exception, where possible, though.Stratfordonavon
Also, you might not have permissions to access it; e.g. to test it by writing, even if you can read it if it does or will exist.Saury
@OwenBlacker That's needless preoptimization.Report
B
24

This class cleans filenames and paths; use it like

var myCleanPath = PathSanitizer.SanitizeFilename(myBadPath, ' ');

Here's the code;

/// <summary>
/// Cleans paths of invalid characters.
/// </summary>
public static class PathSanitizer
{
    /// <summary>
    /// The set of invalid filename characters, kept sorted for fast binary search
    /// </summary>
    private readonly static char[] invalidFilenameChars;
    /// <summary>
    /// The set of invalid path characters, kept sorted for fast binary search
    /// </summary>
    private readonly static char[] invalidPathChars;

    static PathSanitizer()
    {
        // set up the two arrays -- sorted once for speed.
        invalidFilenameChars = System.IO.Path.GetInvalidFileNameChars();
        invalidPathChars = System.IO.Path.GetInvalidPathChars();
        Array.Sort(invalidFilenameChars);
        Array.Sort(invalidPathChars);

    }

    /// <summary>
    /// Cleans a filename of invalid characters
    /// </summary>
    /// <param name="input">the string to clean</param>
    /// <param name="errorChar">the character which replaces bad characters</param>
    /// <returns></returns>
    public static string SanitizeFilename(string input, char errorChar)
    {
        return Sanitize(input, invalidFilenameChars, errorChar);
    }

    /// <summary>
    /// Cleans a path of invalid characters
    /// </summary>
    /// <param name="input">the string to clean</param>
    /// <param name="errorChar">the character which replaces bad characters</param>
    /// <returns></returns>
    public static string SanitizePath(string input, char errorChar)
    {
        return Sanitize(input, invalidPathChars, errorChar);
    }

    /// <summary>
    /// Cleans a string of invalid characters.
    /// </summary>
    /// <param name="input"></param>
    /// <param name="invalidChars"></param>
    /// <param name="errorChar"></param>
    /// <returns></returns>
    private static string Sanitize(string input, char[] invalidChars, char errorChar)
    {
        // null always sanitizes to null
        if (input == null) { return null; }
        StringBuilder result = new StringBuilder();
        foreach (var characterToTest in input)
        {
            // we binary search for the character in the invalid set. This should be lightning fast.
            if (Array.BinarySearch(invalidChars, characterToTest) >= 0)
            {
                // we found the character in the array of 
                result.Append(errorChar);
            }
            else
            {
                // the character was not found in invalid, so it is valid.
                result.Append(characterToTest);
            }
        }

        // we're done.
        return result.ToString();
    }

}
Breccia answered 6/8, 2010 at 16:16 Comment(1)
your answer could be better fit here:#146634Merv
T
23

This is what I use:

    public static bool IsValidFileName(this string expression, bool platformIndependent)
    {
        string sPattern = @"^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\"";|/]+$";
        if (platformIndependent)
        {
           sPattern = @"^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?))\\)*[^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?$";
        }
        return (Regex.IsMatch(expression, sPattern, RegexOptions.CultureInvariant));
    }

The first pattern creates a regular expression containing the invalid/illegal file names and characters for Windows platforms only. The second one does the same but ensures that the name is legal for any platform.

Tamatave answered 15/9, 2008 at 14:11 Comment(7)
sPattern regex doesn't allow files started with period character. But MSDN says "it is acceptable to specify a period as the first character of a name. For example, ".temp"". I would remove "\..*" to make .gitignore correct file name :)Cavell
(I have incrementally made this better and deleted prev comments I left) This one is better than the answer's regex because it allows ".gitignore", "..asdf", doesn't allow '<' and '>' or the yen sign, and doesn't allow space or period at the end (which disallows names consisting only of dots): @"^(?!(?:PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d)(?:\..+)?$)[^\x00-\x1F\xA5\\?*:\"";|\/<>]+(?<![\s.])$"Manstopper
this fails for all files I tested. running it for C:\Windows\System32\msxml6.dll reports false.Zimmer
@Zimmer You need to give it just the file name, not the fully qualified path.Tamatave
ok, but I need to check if the full path is valid. I used now a different solution.Zimmer
Your pattern fails on .foo.bar.Beach
It also allows < and >.Beach
M
19

One corner case to keep in mind, which surprised me when I first found out about it: Windows allows leading space characters in file names! For example, the following are all legal, and distinct, file names on Windows (minus the quotes):

"file.txt"
" file.txt"
"  file.txt"

One takeaway from this: Use caution when writing code that trims leading/trailing whitespace from a filename string.

Mcinnis answered 19/9, 2008 at 13:11 Comment(0)
L
9

Simplifying the Eugene Katz's answer:

bool IsFileNameCorrect(string fileName){
    return !fileName.Any(f=>Path.GetInvalidFileNameChars().Contains(f))
}

Or

bool IsFileNameCorrect(string fileName){
    return fileName.All(f=>!Path.GetInvalidFileNameChars().Contains(f))
}
Libbie answered 3/3, 2017 at 22:24 Comment(5)
Did you mean : "return !fileName.Any(f=>Path.GetInvalidFileNameChars().Contains(f));" ?Lycian
@JackGriffin Of course! Thank you for your attentiveness.Libbie
While this code is very nice to read, we should take into account the sorry internals of Path.GetInvalidFileNameChars. Take a look here: referencesource.microsoft.com/#mscorlib/system/io/path.cs,289 - for each character of your fileName, a clone of the array is created.Questionary
"DD:\\\\\AAA.....AAAA". Not valid, but for your code, it is.Cuprous
Are you really comfortable with calling Path.GetInvalidFileNameChars() for each character of the file name?Alumina
K
8

Microsoft Windows: Windows kernel forbids the use of characters in range 1-31 (i.e., 0x01-0x1F) and characters " * : < > ? \ |. Although NTFS allows each path component (directory or filename) to be 255 characters long and paths up to about 32767 characters long, the Windows kernel only supports paths up to 259 characters long. Additionally, Windows forbids the use of the MS-DOS device names AUX, CLOCK$, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, CON, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, NUL and PRN, as well as these names with any extension (for example, AUX.txt), except when using Long UNC paths (ex. \.\C:\nul.txt or \?\D:\aux\con). (In fact, CLOCK$ may be used if an extension is provided.) These restrictions only apply to Windows - Linux, for example, allows use of " * : < > ? \ | even in NTFS.

Source: http://en.wikipedia.org/wiki/Filename

Kyliekylila answered 15/9, 2008 at 13:25 Comment(1)
I can create a file named "CLOCK$" just fine. Windows 7.Beach
M
7

Rather than explicitly include all possible characters, you could do a regex to check for the presence of illegal characters, and report an error then. Ideally your application should name the files exactly as the user wishes, and only cry foul if it stumbles across an error.

Menjivar answered 15/9, 2008 at 13:19 Comment(0)
T
6

The question is are you trying to determine if a path name is a legal windows path, or if it's legal on the system where the code is running.? I think the latter is more important, so personally, I'd probably decompose the full path and try to use _mkdir to create the directory the file belongs in, then try to create the file.

This way you know not only if the path contains only valid windows characters, but if it actually represents a path that can be written by this process.

Trickster answered 15/9, 2008 at 13:27 Comment(0)
J
6

I use this to get rid of invalid characters in filenames without throwing exceptions:

private static readonly Regex InvalidFileRegex = new Regex(
    string.Format("[{0}]", Regex.Escape(@"<>:""/\|?*")));

public static string SanitizeFileName(string fileName)
{
    return InvalidFileRegex.Replace(fileName, string.Empty);
}
Janssen answered 25/2, 2013 at 17:24 Comment(0)
U
5

Also CON, PRN, AUX, NUL, COM# and a few others are never legal filenames in any directory with any extension.

Undulation answered 15/9, 2008 at 13:24 Comment(2)
This is only half of the truth. You can create files with these names if calling the unicode version of CreateFile (prefixing the file name with "\\?\").Makassar
This statement is incomplete and misses LPT#Pedicular
B
4

From MSDN, here's a list of characters that aren't allowed:

Use almost any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

  • The following reserved characters are not allowed: < > : " / \ | ? *
  • Characters whose integer representations are in the range from zero through 31 are not allowed.
  • Any other character that the target file system does not allow.
Brahmi answered 15/9, 2008 at 13:20 Comment(0)
D
4

To complement the other answers, here are a couple of additional edge cases that you might want to consider.

Dowski answered 19/1, 2012 at 18:52 Comment(0)
U
3

This is an already answered question, but just for the sake of "Other options", here's a non-ideal one:

(non-ideal because using Exceptions as flow control is a "Bad Thing", generally)

public static bool IsLegalFilename(string name)
{
    try 
    {
        var fileInfo = new FileInfo(name);
        return true;
    }
    catch
    {
        return false;
    }
}
Unproductive answered 31/12, 2012 at 19:37 Comment(5)
Your example didn't worked for a CON file (C:\temp\CON).Roubaix
But isn't 'C:\temp\CON' a valid filename? Why wouldn't it be?Bigeye
@MarqueIV -- no, it's not valid. Read all the answers and comments above, or try it yourself and see.Beach
@Jer, "/example" is not legal, yet your method returns true.Beach
Aaaah... I missed the 'CON' part. The name itself is valid from a string standpoint (which is what I was referring to), but I see now CON is a reserved name, making it non-valid from a Windows standpoint. My bad.Bigeye
S
2

Regular expressions are overkill for this situation. You can use the String.IndexOfAny() method in combination with Path.GetInvalidPathChars() and Path.GetInvalidFileNameChars().

Also note that both Path.GetInvalidXXX() methods clone an internal array and return the clone. So if you're going to be doing this a lot (thousands and thousands of times) you can cache a copy of the invalid chars array for reuse.

Salic answered 15/9, 2008 at 17:12 Comment(0)
S
2

Also the destination file system is important.

Under NTFS, some files can not be created in specific directories. E.G. $Boot in root

Spiritless answered 23/8, 2010 at 20:19 Comment(1)
Surely that's not due to an NTFS naming rule, but merely because a file called $Boot already exists in the directory?Gromyko
H
1

many of these answers will not work if the filename is too long & running on a pre Windows 10 environment. Similarly, have a think about what you want to do with periods - allowing leading or trailing is technically valid, but can create problems if you do not want the file to be difficult to see or delete respectively.

This is a validation attribute I created to check for a valid filename.

public class ValidFileNameAttribute : ValidationAttribute
{
    public ValidFileNameAttribute()
    {
        RequireExtension = true;
        ErrorMessage = "{0} is an Invalid Filename";
        MaxLength = 255; //superseeded in modern windows environments
    }
    public override bool IsValid(object value)
    {
        //https://mcmap.net/q/65597/-in-c-check-that-filename-is-possibly-valid-not-that-it-exists-duplicate
        var fileName = (string)value;
        if (string.IsNullOrEmpty(fileName)) { return true;  }
        if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) > -1 ||
            (!AllowHidden && fileName[0] == '.') ||
            fileName[fileName.Length - 1]== '.' ||
            fileName.Length > MaxLength)
        {
            return false;
        }
        string extension = Path.GetExtension(fileName);
        return (!RequireExtension || extension != string.Empty)
            && (ExtensionList==null || ExtensionList.Contains(extension));
    }
    private const string _sepChar = ",";
    private IEnumerable<string> ExtensionList { get; set; }
    public bool AllowHidden { get; set; }
    public bool RequireExtension { get; set; }
    public int MaxLength { get; set; }
    public string AllowedExtensions {
        get { return string.Join(_sepChar, ExtensionList); } 
        set {
            if (string.IsNullOrEmpty(value))
            { ExtensionList = null; }
            else {
                ExtensionList = value.Split(new char[] { _sepChar[0] })
                    .Select(s => s[0] == '.' ? s : ('.' + s))
                    .ToList();
            }
    } }

    public override bool RequiresValidationContext => false;
}

and the tests

[TestMethod]
public void TestFilenameAttribute()
{
    var rxa = new ValidFileNameAttribute();
    Assert.IsFalse(rxa.IsValid("pptx."));
    Assert.IsFalse(rxa.IsValid("pp.tx."));
    Assert.IsFalse(rxa.IsValid("."));
    Assert.IsFalse(rxa.IsValid(".pp.tx"));
    Assert.IsFalse(rxa.IsValid(".pptx"));
    Assert.IsFalse(rxa.IsValid("pptx"));
    Assert.IsFalse(rxa.IsValid("a/abc.pptx"));
    Assert.IsFalse(rxa.IsValid("a\\abc.pptx"));
    Assert.IsFalse(rxa.IsValid("c:abc.pptx"));
    Assert.IsFalse(rxa.IsValid("c<abc.pptx"));
    Assert.IsTrue(rxa.IsValid("abc.pptx"));
    rxa = new ValidFileNameAttribute { AllowedExtensions = ".pptx" };
    Assert.IsFalse(rxa.IsValid("abc.docx"));
    Assert.IsTrue(rxa.IsValid("abc.pptx"));
}
Hawsepiece answered 14/3, 2017 at 4:44 Comment(0)
F
1

If you're only trying to check if a string holding your file name/path has any invalid characters, the fastest method I've found is to use Split() to break up the file name into an array of parts wherever there's an invalid character. If the result is only an array of 1, there are no invalid characters. :-)

var nameToTest = "Best file name \"ever\".txt";
bool isInvalidName = nameToTest.Split(System.IO.Path.GetInvalidFileNameChars()).Length > 1;

var pathToTest = "C:\\My Folder <secrets>\\";
bool isInvalidPath = pathToTest.Split(System.IO.Path.GetInvalidPathChars()).Length > 1;

I tried running this and other methods mentioned above on a file/path name 1,000,000 times in LinqPad.

Using Split() is only ~850ms.

Using Regex("[" + Regex.Escape(new string(System.IO.Path.GetInvalidPathChars())) + "]") is around 6 seconds.

The more complicated regular expressions fair MUCH worse, as do some of the other options, like using the various methods on the Path class to get file name and let their internal validation do the job (most likely due to the overhead of exception handling).

Granted it's not very often you need to validation 1 million file names, so a single iteration is fine for most of these methods anyway. But it's still pretty efficient and effective if you're only looking for invalid characters.

Figment answered 25/8, 2017 at 18:45 Comment(0)
U
1

I got this idea from someone. - don't know who. Let the OS do the heavy lifting.

public bool IsPathFileNameGood(string fname)
{
    bool rc = Constants.Fail;
    try
    {
        this._stream = new StreamWriter(fname, true);
        rc = Constants.Pass;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Problem opening file");
        rc = Constants.Fail;
    }
    return rc;
}
Utilitarianism answered 1/10, 2017 at 23:23 Comment(1)
This should be the accepted answer (with the possible exception of network paths).Infield
R
0

Windows filenames are pretty unrestrictive, so really it might not even be that much of an issue. The characters that are disallowed by Windows are:

\ / : * ? " < > |

You could easily write an expression to check if those characters are present. A better solution though would be to try and name the files as the user wants, and alert them when a filename doesn't stick.

Riocard answered 15/9, 2008 at 13:23 Comment(1)
Also characters <= 31 are forbidden.Brassica
C
0

I suggest just use the Path.GetFullPath()

string tagetFileFullNameToBeChecked;
try
{
  Path.GetFullPath(tagetFileFullNameToBeChecked)
}
catch(AugumentException ex)
{
  // invalid chars found
}
Commando answered 10/1, 2017 at 7:57 Comment(3)
Add some explanation with answer for how this answer help OP in fixing current issueTrowbridge
See the doc in the MSDN for the AugumentExcpetion, it reads:path is a zero-length string, contains only white space, or contains one or more of the invalid characters defined in GetInvalidPathChars. -or- The system could not retrieve the absolute path.Commando
In theory (according to the docs) this should work, problem is though at least in .NET Core 3.1, it does not.Sculpsit
T
0

My attempt:

using System.IO;

static class PathUtils
{
  public static string IsValidFullPath([NotNull] string fullPath)
  {
    if (string.IsNullOrWhiteSpace(fullPath))
      return "Path is null, empty or white space.";

    bool pathContainsInvalidChars = fullPath.IndexOfAny(Path.GetInvalidPathChars()) != -1;
    if (pathContainsInvalidChars)
      return "Path contains invalid characters.";

    string fileName = Path.GetFileName(fullPath);
    if (fileName == "")
      return "Path must contain a file name.";

    bool fileNameContainsInvalidChars = fileName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1;
    if (fileNameContainsInvalidChars)
      return "File name contains invalid characters.";

    if (!Path.IsPathRooted(fullPath))
      return "The path must be absolute.";

    return "";
  }
}

This is not perfect because Path.GetInvalidPathChars does not return the complete set of characters that are invalid in file and directory names and of course there's plenty more subtleties.

So I use this method as a complement:

public static bool TestIfFileCanBeCreated([NotNull] string fullPath)
{
  if (string.IsNullOrWhiteSpace(fullPath))
    throw new ArgumentException("Value cannot be null or whitespace.", "fullPath");

  string directoryName = Path.GetDirectoryName(fullPath);
  if (directoryName != null) Directory.CreateDirectory(directoryName);
  try
  {
    using (new FileStream(fullPath, FileMode.CreateNew)) { }
    File.Delete(fullPath);
    return true;
  }
  catch (IOException)
  {
    return false;
  }
}

It tries to create the file and return false if there is an exception. Of course, I need to create the file but I think it's the safest way to do that. Please also note that I am not deleting directories that have been created.

You can also use the first method to do basic validation, and then handle carefully the exceptions when the path is used.

Teresita answered 30/9, 2017 at 13:16 Comment(0)
G
0

This check

static bool IsValidFileName(string name)
{
    return
        !string.IsNullOrWhiteSpace(name) &&
        name.IndexOfAny(Path.GetInvalidFileNameChars()) < 0 &&
        !Path.GetFullPath(name).StartsWith(@"\\.\");
}

filters out names with invalid chars (<>:"/\|?* and ASCII 0-31), as well as reserved DOS devices (CON, NUL, COMx). It allows leading spaces and all-dot-names, consistent with Path.GetFullPath. (Creating file with leading spaces succeeds on my system).


Used .NET Framework 4.7.1, tested on Windows 7.

Gatto answered 15/3, 2018 at 13:41 Comment(0)
L
-1

One liner for verifying illigal chars in the string:

public static bool IsValidFilename(string testName) => !Regex.IsMatch(testName, "[" + Regex.Escape(new string(System.IO.Path.InvalidPathChars)) + "]");
Lakeishalakeland answered 2/12, 2018 at 1:45 Comment(0)
I
-1

In my opinion, the only proper answer to this question is to try to use the path and let the OS and filesystem validate it. Otherwise you are just reimplementing (and probably poorly) all the validation rules that the OS and filesystem already use and if those rules are changed in the future you will have to change your code to match them.

Infield answered 22/8, 2019 at 15:18 Comment(3)
a name might be valid and still not work as a file, see answers above.Sycophancy
I know exactly what you are talking about. But please check how Windows manages file names (some are actually devices). as I said, read the comments above that explain in details already why this approach can not be used. And I am not even starting to talk about security and auditing issues with this approach.Sycophancy
I use Windows API since 1997 so I am well aware that some file names can actually be devices. Same goes for /dev/null on linux, but that is still valid input and output filename depending on what the user wants / is allowed to do. You can "validate" the filename with the same level of confidence you can "validate" an email address using regex, but you won't know whether you can create the file / send the email until you actually try to do it. Therefore, such validation is entirely pointless, not to mention it is not portable and can't possibly cover all present and future filesystems.Infield

© 2022 - 2024 — McMap. All rights reserved.