如何编辑C#中特定位置的二进制文件中的数据 [英] How to edit data in a binaryfile on a specific position in C#

查看:272
本文介绍了如何编辑C#中特定位置的二进制文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过上传二进制文件来编辑特定位置的二进制文件中的特定数据。如何在c#中执行此操作。



我尝试过:



代码

<

使用(FileStream sr = File.OpenRead(someFile.dat))

{

sr.Seek(100,SeekOrigin.Begin);

int read = sr.ReadByte();

// ...

}



>

解决方案

而不是寻找一个流,只需阅读整个dtaa:

  byte  [] data File.ReadAllBytes(pathToFile); 

然后,您可以使用数组索引分别访问每个字节,并使用File.WriteAllBytes保存更改后的数据结束。


I have to edit a particular data in a binary file at a particular position by uploading a binary file.How can i do this in c#.

What I have tried:

code
<
using (FileStream sr = File.OpenRead("someFile.dat"))
{
sr.Seek(100, SeekOrigin.Begin);
int read = sr.ReadByte();
//...
}

>

解决方案

Instead of seeking a stream, just read the whole dtaa in:

byte[] data File.ReadAllBytes(pathToFile);

You can then use the array index to access each byte individually, and use File.WriteAllBytes to save the changed data when you are finished.


这篇关于如何编辑C#中特定位置的二进制文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆