简体   繁体   English

在C#中寻找和写入大于2GB的文件

[英]Seeking and writing files bigger than 2GB in C#

In C#, the FileStream 's methods Read/Write/Seek take integer in parameter. 在C#中, FileStream的方法Read / Write / Seek在参数中取integer 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. 但在我的情况下,我收到的数据块没有特别的顺序。

I have a code that works for files smaller than 2GB : 我有一个适用于小于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();
    }

Is there a way I can seek and write my chunks in files bigger than 2GB? 有没有办法可以在大于2GB的文件中搜索和编写我的块?

Don't use int, use long. 不要使用int,使用long。 Seek takes a long. 寻求需要很长时间。

You need to use long everywhere though and not just cast to int somewhere. 你需要在任何地方长时间使用,而不是仅仅在某处转换为int。

writer.Seek((long)position*chunkSize, SeekOrigin.Begin);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM