Export (0) Print
Expand All

Path.GetInvalidPathChars Method

Note: This method is new in the .NET Framework version 2.0.

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 ()
public static char[] GetInvalidPathChars ()
public static function GetInvalidPathChars () : char[]

Return Value

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:");

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


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

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

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft