将文件转换为字节 [英] convert file to byte

查看:137
本文介绍了将文件转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将文件转换为字节
并更改此字节
并使用新文件保存更改
thnx寻求帮助

how i can convert file to bytes
and change this byte
and save a change with new file
thnx for any help

推荐答案


  1. 使用File.ReadAllBytes读取文件中的字节.
  2. 根据需要直接修改返回的字节数组.
  3. 使用File.WriteAllBytes保存修改后的字节. /li>

  1. Use File.ReadAllBytes to read the bytes in the file.
  2. Directly modify the returned byte array as you please.
  3. Use File.WriteAllBytes to save the modified bytes.




----------

下面的示例代码(由OP取代):




----------

Sample code below (requsted by OP):

byte[] bytes = File.ReadAllBytes(@"d:\abc.pdf");
bytes[10] = 34;
bytes[11] = 89;
File.WriteAllBytes(@"d:\abc.pdf", bytes);


Nishant Sivakumar提供了一个很好的答案,但我想指出一个适用于较大文件的替代方法:

使用 MemoryMappedFile [MemoryMappedViewAccessor [
Nishant Sivakumar has provided a good answer, but I''d like to point to an alternative suitable for larger files:

Use MemoryMappedFile[^] with MemoryMappedViewAccessor[^] to read and write data.

Perhaps like this (copied from the documentation):

using (var accessor = mmf.CreateViewAccessor(offset, length))
{

  int colorSize = Marshal.SizeOf(typeof(MyColor));
  MyColor color;

  // Make changes to the view.
  for (long i = 0; i < length; i += colorSize)
  {
    accessor.Read(i, out color);
    color.Brighten(10);
    accessor.Write(i, ref color);
  }
}



.Net 4引入了处理内存映射文件的功能,因此很容易被忽略.

问候
Espen Harlinn



This ability to work with memory mapped files was introduced with .Net 4 - so it''s easy to overlook.

Regards
Espen Harlinn


这篇关于将文件转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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