What is the correct way to check if a path is an UNC path or a local path?

后端 未结 6 792
说谎
说谎 2020-12-15 04:09

The easiest way to check if a path is an UNC path is of course to check if the first character in the full path is a letter or backslash. Is this a good solution or could th

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-15 04:54

    Maybe this answer can be helpful to someone who wants to validate only UNC server + share + subdirectories, for example path to network repository like

    • \\Server1\Share1
    • \\Server2\Share22\Dir1\Dir2
    • \\Server3

    Use the following regex:

    ^\\\\([A-Za-z0-9_\-]{1,32}[/\\]){0,10}[A-Za-z0-9_\-]{1,32}$
    
    • replace 32 (2 times) with maximum allowed length of server/directory name
    • replace 10 with maximum allowed path depth (maximum count of directories)
    • extend [A-Za-z0-9_\-] (2 times) if you are missing some character allowed in server/directory name

    I've successfully tested it. Enjoy!

提交回复
热议问题