c# 如何判断textBox.Text内容是否符合文件或者文件夹命名规则?
要取里面的内容来对新建文件夹命名,但是考虑到windows命名规则比较复杂,命名不符合规则的话,就无法新建文件夹。请问,有没有什么判断的方法,或者直接可以将文本格式化成规...
要取里面的内容来对新建文件夹命名,但是考虑到windows命名规则比较复杂,命名不符合规则的话,就无法新建文件夹。请问,有没有什么判断的方法,或者直接可以将文本格式化成规范名称的办法?
展开
2个回答
展开全部
使用 IO.Path.GetInvalidFileNameChars
给你个VB的代码,返回有效的文件名,自己转C#吧
Public Shared Function ReturnValidFileName(ByVal pstrFileName As String) As String
Dim chrInvalidFileNameChars() As Char
Dim sbFileName As New StringBuilder
chrInvalidFileNameChars = IO.Path.GetInvalidFileNameChars
'Add invalid character of bracket
Dim chrBracket() As Char = {"[", "]"}
For Each chrChar As Char In pstrFileName
If Array.IndexOf(chrInvalidFileNameChars, chrChar) > -1 OrElse Array.IndexOf(chrBracket, chrChar) > -1 Then
sbFileName.Append(" ")
Else
sbFileName.Append(chrChar)
End If
Next
Return sbFileName.ToString
End Function
给你个VB的代码,返回有效的文件名,自己转C#吧
Public Shared Function ReturnValidFileName(ByVal pstrFileName As String) As String
Dim chrInvalidFileNameChars() As Char
Dim sbFileName As New StringBuilder
chrInvalidFileNameChars = IO.Path.GetInvalidFileNameChars
'Add invalid character of bracket
Dim chrBracket() As Char = {"[", "]"}
For Each chrChar As Char In pstrFileName
If Array.IndexOf(chrInvalidFileNameChars, chrChar) > -1 OrElse Array.IndexOf(chrBracket, chrChar) > -1 Then
sbFileName.Append(" ")
Else
sbFileName.Append(chrChar)
End If
Next
Return sbFileName.ToString
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询