【发布时间】:2022-01-20 10:22:19
【问题描述】:
我似乎找不到比以下更有效的方法将嵌入式资源“复制”到磁盘:
using (BinaryReader reader = new BinaryReader(
assembly.GetManifestResourceStream(@"Namespace.Resources.File.ext")))
{
using (BinaryWriter writer
= new BinaryWriter(new FileStream(path, FileMode.Create)))
{
long bytesLeft = reader.BaseStream.Length;
while (bytesLeft > 0)
{
// 65535L is < Int32.MaxValue, so no need to test for overflow
byte[] chunk = reader.ReadBytes((int)Math.Min(bytesLeft, 65536L));
writer.Write(chunk);
bytesLeft -= chunk.Length;
}
}
}
似乎没有更直接的方法来进行复制,除非我遗漏了什么......
【问题讨论】:
-
对我来说看起来不错。是不是感觉代码行数太多了?
-
感觉应该有比分块更直接的方式。