将大文件转换为c#win中的字节。形成 [英] Convert Big file to bytes in c# win. form

查看:121
本文介绍了将大文件转换为c#win中的字节。形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 2.17 GB 文件,在转换时会出错。需要非负数。参数名称:计数。我知道因为文件的字节对整数来说太大了。但是如何解决这个问题?



I have a "2.17 GB" file and at the time of conversion it gives error.Non-negative number required.Parameter name: count. I know because the file's byte is too large for integer. But How to solve this?

OpenFileDialog f = new OpenFileDialog();
f.ShowDialog();
if (f.FileName != null && f.FileName != "")
{
        string filePath = f.FileName;
        string connectionString = Program.connectionString;
        FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        BinaryReader reader = new BinaryReader(stream);
        byte[] file = reader.ReadBytes((int)stream.Length);//This line gives error
        reader.Close();
        stream.Close();
}

推荐答案

你不能。

大小有一个基本限制.NET中任何一个对象:2 GB。

如果你运行32位系统或64,没有一个对象可以超过2GB没关系。

当你将long转换为int时,加上2GB是80000000 hex,这将溢出一个int并变为负数,你开始遇到真正的问题!



你必须找到一些方法来操作一次不涉及读取整个文件的文件,并使用较小的块。
You can't.
There is a fundamental limit on the size of any one object in .NET: 2 GB.
It doesn't matter if you run a 32 bit system or 64, no one single object can exceed 2GB.
Add to that when you convert a long to an int, 2GB is 80000000 hex, which will overflow an int and go negative, and you start to get real problems!

You will have to find some way to manipulate the file that doesn't involve reading the whole file at a time, and work with smaller chunks.


这篇关于将大文件转换为c#win中的字节。形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆