通过C#确定字符串是否是有效的文件路径

编程入门 行业动态 更新时间:2024-10-25 00:34:00
本文介绍了通过C#确定字符串是否是有效的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何确定字符串是否是有效的文件路径。

文件路径可能或不可以 存在。 / p>

解决方案

对路径的字符串格式进行100%准确的检查是非常困难的,因为它取决于使用它的文件系统(和网络协议,如果它不在同一台计算机上)。

甚至在Windows或甚至NTFS中它不是简单的,因为它仍然依赖于API .NET使用背景与内核进行通信。

由于目前大多数文件系统都支持unicode,所以还可能需要检查正确编码的unicode,normalization等的所有规则。

我要做的只是做一些基本的检查,然后在使用路径之后正确处理异常。有关可能的规则,请参阅:

  • Wikipedia - 文件名,了解不同文件系统使用的规则概述。 命名文件,路径和命名空间获得窗口的特定规则

I would like to know how to determine whether string is valid file path.

The file path may or may not exist.

解决方案

A 100% accurate checking of a path's string format is quite difficult, since it will depend on the filesystem on which it is used (and network protocols if its not on the same computer).

Even within windows or even NTFS its not simple since it still depends on the API .NET is using in the background to communicate with the kernel.

And since most filesystems today support unicode, one might also need to check for all the rules for correcly encoded unicode, normalization, etc etc.

What I'd do is to make some basic checks only, and then handle exceptions properly once the path is used. For possible rules see:

  • Wikipedia - Filename for an overview of the rules used by different file systems
  • Naming Files, Paths, and Namespaces for windows specific rules

更多推荐

通过C#确定字符串是否是有效的文件路径

本文发布于:2023-06-07 15:15:03,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   路径   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!