如何使用内存映射文件C#读写文件? [英] How to read and write a file using Memory Mapped File C#?

查看:412
本文介绍了如何使用内存映射文件C#读写文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在D驱动器中有一个图像,例如"D:\ Image \ 1.tiff".我想读取此文件并将其写入其他位置,例如在路径"D:\ Project \"中.如何使用内存映射文件来做到这一点?

I have an image in D Drive like "D:\Image\1.tiff". I want to read this file and write it in an another location, for example in the path "D:\Project\". How to do this using Memory Mapped File?

推荐答案

我现在可以使用以下代码使用内存映射文件"实现读写文件:

I can now achieve reading and writing a file using Memory Mapped File using the below coding:

FileStream stream = File.OpenRead(@"D:\FFv1\dpx1\1.dpx");
byte[] fileBytes = new byte[stream.Length];
string Output = @"D:\Vanthiya Thevan\FFv1\dpx1\2.dpx";
using (var fileStream = new FileStream(Output, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
using (MemoryMappedFile memoryMapped = MemoryMappedFile.CreateFromFile(fileStream, "MapName", fileBytes.Length,
MemoryMappedFileAccess.ReadWrite, new MemoryMappedFileSecurity(), HandleInheritability.Inheritable, true))
{
    var viewStream = memoryMapped.CreateViewStream();
    viewStream.Write(fileBytes, 0, fileBytes.Length); 
}

这篇关于如何使用内存映射文件C#读写文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆