Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool bOk = false;
- try
- {
- new System.IO.FileInfo(fileName);
- bOk = true;
- }
- catch (ArgumentException)
- {
- }
- catch (System.IO.PathTooLongException)
- {
- }
- catch (NotSupportedException)
- {
- }
- if (!bOk)
- {
- ...
- }
- else
- {
- ...
- }
- Directory.GetLogicalDrives() // Returns an array of strings like "c:"
- Path.GetInvalidFileNameChars() // Returns an array of characters that cannot be used in a file name
- Path.GetInvalidPathChars() // Returns an array of characters that cannot be used in a path.
- bool IsValidFilename(string testName)
- {
- string regexString = "[" + Regex.Escape(Path.GetInvalidPathChars()) + "]";
- Regex containsABadCharacter = new Regex(regexString);
- if (containsABadCharacter.IsMatch(testName)
- {
- return false;
- }
- // Check for drive
- string pathRoot = Path.GetPathRoot(testName);
- if (Directory.GetLogicalDrives().Contains(pathRoot))
- {
- // etc
- }
- // other checks for UNC, drive-path format, etc
- return true;
- }
- char[] invalidChars = Path.GetInvalidFileNameChars(); //returns invalid charachters
- Path.GetPathRoot(string); // will return the root.
- System.IO.DriveInfo.GetDrives()
- System.IO.Path.GetInvalidFileNameChars();
- System.IO.Path.GetInvalidPathChars();
Add Comment
Please, Sign In to add comment