本文介绍了在任何地方都有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

结束功能


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


有什么建议吗?

解决方案






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

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

约?和*


#%〜`


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




如果你所做的只是一个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?

解决方案





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函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:15