Aside of doing crazy cycles like this for every type of port:
bool IsGoodFileName(string file_name)
{
for (int i = 0; i < 256; i++)
if (string.Compare(file_name, "COM" + i) == 0)
return false;
return true;
}
According to the documentation, you don't have to check beyond port 9 for these, so you could do something like this:
static bool IsGoodFileName(string file_name)
{
var reserved = new[]
{
"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
};
var fileName = Path.GetFileNameWithoutExtension(file_name);
//TODO: handle case where fileName is null.
return !reserved.Any(r => fileName.Equals(r, StringComparison.InvariantCultureIgnoreCase));
}
And indeed, Windows Explorer lets you make a file named COM20, so I don't think you need to check those.