[英]Using a BinaryWriter for partial file updates
I'm looking for a method by which I can do partial updates, adding and removing key/value pairs to a file from my dictionary using BinaryWriter
. 我正在寻找一种可以进行部分更新的方法,可以使用BinaryWriter
从字典中添加键/值对并将其删除。 Overwriting a complete file seems simple enough with the following code but since I could potentially have a lot of records, ~26,000, I'm worried that performance would become an issue. 使用下面的代码覆盖一个完整的文件似乎很简单,但是由于我可能有很多记录(约26,000个),因此我担心性能会成为问题。 (I have not run tests to confirm this). (我尚未运行测试来确认这一点)。
The context in which I need this is for a persistable index of cached image files accessed via a HttpModule
. 我需要的上下文是通过HttpModule
访问的缓存图像文件的持久索引。
My starting point: 我的出发点:
using (FileStream fileStream = File.Create(this.cachedIndexFile))
{
using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
{
// Put the count.
binaryWriter.Write(this.Count);
// Put the values.
foreach (var pair in this)
{
binaryWriter.Write(pair.Key);
binaryWriter.Write(pair.Value.ToString());
}
}
}
If there are better alternatives within the framework I would happily use them. 如果框架内有更好的选择,我会很乐意使用它们。 BinaryWriter
is the only thing I know of and searching alternatives has proved unrewarding. BinaryWriter
是我唯一了解的事情,并且搜索替代品已证明是毫无用处的。
I have found this article here but have found it difficult to follow and the downloadable project file even more difficult. 我在这里找到了这篇文章,但发现很难理解,并且下载可下载的项目文件更加困难。 Perhaps a solution could be based upon that? 也许可以基于此解决方案?
Following the advice in comments I decided that indeed a database was the best idea. 遵循注释中的建议,我认为确实最好的数据库是数据库。 I have gone with SQLite. 我已经使用SQLite。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.