In C#, Path.GetInvalidPathChars() returns a char[] of the characters that are not allowed anywhere in a path on the current system. It is the shorter sibling of GetInvalidFileNameChars: it does not contain the separators, because a path is made of folders and separators.
The list depends on the system. On Windows it holds about 33 characters in current .NET: the control characters and | (the old .NET Framework also listed ", <, and >). On Linux and macOS it has one entry: the null character '\0'. Characters such as ? and * are not in this list, and they are checked later, when the file system is used.
This is a static method of System.IO.Path. Call it as Path.GetInvalidPathChars(...); 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 path with IndexOfAny.
C#
char[] bad = Path.GetInvalidPathChars();
bool nullBad = Array.IndexOf(bad, '\0') >= 0; // true on every system
bool ok = "/data/report.txt".IndexOfAny(bad) < 0; // true
Call
What it does
Path.GetInvalidPathChars()
A new char[] of invalid path characters.
path.IndexOfAny(bad) < 0
true when the path has none of them.
Path.GetInvalidFileNameChars()
The longer list for a single name; it adds the separators.
Array.IndexOf(bad, '/')
Returns -1: the slash is a valid path character.
Since .NET Core 2.1, most Path methods no longer throw for these characters. They simply work on the text, and the error appears later when you call File or Directory. So check the path yourself if it comes from a user.
Library declarations
C#
public static char[] GetInvalidPathChars();
Parameters
Parameter
Description
None
GetInvalidPathChars() takes no parameters.
Return Value
Return Value
Description
char[]
A new array of the characters that are invalid in a path on this system. It is a copy; changing it has no effect on the list.
C# Examples
Example 1 — Check Facts That Hold on Every System
Some characters are always on the list, some never. Test those, not the size.
C#
using System;
using System.IO;
class Program {
static void Main() {
char[] bad = Path.GetInvalidPathChars();
Console.WriteLine("Has entries: " + (bad.Length > 0));
Console.WriteLine("Null char is invalid: " + (Array.IndexOf(bad, '\0') >= 0));
Console.WriteLine("Slash is invalid: " + (Array.IndexOf(bad, '/') >= 0));
Console.WriteLine("Letter a is invalid: " + (Array.IndexOf(bad, 'a') >= 0));
}
}
Output
Has entries: True
Null char is invalid: True
Slash is invalid: False
Letter a is invalid: False
How It Works
At least one entry. On Linux the array has a single character and on Windows about 33, so the program only tests that it is not empty.
The null character.'\0' is forbidden in every path on every system, so the answer is True.
The slash is allowed. A path needs separators, so the slash is not on this list and Array.IndexOf gives a negative number, which makes the line False.
Letters are fine. Normal letters are never invalid, so the last line is False. The next example compares the file name list.
Example 2 — Validate a Path Before Using It
Test three paths with IndexOfAny. One has an illegal null character.
C#
using System;
using System.IO;
class Program {
static bool HasBadChar(string path) {
return path.IndexOfAny(Path.GetInvalidPathChars()) >= 0;
}
static void Main() {
Console.WriteLine("/data/report.txt: " + HasBadChar("/data/report.txt"));
Console.WriteLine("/data/new folder/a b.txt: " + HasBadChar("/data/new folder/a b.txt"));
Console.WriteLine("path with a null char: " + HasBadChar("/data/rep\0ort.txt"));
}
}
Output
/data/report.txt: False
/data/new folder/a b.txt: False
path with a null char: True
How It Works
A helper.HasBadChar returns true when IndexOfAny finds a character from the list, which is the opposite of the file name check in the other method.
A normal path. Slashes, letters, and a dot are all allowed in a path, so the first answer is False.
Spaces are allowed. A space in a folder or file name is legal, so the second path is also False.
The null character. The last path has \0 inside the file name, so the answer is True. The label is written separately, because a null character cannot be shown in text output.
Example 3 — Compare the Path List With the File Name List
Ask both lists about the same three characters and see where they differ. The slash is the key difference.
C#
using System;
using System.IO;
class Program {
static void Main() {
char[] pathBad = Path.GetInvalidPathChars();
char[] nameBad = Path.GetInvalidFileNameChars();
string[] labels = { "slash", "null char", "letter a" };
char[] chars = { '/', '\0', 'a' };
for (int i = 0; i < chars.Length; i++) {
bool inPath = Array.IndexOf(pathBad, chars[i]) >= 0;
bool inName = Array.IndexOf(nameBad, chars[i]) >= 0;
Console.WriteLine(labels[i] + " -> path invalid: " + inPath + ", file name invalid: " + inName);
}
Console.WriteLine("File name list is longer: " + (nameBad.Length > pathBad.Length));
}
}
Output
slash -> path invalid: False, file name invalid: True
null char -> path invalid: True, file name invalid: True
letter a -> path invalid: False, file name invalid: False
File name list is longer: True
How It Works
Parallel arrays.labels holds readable names, because the null character cannot be printed. chars holds the real characters in the same order.
The slash. It is a valid part of a path (it separates folders) but not of a file name, so the first line says False for the path and True for the file name.
The null character. It is invalid in both lists, and the letter a is invalid in neither. Only the slash differs among these three.
Which list is longer? The file name list holds everything the path list has, plus the separators and, on Windows, more. On Linux that is two against one, and on Windows about 41 against 33, so the answer is True.
Frequently Asked Questions
The characters that cannot appear in a path on this system. On Linux and macOS that is only the null character. On Windows it is about 33 characters in current .NET: the control characters and the vertical bar. Older .NET Framework also listed the quote, < and >.
No. The slash separates folders, so it is valid in a path on every system. It is invalid in a file name, which is why GetInvalidFileNameChars has it and GetInvalidPathChars does not.
A path may contain separators and, on Windows, a colon after the drive letter. A file name may not. So the file name list is the path list plus those extra characters.
Not in newer .NET (Core 2.1 and later). Older .NET Framework threw ArgumentException. Now the bad text is passed along, and the failure comes when you open or create the file.
Test path.IndexOfAny(Path.GetInvalidPathChars()) < 0, then try the real operation inside try and catch. The list alone cannot find every problem, such as a reserved name or a path that is too long.