如何检查字符串以查看它是否是有效的文件名 [英] How to check a string to see that it is a valid file name

查看:151
本文介绍了如何检查字符串以查看它是否是有效的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个字符串,看它是否是一个有效的文件名。


是否有类似的模式或RegEx可以做到这一点。


1)只是带有扩展名的文件名


2)完整路径

以上任何一种的帮助我将不胜感激。


I''d like to check a string to see that it is a valid file name.

Is there a Like pattern or RegEx that can do that.

1) Just the file name with maybe an extension

2)A full path
An help for either of the above would be appreciated.



推荐答案

11月21日下午2:01,学术界 < academiaNOS ... @ a-znet.comwrote:
On Nov 21, 2:01 pm, "Academia" <academiaNOS...@a-znet.comwrote:

我想检查一个字符串,看它是否是一个有效的文件名。 />

是否有类似模式或RegEx可以做到这一点。


1)只是文件名可能有扩展名


2)完整路径


对上述任何一种方式的帮助都将不胜感激。
I''d like to check a string to see that it is a valid file name.

Is there a Like pattern or RegEx that can do that.

1) Just the file name with maybe an extension

2)A full path

An help for either of the above would be appreciated.



如果你给FileInfo类提供文件的完整路径,它可以确定文件是否存在。


dim fi as new System.IO.FileInfo(" [Path]")

if fi.Exists then

''do work

如果

If you give the FileInfo class the full path to the file it can
determine if the file exists.

dim fi as new System.IO.FileInfo("[Path]")
if fi.Exists then
''do work
end if


11月21日,下午1点01分,学术界 < academiaNOS ... @ a-znet.comwrote:
On Nov 21, 1:01 pm, "Academia" <academiaNOS...@a-znet.comwrote:

我想检查一个字符串,看它是否是一个有效的文件名。 />

是否有类似模式或RegEx可以做到这一点。


1)只是文件名可能有扩展名


2)完整路径


对上述任何一种方式的帮助都将不胜感激。
I''d like to check a string to see that it is a valid file name.

Is there a Like pattern or RegEx that can do that.

1) Just the file name with maybe an extension

2)A full path

An help for either of the above would be appreciated.



嗯......这取决于你想要完成什么?如果你的
检查是否存在,那么总会有System.IO.File.Exists。

如果您只是检查以确保您有可能的有效
文件名,然后你可以使用System.IO.Path类

GetInvalidFileNameChars和GetInvalidPathChars来获取一个不允许的字符数组的
字符文件名和路径,然后你可以检查以确保你的文件名不包含任何这些

字符,使用String.IndexOfAny。如果它返回任何其他

然后返回负1,则表示文件名错误:


dim bfc()as char = path.getinvalidfilenamechars()


如果filename.indexofany(bfc)-1则

''非法文件名

结束如果


-

Tom Shelton

Well... That depends on what your trying to accomplish? If your
checking for existance, then there is always System.IO.File.Exists.
If your just checking to make sure that you have a potentially valid
file name, then you can use the System.IO.Path classes
GetInvalidFileNameChars and GetInvalidPathChars to get an array of
characters that are not allowed in a file name and path, then you
could check to make sure your file name doesn''t contain any of those
characters, using the String.IndexOfAny. If it returns anything other
then negative 1, then you have a bad filename:

dim bfc() as char = path.getinvalidfilenamechars()

if filename.indexofany(bfc) -1 then
'' illegal file name
end if

--
Tom Shelton


谢谢,但我知道文件不存在(还)。


感谢您的回答

" cfps.Christian" < ge ******* @ otc.eduwrote in message

news:e5 *********************** *********** @ w28g2000 hsf.googlegroups.com ...
Thanks but I know the file does not exist (yet).

Thanks for answering
"cfps.Christian" <ge*******@otc.eduwrote in message
news:e5**********************************@w28g2000 hsf.googlegroups.com...

11月21日下午2:01,Academia < academiaNOS ... @ a-znet.comwrote:
On Nov 21, 2:01 pm, "Academia" <academiaNOS...@a-znet.comwrote:

>我想检查一个字符串,看它是否是一个有效的文件名。

是否有类似的模式或RegEx可以做到这一点。

1)只是文件名可能有一个扩展名

2)一个完整的路径

以上任何一方的帮助将不胜感激。
>I''d like to check a string to see that it is a valid file name.

Is there a Like pattern or RegEx that can do that.

1) Just the file name with maybe an extension

2)A full path

An help for either of the above would be appreciated.



如果你给FileInfo类提供文件的完整路径,它可以确定文件是否存在。


dim fi as new System.IO.FileInfo(" [Path]")

if fi.Exists then

''do work

如果
结束


If you give the FileInfo class the full path to the file it can
determine if the file exists.

dim fi as new System.IO.FileInfo("[Path]")
if fi.Exists then
''do work
end if



这篇关于如何检查字符串以查看它是否是有效的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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