It’s not a trivial exercise to validate a file path on a Windows PC.  There are a few special cases depending on the file system and operating subsystem:

NTFS and the Posix file systems are the most permissive.  A file name may contain up to 32,768 Unicode characters, trailing periods, trailing spaces, and two files may have names that differ only in case (e.g., README.TXT and readme.txt).

The Win32 subsystem enforces additional constraints on legal file names.  A file name may contain at most MAX_PATH characters (defined in windef.h as 260 characters), may not have trailing periods or spaces, and file names are case preserving, not case sensitive (i.e., if two files exist with names that differ only in case, you can only access one of them through Win32 APIs).

DOS and 16-bit Windows applications are still limited to “8.3” names.

Paths can be relative or absolute.  Paths may be also be expressed in UNC (Uniform Naming Convention, such as \ComputerNameSharedFolderResource).  (more)

However, sometimes a simple solution to a complicated problem will suffice.  Following is a function using regular expressions that will validate an absolute file path on a Windows PC in most cases:

using System;
using System.Text.RegularExpressions;

/// <summary>
/// Gets whether the specified path is a valid absolute file path.
/// </summary>
/// <param name="path">Any path. OK if null or empty.</param>
static public bool IsValidPath( string path )
    Regex r = new Regex( @"^(([a-zA-Z]:)|(\))(\{1}|((\{1})[^\]([^/:*?<>""|]*))+)$" );
    return r.IsMatch( path );
Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot