Export (0) Print
Expand All

Path.GetInvalidFileNameChars Method

Gets an array containing the characters that are not allowed in file names.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function GetInvalidFileNameChars As Char()

Return Value

Type: System.Char()
An array containing the characters that are not allowed in file names.

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).

The following code example demonstrates the GetInvalidFileNameChars method and the GetInvalidPathChars method to retrieve invalid characters.

Imports System
Imports System.IO

Namespace PathExample
    Public Class GetCharExample
        Public Shared Sub Main()
            ' Get a list of invalid path characters. 
            Dim invalidPathChars() As Char = Path.GetInvalidPathChars()

            Console.WriteLine("The following characters are invalid in a path:")
            ShowChars(invalidPathChars)
            Console.WriteLine()

            ' Get a list of invalid file characters. 
            Dim invalidFileChars() As Char = Path.GetInvalidFileNameChars()

            Console.WriteLine("The following characters are invalid in a filename:")
            ShowChars(invalidFileChars)
        End Sub 

        Public Shared Sub ShowChars(charArray As Char())
            Console.WriteLine("Char" + vbTab + "Hex Value")
            ' Display each invalid character to the console. 
            For Each someChar As Char In charArray
                If Char.IsWhiteSpace(someChar)
                    Console.WriteLine("," + vbTab + "{0:X4}", _
                        Microsoft.VisualBasic.Asc(someChar))
                Else
                    Console.WriteLine("{0:c}," + vbTab +"{1:X4}", someChar, _
                        Microsoft.VisualBasic.Asc(someChar))
                End If 
            Next someChar
        End Sub 
    End Class 
End Namespace 
' Note: Some characters may not be displayable on the console. 
' The output will look something like: 

' The following characters are invalid in a path: 
' Char    Hex Value 
' ",      0022 
' <,      003C 
' >,      003E 
' |,      007C 
' ... 

' The following characters are invalid in a filename: 
' Char    Hex Value 
' ",      0022 
' <,      003C 
' >,      003E 
' |,      007C 
' ...

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft