C# 将字节数组 append 到现有文件

标签 c# arrays byte append concatenation

我想将一个字节数组 append 到一个已经存在的文件 (C:\test.exe)。假设以下字节数组:

byte[] appendMe = new byte[ 1000 ] ;

File.AppendAllBytes(@"C:\test.exe", appendMe); // Something like this - Yes, I know this method does not really exist.

我会使用 File.WriteAllBytes 执行此操作,但我将使用一个巨大的字节数组,并且不断抛出 System.MemoryOverload 异常。因此,我很可能必须将大数组拆分成多个部分,并将每个字节数组 append 到文件末尾。

谢谢,

埃文

最佳答案

一种方法是创建一个 FileStreamFileMode.Append创作模式。

Opens the file if it exists and seeks to the end of the file, or creates a new file.

这看起来像:

public static void AppendAllBytes(string path, byte[] bytes)
{
    //argument-checking here.

    using (var stream = new FileStream(path, FileMode.Append))
    {
        stream.Write(bytes, 0, bytes.Length);
    }
}

关于C# 将字节数组 append 到现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6862368/

相关文章:

c# - Owin 中间件中的 MVC 错误处理

c# - 如何从服务器加载 Dicom 图像(路径包含 https ://)

c - char 数组的 SizeOf 函数的工作

python - PIL : Convert Bytearray to Image

c# - 如何清除一个字节中的一个位?

c# - 我如何在 Web 应用程序中使用配置文件

c# - 将 geoJson 数据转换为 sql server 空间数据类型 (GIS)

python - 根据另一个数组的索引取一个数组的平均值

JavaScript 循环遍历多维 JSON 数组

C++ - 从控制台获取字节