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 ); }
>will validate an absolute file path on a Windows PC in most cases
An obvious execption is filenames in non-English
(ie other characters than a-z)
Still, thanks 🙂
To get a valid FILE NAME after removing the Invalid file name characters, you can use the following function.
public static string RemoveInvalidFileNameChars(string fileName)
{
char[] invalidFileChars = Path.GetInvalidFileNameChars();
foreach (char invalidFChar in invalidFileChars)
{
fileName = fileName.Replace(invalidFChar.ToString(),””);
}
return fileName;
}
Try:
private static void CheckValidPath(string pathFile)
{
string fullPath = pathFile;
FileIOPermission permission = new FileIOPermission(PermissionState.None);
permission.AllFiles = FileIOPermissionAccess.PathDiscovery;
permission.Assert();
try { fullPath = Path.GetFullPath(pathFile); }
finally { CodeAccessPermission.RevertAssert(); }
FileIOPermission checkFile = new FileIOPermission(FileIOPermissionAccess.AllAccess, fullPath);
checkFile.Demand();
}
Regards,
Romy
hi Robin,
the code you have provided for RemoveInvalidFileNameChars()
is really very useful to me.
thanks a lot…
Srikanth.g
Remember, that there are special files under windows which cannot be created (or have undesired effects when attempting to) and whose characters are not in Path.InvalidFileNameChars
For example files named “COM1”, “PRN” or just “NUL”.
Look here: http://social.technet.microsoft.com/Forums/en/w7itprogeneral/thread/e22c021d-d188-4ff2-a4dd-b5d58d979c58
hi, your solution won’t cover the following malformed win local or unc-paths:
Path \SWL25xxxc$\
Path c:test
Path c:
the corrected one:
@”^((([a-zA-Z]:)|(\))(((\{1})[^\ ]([^/:*?””|\]*))+)[\])$”
the forum wasted the undoubbled backslashes:
next try:
Path \\\\SWL25xxx\\c$\\
Path c:\\test
Path c:\\ \\
aha
..take the doubbled ones as only one please *rolleyes*