相关疑难解决方法(0)

将汇编资源流中的文件写入磁盘

我似乎无法找到一种更有效的方法将嵌入式资源"复制"到磁盘,而不是以下方法:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

似乎没有更直接的方式来复制,除非我遗漏了一些东西......

.net c#

44
推荐指数
3
解决办法
4万
查看次数

标签 统计

.net ×1

c# ×1