C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Reserved filenames in Windows operating systems
aux
con
clock$
nul
prn
com1
com2
com3
com4
com5
com6
com7
com8
com9
lpt1
lpt2
lpt3
lpt4
lpt5
lpt6
lpt7
lpt8
lpt9
Examples of reserved filenames
aux.txt
Con.bin
NUL.png
com2.png
lpt8.aspx
Class that contains reserved names: C#
using System.IO;
/// <summary>
/// Contains logic for detecting reserved file names.
/// </summary>
static class ReservedFileNameTool
{
/// <summary>
/// Reserved file names in Windows.
/// </summary>
static string[] _reserved = new string[]
{
"con",
"prn",
"aux",
"nul",
"com1",
"com2",
"com3",
"com4",
"com5",
"com6",
"com7",
"com8",
"com9",
"lpt1",
"lpt2",
"lpt3",
"lpt4",
"lpt5",
"lpt6",
"lpt7",
"lpt8",
"lpt9",
"clock$"
};
/// <summary>
/// Determine if the path file name is reserved.
/// </summary>
public static bool IsReservedFileName(string path)
{
string fileName = Path.GetFileNameWithoutExtension(path); // Extension doesn't matter
fileName = fileName.ToLower(); // Case-insensitive
foreach (string reservedName in _reserved)
{
if (reservedName == fileName)
{
return true;
}
}
return false;
}
/// <summary>
/// Determine if the path file name is reserved.
/// </summary>
public static bool IsNotReservedFileName(string path)
{
return !IsReservedFileName(path);
}
}
But: There must be further letters. You are not allowed to use the reserved file names.
Note: This code could be sped up with a static Dictionary. But the performance difference isn't important in many cases.
Static Dictionary