I have run into something curious. I have a decompiler that extracts information from a binary file. I am extracting a series of objects that I need to write separately to
Wrap the call to "new FileStream" with a try/catch block to specifically catch System.ArgumentException. If you catch this, assume the the filename is invalid and try again with a different filename (e.g. prepend "foo" to the filename string).
Also, you can use System.IO.Path.GetInvalidPathChars() and System.IO.Path.GetInvalidFileNameChars(); to get the complete list of "invalid characters" that won't fit inside a windows filename. So you can strip out or replace those chars fromthe filename string before attempting to create the file.
The reserved names are AUX, CLOCK$, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, CON, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, NUL and PRN
.
You won't be able to create files with these names (and with any extension, e.g. COM2.txt
on Windows on any File System - that's a Windows Kernel enforced thing, for backwards compatibility with CP/M. It MAY be a limitation on FAT filesystems though, but it's not on NTFS. See Wikipedia for some more info.
However, you can try to use UNC File Names, these should to work:
echo test > com2.txt
-> The system cannot find the file specified.
echo test > \\mypc\c$\Users\Michael\Desktop\com2.txt
-> works flawlessly
I'm not 100% sure if UNC Paths work with File Stream, but there certainly is a way to use them in .net.