2017年1月2日 星期一

C#判斷使用者輸入的本機路徑路徑是有效的

在stackoverflow看到的一片文章:Check if a string is a valid Windows directory (folder) path
可以使用Path.GetFullPath去判斷使用者輸入的本機路徑路徑是有效的
如果是無效的會丟出System.ArgumentException
只是空白字元算有效字元,如果怕使用者誤多輸入空白,可以自己判斷
if (inputPath.Contains(" ") )

Naming Files, Paths, and Namespaces
https://msdn.microsoft.com/en-us/library/aa365247
Use 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:
    • < (less than)
    • > (greater than)
    • : (colon)
    • " (double quote)
    • / (forward slash)
    • \ (backslash)
    • | (vertical bar or pipe)
    • ? (question mark)
    • * (asterisk)
  • Integer value zero, sometimes referred to as the ASCII NUL character.
  • Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.
  • Any other character that the target file system does not allow.

What characters are forbidden in Windows and Linux directory names?
http://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names

沒有留言:

張貼留言