C# Path GetInvalidFileNameChars() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Path.GetInvalidFileNameChars() returns a char[] of the characters that are not allowed in a file name on the current operating system. Use it to check or clean a name that comes from a user.

The list depends on the system. On Windows it holds about 41 characters: the control characters, plus " < > | : * ? \ /. On Linux and macOS it has only two: the null character '\0' and the slash /. So a name that is fine on Linux may be invalid on Windows.

This is a static method of System.IO.Path. Call it as Path.GetInvalidFileNameChars(...); there is no new Path(). Like most Path methods, it only works with text: it never opens, reads, or creates a file, and it works the same whether or not the path exists.

Syntax

The method takes no arguments. Test a name with IndexOfAny.

C#
char[] bad = Path.GetInvalidFileNameChars();

bool hasSlash = Array.IndexOf(bad, '/') >= 0;                    // true on every system
bool isValid = "notes.txt".IndexOfAny(bad) < 0;                   // true
CallWhat it does
Path.GetInvalidFileNameChars()A new char[] of the invalid characters.
name.IndexOfAny(bad) < 0true when the name has no invalid character.
Path.GetInvalidPathChars()The shorter list for whole paths. It leaves out the separators.

'/' and '\0' are on the list on every system. Anything else (such as ? or :) is invalid only on Windows. If your files may move between systems, clean for the strictest list.

Library declarations
C#
public static char[] GetInvalidFileNameChars();

Parameters

ParameterDescription
NoneGetInvalidFileNameChars() takes no parameters.

Return Value

Return ValueDescription
char[]A new array of the characters that cannot be used in a file name here. The array is a copy: changing it does not change the list.

C# Examples

Example 1 — Check Facts That Hold on Every System

The size of the array differs by system, so test for characters that are always in the list or always out of it.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        char[] bad = Path.GetInvalidFileNameChars();

        Console.WriteLine("Has entries: " + (bad.Length > 0));
        Console.WriteLine("Slash is invalid: " + (Array.IndexOf(bad, '/') >= 0));
        Console.WriteLine("Null char is invalid: " + (Array.IndexOf(bad, '\0') >= 0));
        Console.WriteLine("Letter a is invalid: " + (Array.IndexOf(bad, 'a') >= 0));
        Console.WriteLine("Digit 5 is invalid: " + (Array.IndexOf(bad, '5') >= 0));
    }
}

How It Works

  1. A real list. The array is never empty. On Linux it has two entries and on Windows about 41, so the program checks only that the length is above zero, not the exact size.
  2. The slash. A slash separates folders on every system, so it cannot be part of a file name. A position of zero or more from Array.IndexOf means "found".
  3. The null character. '\0' marks the end of a string in the file system. It is invalid everywhere, so the line is True.
  4. Normal characters are fine. Letters and digits are never on the list, so both of the last lines are False. The checks avoid ? and :, which differ by system.

Example 2 — Replace Invalid Characters With an Underscore

Clean a title that contains a slash and a null character, with two different methods that give the same result.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string name = "a/b\0c.txt";

        string clean = name;
        foreach (char c in Path.GetInvalidFileNameChars()) {
            clean = clean.Replace(c, '_');
        }
        Console.WriteLine(clean);

        string viaSplit = string.Join("_", name.Split(Path.GetInvalidFileNameChars()));
        Console.WriteLine(viaSplit);

        Console.WriteLine(clean == viaSplit);
        Console.WriteLine(clean.IndexOfAny(Path.GetInvalidFileNameChars()) < 0);
    }
}

How It Works

  1. A bad input. The name has a slash and a null character, which are invalid on every system. The code never prints the original, because a null character cannot be shown.
  2. Replace in a loop. For each invalid character, Replace(c, '_') swaps it for an underscore. Characters that are not in the text change nothing, so the loop is safe on any system.
  3. Split and Join. Split cuts the name at every invalid character, and string.Join("_", ...) glues the pieces with underscores. The result is the same a_b_c.txt.
  4. Verify the result. Both texts are equal, and IndexOfAny finds no invalid character left in the cleaned name, so the two last lines are True.

Example 3 — Validate a File Name: Windows and Linux Differ

A helper that checks a name. The slash and the null character always fail. A question mark and a colon fail on Windows only, so the output shows both systems.

C#
using System;
using System.IO;

class Program {
    static bool IsValid(string name) {
        return name.Length > 0 && name.IndexOfAny(Path.GetInvalidFileNameChars()) < 0;
    }

    static void Main() {
        Console.WriteLine("notes.txt valid: " + IsValid("notes.txt"));
        Console.WriteLine("a/b.txt valid: " + IsValid("a/b.txt"));
        Console.WriteLine("report?.txt valid: " + IsValid("report?.txt"));
        Console.WriteLine("a:b.txt valid: " + IsValid("a:b.txt"));
        Console.WriteLine("More than 2 invalid chars: " + (Path.GetInvalidFileNameChars().Length > 2));
    }
}

How It Works

  1. Two blocks listed. A real run prints only the block for your system.
  2. Always the same. notes.txt is valid and a/b.txt is not, on every system, because a slash can never be inside a file name.
  3. Windows is stricter. The question mark and the colon are on the Windows list, so report?.txt and a:b.txt are invalid there. On Linux and macOS they are ordinary characters, so those names are valid.
  4. The size tells the story. Windows has far more than two invalid characters and Linux has exactly two. Clean names with the Windows rules if files move between systems.

Frequently Asked Questions

An array with every character that cannot be used in a file name on the current system. On Linux and macOS, that is only the slash and the null character. On Windows, it is about 41 characters.
Because their file systems have different rules. Windows reserves characters such as ? : * < > | and the backslash. Linux only forbids the slash, which separates folders, and the null character, which ends strings.
Loop over Path.GetInvalidFileNameChars() and call name.Replace(c, '_'), or use string.Join("_", name.Split(Path.GetInvalidFileNameChars())). Both give the same text for plain names.
Test that name.IndexOfAny(Path.GetInvalidFileNameChars()) is below zero. Also reject an empty name, and reserved names such as CON on Windows.
The file name list includes the separators, because a file name must not contain a slash. The path list leaves them out, because a path is made of folders and separators. The path list is shorter.

Next: GetInvalidPathChars()

Get the characters that are not allowed anywhere in a path on this operating system.

GetInvalidPathChars() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful