在任何地方都有Filename.IsValid函数吗? [英] Is there a Filename.IsValid function anywhere?

查看:60
本文介绍了在任何地方都有Filename.IsValid函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在框架中找不到任何能告诉我
文件名是否有效的内容。我想我可以尝试打开它并陷阱

错误,但这似乎很浪费。我挖了一遍,发现了这段代码:

公共函数IsValidName(ByVal name As String)As Boolean

Dim i As Integer

For i = 0 to name.Length - 1

Dim ch As Char = name.Chars(i)

Dim uc As Globalization.UnicodeCategory =

[ Char] .GetUnicodeCategory(ch)

Select Case uc

Case Globalization.UnicodeCategory.UppercaseLetter,

Globalization.UnicodeCategory.LowercaseLetter,

Globalization.UnicodeCategory.TitlecaseLetter,

Globalization.UnicodeCategory.DecimalDigitNumber

Case Else

返回False

结束选择

下一个我

返回True

结束功能


但是这个失败了在一个有空格的名字上,这是有效的。


有什么建议吗?

解决方案

..NET Framework类库

Path.GetInvalidFileNameChars方法

注意:这个方法是新的我.NET Framework 2.0版。
http://msdn2.microsoft.com/en-us/lib...rs(VS.80).aspx

dgk <所以********* @ zero-spam-hotmail.com>在留言中写道

news:fj ******************************** @ 4ax.com ...

我在框架中找不到任何能告诉我
文件名是否有效的内容。我想我可以尝试打开它并陷入错误,但这似乎很浪费。我挖了一遍,找到了这段代码:

公共函数IsValidName(ByVal name As String)As Boolean
Dim i As Integer
For i = 0 to name.Length - 1
Dim ch As Char = name.Chars(i)
Dim uc As Globalization.UnicodeCategory =
[Char] .GetUnicodeCategory(ch)
Select Case uc
案例全球化.UnicodeCategory.UppercaseLetter,
Globalization.UnicodeCategory.LowercaseLetter,
Globalization.UnicodeCategory.TitlecaseLetter,
Globalization.UnicodeCategory.DecimalDigitNumber
Case Else
返回错误
结束选择
接下来我
返回真实
结束功能

但是这个名字中有一个空格失败了,这是有效的。

有什么建议吗?



On Sun,2006年4月16日16:05:34 -0700,Jim Hughes

< NO ********* @ Hotmail.com>写道:

.NET Framework类库
Path.GetInvalidFileNameChars方法
注意:此方法是.NET Framework 2.0版中的新增功能。

http://msdn2.microsoft.com/en-us/lib...rs(VS.80).aspx




谢谢,这让事情变得更容易。但它返回的只是<> |那些是

唯一无效的文件字符? /和\应该是非法的。什么

约?和*


#%〜`


好​​吧,我猜他们是合法的。


" DGK" <否***** @ MailsAnonymous.com>在消息中写道

新闻:例如******************************** @ 4ax.com ...

On Sun,2006年4月16日16:05:34 -0700,Jim Hughes
< NO ********* @ Hotmail.com> ;写道:

.NET Framework类库
Path.GetInvalidFileNameChars方法
注意:此方法是.NET Framework 2.0版中的新增功能。

http://msdn2.microsoft.com/en-us/lib...rs(VS.80).aspx

谢谢,这让事情变得更轻松。但它返回的只是<> |那些是唯一无效的文件字符? /和\应该是非法的。什么
关于?和*

#%〜`

嗯,我猜他们是合法的。



如果你所做的只是一个System.IO.Path.GetInvalidFileNameChars()。ToString(),我

可以看出你怎么想。


然而,有41个单独的我系统上该列表中的字符。


"

<



|

(我回复的其他不可打印的字符):



*



\\ \\ b
/

静态无效测试()

{

char [] ch =系统.IO.Path.GetInvalidFileNameChars();

System.Diagnostics.Trace.WriteLine(ch.Length);

for(int i = 0; i< ch。长度; i ++)

{

//为每个字符转储十六进制值

//System.Diagnostics.Trace.WriteLine(Convert.ToByte (ch [i])。ToString(" X"));

System.Diagnostics.Trace.WriteLine(ch [i]);

}

}


来自t他发来的链接:


备注


从这种方法返回的数组不保证包含

文件和目录名称中无效的完整字符集。

完整的无效字符集可能因文件系统而异。例如,在基于Windows的桌面平台上,无效的路径字符可能包括

ASCII / Unicode字符1到31,以及引用(),小于(<),

大于(>),管道(|),退格(\b),null(\0)和制表符(\ t)。



I can''t find anything in the framework that will tell me whether a
filename is valid. I suppose that I can just try to open it and trap
an error but that seems wasteful. I dug around and found this code:
Public Function IsValidName(ByVal name As String) As Boolean
Dim i As Integer
For i = 0 To name.Length - 1
Dim ch As Char = name.Chars(i)
Dim uc As Globalization.UnicodeCategory =
[Char].GetUnicodeCategory(ch)
Select Case uc
Case Globalization.UnicodeCategory.UppercaseLetter,
Globalization.UnicodeCategory.LowercaseLetter,
Globalization.UnicodeCategory.TitlecaseLetter,
Globalization.UnicodeCategory.DecimalDigitNumber
Case Else
Return False
End Select
Next i
Return True
End Function

but this one failed on a name with a space in it, and that is valid.

Any suggestions?

解决方案

..NET Framework Class Library
Path.GetInvalidFileNameChars Method
Note: This method is new in the .NET Framework version 2.0.

http://msdn2.microsoft.com/en-us/lib...rs(VS.80).aspx
"dgk" <so*********@zero-spam-hotmail.com> wrote in message
news:fj********************************@4ax.com...

I can''t find anything in the framework that will tell me whether a
filename is valid. I suppose that I can just try to open it and trap
an error but that seems wasteful. I dug around and found this code:
Public Function IsValidName(ByVal name As String) As Boolean
Dim i As Integer
For i = 0 To name.Length - 1
Dim ch As Char = name.Chars(i)
Dim uc As Globalization.UnicodeCategory =
[Char].GetUnicodeCategory(ch)
Select Case uc
Case Globalization.UnicodeCategory.UppercaseLetter,
Globalization.UnicodeCategory.LowercaseLetter,
Globalization.UnicodeCategory.TitlecaseLetter,
Globalization.UnicodeCategory.DecimalDigitNumber
Case Else
Return False
End Select
Next i
Return True
End Function

but this one failed on a name with a space in it, and that is valid.

Any suggestions?



On Sun, 16 Apr 2006 16:05:34 -0700, "Jim Hughes"
<NO*********@Hotmail.com> wrote:

.NET Framework Class Library
Path.GetInvalidFileNameChars Method
Note: This method is new in the .NET Framework version 2.0.

http://msdn2.microsoft.com/en-us/lib...rs(VS.80).aspx



Thanks, that makes things easier. But all it returns is "<>| those are
the only invalid file characters? / and \ should be illegal. What
about ? and *

#%~`

Well, I guess that they''re legal.


"dgk" <No*****@MailsAnonymous.com> wrote in message
news:eg********************************@4ax.com...

On Sun, 16 Apr 2006 16:05:34 -0700, "Jim Hughes"
<NO*********@Hotmail.com> wrote:

.NET Framework Class Library
Path.GetInvalidFileNameChars Method
Note: This method is new in the .NET Framework version 2.0.

http://msdn2.microsoft.com/en-us/lib...rs(VS.80).aspx
Thanks, that makes things easier. But all it returns is "<>| those are
the only invalid file characters? / and \ should be illegal. What
about ? and *

#%~`

Well, I guess that they''re legal.


If all you did is a System.IO.Path.GetInvalidFileNameChars().ToString( ), I
can see how you might think that.

However, there are 41 separate characters in that list on my system.

"
<


|
(additional unprintable characters stripped by me from reply):
:
*
?
\
/

static void test()
{
char[] ch = System.IO.Path.GetInvalidFileNameChars();
System.Diagnostics.Trace.WriteLine(ch.Length);
for (int i = 0; i < ch.Length; i++)
{
// dump hex values for each char
//System.Diagnostics.Trace.WriteLine(Convert.ToByte( ch[i]).ToString("X"));
System.Diagnostics.Trace.WriteLine(ch[i]);
}
}

From the link I sent:

Remarks

The array returned from this method is not guaranteed to contain the
complete set of characters that are invalid in file and directory names. The
full set of invalid characters can vary by file system. For example, on
Windows-based desktop platforms, invalid path characters might include
ASCII/Unicode characters 1 through 31, as well as quote ("), less than (<),
greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).



这篇关于在任何地方都有Filename.IsValid函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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