搞清楚字符串是否是有效的文件路径? [英] Figuring out whether string is valid file path?

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

问题描述

文件路径可能会或可能不存在,但我需要知道,如果它在正确的格式,它是一个有效的文件路径?

the file path may or may not exist, but I need to know if it's in the right format for it to be a valid file path?

任何人都知道一个快速的方法来做到这一点?

Anyone know a quick way to do this?

推荐答案

之路的字符串格式的100%准确的检查是相当困难的,因为它依赖于文件系统上使用它(和网络协议,如果它不是在同一台计算机上)。

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).

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

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.

此外,由于大多数文件系统都支持单向code,人们可能还需要检查所有的规则,正确地将连接codeD UNI code,规范化,等等等等。

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

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

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