How to read and write a file using Memory Mapped File C#?

前端 未结 2 1401
遥遥无期
遥遥无期 2021-01-01 07:03

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 thi

2条回答
  •  被撕碎了的回忆
    2021-01-01 07:26

    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); 
    }
    

提交回复
热议问题