私はこのコードを使用しています
string location1 = textBox2.Text;
byte[] bytes = File.ReadAllBytes(location1);
string text = (Convert.ToBase64String(bytes));
richTextBox1.Text = text;
しかし、大きすぎるファイルを使用すると、メモリ不足の例外が発生します。
File.ReadAllBytes
チャンクで使いたい。私は以下のようなコードを見ました
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
しかし、実際にをテキストに変換するバイト配列に変換する方法がわかりません。
編集:答えが見つかりました。これがコードです!
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))。みんな、ありがとう!