gpt4 book ai didi

c# - 检查路径是否有效

转载 作者:IT王子 更新时间:2023-10-29 03:36:14 24 4
gpt4 key购买 nike

我只是想知道:我正在寻找一种方法来验证给定路径是否有效。(注意:我不想检查文件是否存在!我只想证明路径的有效性 - 所以如果文件可能存在于该位置)

问题是,我在 .Net API 中找不到任何内容。由于 Windows 支持多种格式和位置,我宁愿使用 MS 原生的东西。

因为函数应该能够检查:

  • Relative Paths (./)
  • Absolute Paths (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 many internal Windows functions. Renaming it with Explorer still works
  • Volume GUID Paths : "\?\Volume{GUID}\somefile.foo

有没有人有这样的功能?

最佳答案

尝试 Uri.IsWellFormedUriString() :

  • 字符串未正确转义。

      http://www.example.com/path???/file name
  • 该字符串是表示隐式文件 Uri 的绝对 Uri。

      c:\\directory\filename
  • 该字符串是路径前缺少斜杠的绝对 URI。

      file://c:/directory/filename
  • 字符串包含未转义的反斜杠,即使它们被视为正斜杠也是如此。

      http:\\host/path/file
  • 该字符串表示一个层次化的绝对Uri,不包含“://”。

      www.example.com/path/file
  • Uri.Scheme 的解析器指示原始字符串的格式不正确。

      The example depends on the scheme of the URI.

关于c# - 检查路径是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6198392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com