This topic has not yet been rated - Rate this topic

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 retrieve invalid characters.


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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Keep to the topic
Please don't add irrelevant comments.

This method is mainly used to help construct other methods that can be used to validate paths, this method is not a be all end all method and contains this list of characters for very good reasons - of which I will not cover to give you something better to do other than make a fool of yourself.


Colon is a valid character for a path
The colon character can appear in a path along with \ / and ? so they're not included in the invalid characters list. Use Path.GetInvalidFileNameChars if you want to validate individual folder names or file names. This is the invaild path characters list with : / \ ? and * added to it.

On one or multiple occurences of a colon ':' in a path.
For your information.

Is ':' vallid or not? One occurence (at the second position) is valid as in "a:\"
Multiple occurences are not as in "a:\bcd-not a valid colon here : and more here ::::".

The ArgumentException is not triggered in the second case for all of the calls that claim to check on invalid characters in a path.

Kind regards,

André Steenveld.
What is point?
If this method is not guaranteed to return all of the invalid characters, then what is the point of getting just some of them?