写入具有设备名称的文件

发布于 2024-10-24 03:49:55 字数 1000 浏览 4 评论 0原文

我遇到了一些奇怪的事情。我有一个反编译器,可以从二进制文件中提取信息。我正在提取一系列需要作为二进制文件单独写入磁盘的对象。这些对象是编译到库中的图形模型。这些对象中嵌入了名称,我需要使用该名称作为文件名。

我正在使用:

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;
        }

我意识到我的异常处理不是很好!但是,当要保存的对象之一名为 COM2 时,问题就出现了,这导致了异常:

FileStream 不会打开 Win32 设备,例如磁盘分区和磁带驱动器。

因此,在我的示例中,我尝试编写一个名为 COM2.mdl 的文件并收到此错误。我真的不想更改这些名称,因为它们是由开发人员嵌入的。

我考虑过根据可能导致错误的设备列表来测试名称 - 但我真的不知道该列表可能是什么,而且这也意味着更改我不想做的文件的名称。

所以我的问题是:除了使用 FileStream 之外,是否有一种方法可以将字节数组写入二进制文件来解决这个问题?

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-10-31 03:49:55

保留名称为 AUX、CLOCK$、COM1、COM2、COM3、COM4、COM5、COM6、COM7、COM8、COM9、CON、LPT1、LPT2、LPT3、LPT4、LPT5、LPT6、LPT7、LPT8、LPT9、 NUL 和 PRN。

您将无法在 Windows 上的任何文件系统上创建具有这些名称(以及任何扩展名,例如 COM2.txt)的文件 - 这是 Windows 内核强制执行的事情,对于 向后兼容 CP/M。这可能是一个限制 在 FAT 文件系统上有效,但在 NTFS 上则不然。

虽然 ,您可以尝试使用 UNC 文件名,这些应该可以工作:

echo test > com2.txt
-> The system cannot find the file specified.

echo test > \\mypc\c$\Users\Michael\Desktop\com2.txt
-> works flawlessly

我不能 100% 确定 UNC 路径是否适用于文件流,但肯定有一种方法可以在 .net 中使用它们。

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.

小兔几 2024-10-31 03:49:55

使用 try/catch 块包装对“new FileStream”的调用,以专门捕获 System.ArgumentException。如果您发现此问题,请假定文件名无效,然后使用不同的文件名重试(例如,在文件名字符串前面添加“foo”)。

另外,您可以使用 System.IO.Path.GetInvalidPathChars() 和 System.IO.Path.GetInvalidFileNameChars();获取不适合 Windows 文件名的“无效字符”的完整列表。因此,您可以在尝试创建文件之前从文件名字符串中删除或替换这些字符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文