Share via


HOW TO:在 Visual Basic 中驗證檔案名稱和路徑

更新:2007 年 11 月

這個範例會傳回 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

這個範例不會檢查名稱的冒號位置是否正確、目錄是否沒有名稱,或名稱長度是否超過系統所定義的最大長度。它也不會檢查應用程式是否具有使用權限可以存取具有指定之名稱的檔案系統資源。

請參閱

參考

GetInvalidPathChars

其他資源

在 Visual Basic 中驗證字串