我想知道如何确定字符串是否是有效的文件路径。
文件路径可能或不可以 存在。 / p>
解决方案对路径的字符串格式进行100%准确的检查是非常困难的,因为它取决于使用它的文件系统(和网络协议,如果它不在同一台计算机上)。
甚至在Windows或甚至NTFS中它不是简单的,因为它仍然依赖于API .NET使用背景与内核进行通信。
由于目前大多数文件系统都支持unicode,所以还可能需要检查正确编码的unicode,normalization等的所有规则。
我要做的只是做一些基本的检查,然后在使用路径之后正确处理异常。有关可能的规则,请参阅:
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#确定字符串是否是有效的文件路径
发布评论