本文介绍了我如何在块中使用 File.ReadAllBytes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此代码
string location1 = textBox2.Text;
byte[] bytes = File.ReadAllBytes(location1);
string text = (Convert.ToBase64String(bytes));
richTextBox1.Text = text;
但是当我使用一个太大的文件时,我会出现内存不足的异常.
But when I use a file that is too big I get out of memory exception.
我想在块中使用 File.ReadAllBytes
.我在下面看到过这样的代码
I want to use File.ReadAllBytes
in chunks. I have seen code like this below
System.IO.FileStream fs = new System.IO.FileStream(textBox2.Text, System.IO.FileMode.Open);
byte[] buf = new byte[BUF_SIZE];
int bytesRead;
// Read the file one kilobyte at a time.
do
{
bytesRead = fs.Read(buf, 0, BUF_SIZE);
// 'buf' contains the last 1024 bytes read of the file.
} while (bytesRead == BUF_SIZE);
fs.Close();
}
但我不知道如何将 bytesRead
实际转换成一个字节数组,然后我将其转换为文本.
But I don't know how to actually convert the bytesRead
into a byte array which I will convert to text.
找到答案.这是代码!
private void button1_Click(object sender, EventArgs e)
{
const int MAX_BUFFER = 2048;
byte[] Buffer = new byte[MAX_BUFFER];
int BytesRead;
using (System.IO.FileStream fileStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read))
while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0)
{
string text = (Convert.ToBase64String(Buffer));
textBox1.Text = text;
}
}
要更改文本格式的可读字节,请创建一个新字节并使其相等(Convert.FromBase64String(Text)).谢谢大家!
To change the readable bytes which are in text format, create a new byte and make it equal(Convert.FromBase64String(Text)). Thanks everyone!
推荐答案
private void button1_Click(object sender, EventArgs e)
{
const int MAX_BUFFER = 2048;
byte[] Buffer = new byte[MAX_BUFFER];
int BytesRead;
using (System.IO.FileStream fileStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read))
while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0)
{
string text = (Convert.ToBase64String(Buffer));
textBox1.Text = text;
}
}
这篇关于我如何在块中使用 File.ReadAllBytes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!