I have a binary file to which I want to append a chunk of data at the end of the file, how can I achieve this using C# and .net? also are there any considerations to take when writing to the end of a binary file? thanks a lot for your help.
我有一个二进制文件,我想在文件的末尾添加一大块数据,我怎样才能使用C#和.net实现这一点?在写入二进制文件的末尾时还有什么需要考虑的事项吗?非常感谢你的帮助。
3 个解决方案
#1
23
private static void AppendData(string filename, int intData, string stringData, byte[] lotsOfData)
{
using (var fileStream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None))
using (var bw = new BinaryWriter(fileStream))
{
bw.Write(intData);
bw.Write(stringData);
bw.Write(lotsOfData);
}
}
#2
5
You should be able to do this via the Stream
:
您应该可以通过Stream执行此操作:
using (FileStream data = new FileStream(path, FileMode.Append))
{
data.Write(...);
}
As for considerations - the main one would be: does the underlying data format support append? Many don't, unless it is your own raw data, or text etc. A well-formed xml document doesn't support append (without considering the final end-element), for example. Nor will something like a Word document. Some do, however. So; is your data OK with this...
至于考虑因素 - 主要的是:基础数据格式支持是否附加?除非它是您自己的原始数据或文本等,否则很多都不会。例如,格式良好的XML文档不支持追加(不考虑最终的结束元素)。也不会像Word文档那样。但有些人会这样做。所以;这是你的数据好......
#3
0
Using StreamWriter
and referencing DotNetPerls, make sure to add the True
boolean to the StreamWriter
constructor, if otherwise left blank, it'll overwrite as usual:
使用StreamWriter并引用DotNetPerls,确保将True布尔值添加到StreamWriter构造函数中,如果另外留空,它将像往常一样覆盖:
using System.IO;
class Program
{
static void Main()
{
// 1: Write single line to new file
using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
{
writer.WriteLine("Important data line 1");
}
// 2: Append line to the file
using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
{
writer.WriteLine("Line 2");
}
}
}
Output
(File "log.txt" contains these lines.)
Important data line 1
Line 2
This is the solution that I was actually looking for when I got here from Google, although it wasn't a binary file though, hope it helps someone else.
这是我从谷歌来到这里时实际寻找的解决方案,虽然它不是二进制文件,但希望它可以帮助其他人。
#1
23
private static void AppendData(string filename, int intData, string stringData, byte[] lotsOfData)
{
using (var fileStream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None))
using (var bw = new BinaryWriter(fileStream))
{
bw.Write(intData);
bw.Write(stringData);
bw.Write(lotsOfData);
}
}
#2
5
You should be able to do this via the Stream
:
您应该可以通过Stream执行此操作:
using (FileStream data = new FileStream(path, FileMode.Append))
{
data.Write(...);
}
As for considerations - the main one would be: does the underlying data format support append? Many don't, unless it is your own raw data, or text etc. A well-formed xml document doesn't support append (without considering the final end-element), for example. Nor will something like a Word document. Some do, however. So; is your data OK with this...
至于考虑因素 - 主要的是:基础数据格式支持是否附加?除非它是您自己的原始数据或文本等,否则很多都不会。例如,格式良好的XML文档不支持追加(不考虑最终的结束元素)。也不会像Word文档那样。但有些人会这样做。所以;这是你的数据好......
#3
0
Using StreamWriter
and referencing DotNetPerls, make sure to add the True
boolean to the StreamWriter
constructor, if otherwise left blank, it'll overwrite as usual:
使用StreamWriter并引用DotNetPerls,确保将True布尔值添加到StreamWriter构造函数中,如果另外留空,它将像往常一样覆盖:
using System.IO;
class Program
{
static void Main()
{
// 1: Write single line to new file
using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
{
writer.WriteLine("Important data line 1");
}
// 2: Append line to the file
using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
{
writer.WriteLine("Line 2");
}
}
}
Output
(File "log.txt" contains these lines.)
Important data line 1
Line 2
This is the solution that I was actually looking for when I got here from Google, although it wasn't a binary file though, hope it helps someone else.
这是我从谷歌来到这里时实际寻找的解决方案,虽然它不是二进制文件,但希望它可以帮助其他人。