c#读取bin文件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

namespace open_bin

{

public partial class BIN_LOAD : Form

{

public BIN_LOAD()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

//原版,打开几十kb的文件时间还行,上百kb之后时间就慢的卡住,主要是Mytext += j.ToString("X2");处理时间长,牵扯到垃圾回收机制

//string Mytext = "";

//int file_len;

//byte[] binchar = new byte[] { };

//FileStream Myfile = new FileStream("test2.bin", FileMode.Open, FileAccess.Read);

//BinaryReader binreader = new BinaryReader(Myfile);

//file_len = (int)Myfile.Length;//获取bin文件长度

//binchar = binreader.ReadBytes(file_len);

//foreach (byte j in binchar)

//{

// Mytext += j.ToString("X2");

// Mytext += " ";

//}

//textBox1.Text = Mytext;

//binreader.Close();

//改进版,读取上百kb的文件执行时间可以接受,StringBuilder执行效率远大于Mytext += j.ToString("X2")。

int file_len;

byte[] binchar = new byte[] { };

FileStream Myfile = new FileStream("test2.bin", FileMode.Open, FileAccess.Read);

BinaryReader binreader = new BinaryReader(Myfile);

file_len = (int)Myfile.Length;//获取bin文件长度

StringBuilder str = new StringBuilder();

binchar = binreader.ReadBytes(file_len);

foreach (byte j in binchar)

{

str.Append(j.ToString("X2"));

str.Append(" ");

}

textBox1.Text = str.ToString();

binreader.Close();

}

}

}

相关推荐
云:鸢10 分钟前
C语言链表设计及应用
c语言·开发语言·数据结构·链表
离越词1 小时前
C++day8作业
开发语言·c++·windows
℃CCCC1 小时前
请求库-axios
开发语言·华为·网络请求·harmonyos·deveco studio·axios请求·arkts编程
ling__i1 小时前
java day18
java·开发语言
矛取矛求1 小时前
日期类的实现
开发语言·c++·算法
大翻哥哥2 小时前
Python 2025:AI工程化与智能代理开发实战
开发语言·人工智能·python
ISDF-工软未来2 小时前
C# 泛型简单案例
c#
在下雨5992 小时前
项目讲解1
开发语言·数据结构·c++·算法·单例模式
再努力"亿"点点2 小时前
Sklearn(机器学习)实战:鸢尾花数据集处理技巧
开发语言·python
饭碗的彼岸one2 小时前
C++ 并发编程:异步任务
c语言·开发语言·c++·后端·c·异步