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();

}

}

}

相关推荐
Ramble_Naylor9 小时前
Rust深度解读:异步流(Async Streams)与生成器的处理艺术
开发语言·后端·rust
努力实干9 小时前
简单工厂模式(Simple Factory Pattern)——C++
开发语言·c++·简单工厂模式
熊猫钓鱼>_>9 小时前
平行视界兼容模式与魔法变量配置避坑【鸿蒙心迹】
运维·服务器·开发语言·笔记·华为·harmonyos·openharmony
大鹏说大话9 小时前
PHP 大文件上传解决方案:分片上传完整思路
开发语言·php
Wang's Blog10 小时前
Java 项目实战:交友项目 - 我的访客:需求说明
java·开发语言·交友
布莱克60511 小时前
C++ 中堆和栈的区别详解
开发语言·c++·栈·堆
像风一样自由202011 小时前
3.Dockerfile如何构建一个Python算法镜像
开发语言·人工智能·python·大模型
BuHuaX12 小时前
Lua 与 ToLua:运行机制及热更新原理
开发语言·unity·junit·c#·lua
xys_67812 小时前
深挖C++:C++入门基础
开发语言·c++