检查路径是否有效 [英] Check whether a path is valid

查看:312
本文介绍了检查路径是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道:我正在寻找一种方法来验证给出的路径是有效的。 。(注:我不想来检查文件是否存在我只是想证明路径的有效性 - 因此,如果一个文件能可能存在的位置)

I am just wondering: I am looking for a way to validate if a given path is valid. (Note: I do not want to check if a file is existing! I only want to proof the validity of the path - So if a file could possibly exists at the location).

但问题是,我无法找到在 .NET API 东西。 由于多种格式和位置,Windows支持,我宁愿用的东西 MS-本地

Problem is, I can't find anything in the .Net API. Due to the many formats and locations that Windows supports, I'd rather use something MS-native.

由于函数应该能够核对:

Since the function should be able to check against:

      
  • 在相对Pathes(./)
  •   
  • 在绝对Pathes(C:\ TMP)
  •   
  • 在UNC-Pathes(\一些-PC \ C $)
  •   
  • NTFS-限制类的完整路径1024个字符 - 如果我没有记错超过路径使文件无法进入5月   内部Windows功能。用资源管理器中重命名它仍然有效
  •   
  • 在卷GUID Pathes:\ \卷{GUID} \ somefile.foo
  •   
  • Relative Pathes (./)
  • Absolute Pathes (c:\tmp)
  • UNC-Pathes (\some-pc\c$)
  • NTFS-Limitations like the full path 1024 chars - If I am not mistaken exceeding the path will make a file inaccessible for may internal Windows functions. Renaming it with Explorer still works
  • Volume GUID Pathes : "\?\Volume{GUID}\somefile.foo

任何人有这样的功能?

推荐答案

尝试<一href="http://msdn.microsoft.com/en-us/library/system.uri.iswellformedoriginalstring.aspx"><$c$c>Uri.IsWellFormedUriString():

  • 该字符串不正确转义。

  • The string is not correctly escaped.

http://www.example.com/path???/file name

  • 该字符串是绝对URI的重新presents一个隐含的文件URI。

  • The string is an absolute Uri that represents an implicit file Uri.

    c:\\directory\filename
    

  • 该字符串是缺少一个斜杠的路径前的绝对URI。

  • The string is an absolute URI that is missing a slash before the path.

    file://c:/directory/filename
    

  • 该字符串包含转义反斜杠,即使它们被视为正斜杠。

  • The string contains unescaped backslashes even if they are treated as forward slashes.

    http:\\host/path/file
    

  • 再$ P $的字符串psents分层绝对URI,不包含://。

  • The string represents a hierarchical absolute Uri and does not contain "://".

    www.example.com/path/file
    

  • 解析器的Uri.Scheme表示未充分形成的原始字符串。

  • The parser for the Uri.Scheme indicates that the original string was not well-formed.

    The example depends on the scheme of the URI.
    

  • 这篇关于检查路径是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆