Skip to content

[API Proposal]: MemoryMappedFile.CreateFromFile(SafeFileHandle fileHandle) #79187

@MJEdwin

Description

@MJEdwin

Background and motivation

Since .Net 7, MemoryMappedFile use SafeFileHandle instead of FileStream to create and use(see issue #63790), but the MemoryMappedFile.CreateFromFile method only accept filePath or fileStream to create.

API Proposal

using Microsoft.Win32.SafeHandles;

namespace System.IO.MemoryMappedFiles
{
    public class MemoryMappedFile : IDisposable
    {
        public static MemoryMappedFile CreateFromFile(SafeFileHandle fileHandle, string? mapName, long capacity, MemoryMappedFileAccess access, HandleInheritability inheritability, bool leaveOpen);
    }
}

API Usage

using SafeFileHandle fileHandle = File.OpenHandle("test.txt",FileAccess.ReadWrite);
int fileLength = RandomAccess.GetLength(fileHandle);
//open mmf using FileHandle
using MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileHandle, "mmf", fileLength, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true);

Alternative Designs

No response

Risks

This is a new extension method on MemoryMappedFile so I can't think of any risks of breaking changes.

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.IOgood first issueIssue should be easy to implement, good for first-time contributorshelp wanted[up-for-grabs] Good issue for external contributors

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions