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

}

}

}

相关推荐
唐青枫1 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech2 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf3 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6253 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech4 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
LDR0064 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术4 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园4 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob4 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享4 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm