Path.GetInvalidPathChars Method

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

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

public static char[] GetInvalidPathChars()

Return Value

Type: 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.

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:");
            ShowChars(invalidPathChars);
            Console.WriteLine();

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

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

        public static void ShowChars(char[] charArray)
        {
            Console.WriteLine("Char\tHex Value");
            // Display each invalid character to the console. 
            foreach (char someChar in charArray)
            {
                if (Char.IsWhiteSpace(someChar))
                {
                    Console.WriteLine(",\t{0:X4}", (int)someChar);
                }
                else
                {
                    Console.WriteLine("{0:c},\t{1:X4}", someChar, (int)someChar);
                }
            }
        }
    }
}
// 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 
// ...

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, 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, Zune

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: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft