C# - 如何读写二进制文件? [英] C# - How do I read and write a binary file?

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

问题描述

我如何读取任意文件的原始字节数组,写字节数组返回到一个新的文件?

How do I read a raw byte array from any file, and write that byte array back into a new file?

推荐答案

(编辑:请注意,这个问题更改;它没有提及字节[] 最初,看到的修订版1

(edit: note that the question changed; it didn't mention byte[] initially; see revision 1)

好吧, File.Copy 跃居头脑;但除此之外,这听起来像一个情况:

Well, File.Copy leaps to mind; but otherwise this sounds like a Stream scenario:

    using (Stream source = File.OpenRead(inPath))
    using (Stream dest = File.Create(outPath)) {
        byte[] buffer = new byte[2048]; // pick size
        int bytesRead;
        while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
            dest.Write(buffer, 0, bytesRead);
        }
    }

这篇关于C# - 如何读写二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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