Tips and tricks for .NET using ASP and VB code.

Determine if filename is valid without creating file.

        If IsValidFilename("Text.txt") Then
My.Computer.FileSystem.WriteAllText("Text.txt", "Text to write.", False)
Else
MsgBox("Invalid filename, please use another.")
End If

Public Function IsValidFilename(ByVal PathAndFilename As String) As Boolean
Dim FileData As FileInfo = Nothing
Dim IsValidName As Boolean = Nothing
Try
FileData = My.Computer.FileSystem.GetFileInfo(PathAndFilename)
IsValidName = True
Catch ex As Exception
IsValidName = False
End Try
If IsValidName Then
If (FileData.Name Like "[!A-z]*") Or (FileData.Name Like "*[;=+#%&]*") Then
IsValidName = False
End If
If FileData.Name.Contains("[") Or FileData.Name.Contains("]") Then
IsValidName = False
End If
End If
Return IsValidName
End Function

No comments:

Post a Comment

Search This Blog