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
Call
What it does
Path.GetInvalidFileNameChars()
A new char[] of the invalid characters.
name.IndexOfAny(bad) < 0
true 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
Parameter
Description
None
GetInvalidFileNameChars() takes no parameters.
Return Value
Return Value
Description
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));
}
}
Output
Has entries: True
Slash is invalid: True
Null char is invalid: True
Letter a is invalid: False
Digit 5 is invalid: False
How It Works
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.
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".
The null character.'\0' marks the end of a string in the file system. It is invalid everywhere, so the line is True.
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);
}
}
Output
a_b_c.txt
a_b_c.txt
True
True
How It Works
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.
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.
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.
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.
Output on Windows:
notes.txt valid: True
a/b.txt valid: False
report?.txt valid: False
a:b.txt valid: False
More than 2 invalid chars: True
Output on Linux/macOS:
notes.txt valid: True
a/b.txt valid: False
report?.txt valid: True
a:b.txt valid: True
More than 2 invalid chars: False
How It Works
Two blocks listed. A real run prints only the block for your system.
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.
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.
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.