這個範例會傳 Boolean 回值,指出字串是否代表檔名或路徑。 驗證會檢查名稱是否包含檔案系統不允許的字元。
範例
Function IsValidFileNameOrPath(ByVal name As String) As Boolean
' Determines if the name is Nothing.
If name Is Nothing Then
Return False
End If
' Determines if there are bad characters in the name.
For Each badChar As Char In System.IO.Path.GetInvalidPathChars
If InStr(name, badChar) > 0 Then
Return False
End If
Next
' The name passes basic validation.
Return True
End Function
這個範例不會檢查名稱是否未正確放置冒號或沒有名稱的目錄,或名稱長度是否超過系統定義的最大長度。 它也不會檢查應用程式是否有權存取具有指定名稱的檔案系統資源。