通过C#确定字符串是否是有效的文件路径 [英] Determine via C# whether a string is a valid file path

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

问题描述

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



文件路径可能或不可以 存在。 / 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:

这篇关于通过C#确定字符串是否是有效的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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