Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Path.GetInvalidFileNameChars Method

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

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

public static char[] GetInvalidFileNameChars ()
public static char[] GetInvalidFileNameChars ()
public static function GetInvalidFileNameChars () : char[]
Not applicable.

Return Value

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 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 Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

Show:
© 2016 Microsoft