Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System.IO Namespace
Path Class
Path Methods
 GetInvalidPathChars Method
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Path..::.GetInvalidPathChars Method

Updated: November 2007

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

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

Visual Basic (Declaration)
Public Shared Function GetInvalidPathChars As Char()
Visual Basic (Usage)
Dim returnValue As Char()

returnValue = Path.GetInvalidPathChars()
C#
public static char[] GetInvalidPathChars()
Visual C++
public:
static array<wchar_t>^ GetInvalidPathChars()
J#
public static char[] GetInvalidPathChars()
JScript
public static function GetInvalidPathChars() : char[]

Return Value

Type: array<System..::.Char>[]()[]

An array containing the characters that are not allowed in path 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 validate a path.

Visual Basic
Imports System
Imports System.IO



Module PathExample

    Sub Main()
        ' Get a list of invalid path characters.
        Dim invalidPathChars As Char() = Path.GetInvalidPathChars()

        Console.WriteLine("The following characters are invalid ina path:")

        ' Display each invalid character to the console.
        Dim invalidPChar As Char
        For Each invalidPChar In invalidPathChars
            Console.WriteLine(invalidPChar)
        Next invalidPChar

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

        Console.WriteLine("The following characters are invalid ina path:")

        ' Display each invalid character to the console.
        Dim invalidFChar As Char
        For Each invalidFChar In invalidFileChars
            Console.WriteLine(invalidFChar)
        Next invalidFChar

        Console.ReadLine()

    End Sub
End Module
' Note that while this code attempts to display a list of all invalid
' characters in paths and filenames, not all of the characters are
' within the displayable set of characters. Because the list of invalid
' characters can vary, based on the system, output for this code can vary.

C#
using System;
using System.IO;

namespace PathExample
{
    class GetCharExample
    {
        public static void Main()
        {
            // Get a list of invalid path characters.
            char[] invalidPathChars = Path.GetInvalidPathChars();

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

            // Display each invalid character to the console.
            foreach (char invalidPChar in invalidPathChars)
            {
                Console.WriteLine(invalidPChar);
            }

            // Get a list of invalid file characters.
            char[] invalidFileChars = Path.GetInvalidFileNameChars();

            Console.WriteLine("The following characters are invalid in a filename:");

            // Display each invalid character to the console.
            foreach (char invalidFChar in invalidFileChars)
            {
                Console.WriteLine(invalidFChar);
            }

            Console.ReadLine();
        }

    }
}
// Note that while this code attempts to display a list of all invalid
// characters in paths and filenames, not all of the characters are
// within the displayable set of characters. Because the list of invalid
// characters can vary, based on the system, output for this code can vary.


Visual C++
using namespace System;
using namespace System::IO;

int main()
{
    // Get a list of invalid path characters.
    array<Char>^ invalidPathChars = Path::GetInvalidPathChars();

    Console::WriteLine("The following characters are invalid in a path:");

    // Display each invalid character to the console.
    for each (Char invalidPathChar in invalidPathChars)
    {
        Console::WriteLine(invalidPathChar);
    }

    // Get a list of invalid file characters.
    array<Char>^ invalidFileChars = Path::GetInvalidFileNameChars();

    Console::WriteLine("The following characters are invalid in a file name:");

    // Display each invalid character to the console.
    for each (Char invalidFileChar in invalidFileChars)
    {
        Console::WriteLine(invalidFileChar);
    }

}

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker