在C#中查找和写入大于2GB的文件 [英] Seeking and writing files bigger than 2GB in C#
问题描述
在C#中,FileStream
的方法Read/Write/Seek在参数中使用integer
.在上一篇文章中,我已经看到了一种读取/写入以下文件的好方法:比分配给进程的虚拟内存大.
In C#, the FileStream
's methods Read/Write/Seek take integer
in parameter. In a previous post , I have seen a good solution to read/write files that are bigger than the virtual memory allocated to a process.
如果您要从头到尾写入数据,则此解决方案有效.但就我而言,我接收的数据块没有特定的顺序.
This solution works if you want to write the data from the beginning to the end. But in my case, the chunks of data I am receiving are in no particular order.
我有一个适用于小于2GB的文件的代码:
I have a code that works for files smaller than 2GB :
private void WriteChunk(byte[] data, int position, int chunkSize, int count, string path)
{
FileStream destination = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(destination);
writer.Seek((int) (position*chunkSize), SeekOrigin.Begin);
writer.Write(data, 0, count);
writer.Close();
}
有没有一种方法可以在大于2GB的文件中查找和写入我的块?
Is there a way I can seek and write my chunks in files bigger than 2GB?
推荐答案
不要使用int,请使用long.寻求需要很长时间.
Don't use int, use long. Seek takes a long.
尽管您需要在各处使用long,而不仅仅是将int转换为int.
You need to use long everywhere though and not just cast to int somewhere.
这篇关于在C#中查找和写入大于2GB的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!