我想将一个字节数组 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 到文件末尾。
谢谢,
埃文
最佳答案
一种方法是创建一个 FileStream
与 FileMode.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/