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 disk as binary files. These objects are graphic models compiled into a library. The objects have names embedded in them and I need to use that name as the file name.
I am using :
try {
// Open file for reading .
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) {
// Writes a block of bytes to this stream using data from a byte array. .
fileStream.Write(byteArray, 0, byteArray.Length);
// close file stream .
fileStream.Close();
}
return true;
}
catch (Exception exception) {
return false;
}
I realize my exception handling is not great! However the problem showed up when one of the objects to save had the name COM2 This caused an exception:
FileStream will not open Win32 devices such as disk partitions and tape drives.
So in my example I am trying to write a file called COM2.mdl and get this error. I really don't want to have to change these names since they are embedded by the developer.
I considered testing the names against a list of devices that may cause the error - but I really don't know what that list might be and also it would mean changing the name of the file which I don't want to do.
So my question: Is there a way to write a byte array as a binary file other than with FileStream that might get over this issue?
Many thanks