Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:50:51+00:00 2026-05-31T23:50:51+00:00

In C# 4.0, MemoryMappedFile has several factory methods: CreateFromFile , CreateNew , CreateOrOpen ,

  • 0

In C# 4.0, MemoryMappedFile has several factory methods: CreateFromFile, CreateNew, CreateOrOpen, or OpenExisting. I need to open the MemoryMappedFile if it exists, and if not, create it from a file. My code to open the memory map looks like this:

try
{
    map = MemoryMappedFile.OpenExisting(
        MapName,
        MemoryMappedFileRights.ReadWrite
        | MemoryMappedFileRights.Delete);
}
catch (FileNotFoundException)
{
    try
    {
        stream = new FileStream(
            FileName,
            FileMode.OpenOrCreate,
            FileAccess.ReadWrite,
            FileShare.Read | FileShare.Delete);
        map = MemoryMappedFile.CreateFromFile(
            stream, MapName, length + 16,
            MemoryMappedFileAccess.ReadWrite,
            null,
            HandleInheritability.None,
            false);
    }
    catch
    {
        if (stream != null)
            stream.Dispose();
        stream = null;
    }
}

It pretty much works the way I want it to, but it ends up throwing an exception on OpenExisting way too often. Is there a way to check whether the MemoryMappedFile actually exists before I try to do the OpenExisting call? Or do I have to deal with the exception every single time?

Also, is there a way to find out if the current handle is the last one to have the MemoryMappedFile open, to determine if the file will be closed when the current handle is disposed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:50:52+00:00Added an answer on May 31, 2026 at 11:50 pm

    See Han Passant’s comment under the original question for accepted answer. Since I can’t mark it as accepted, I’ll just refer to it here and mark this as accepted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is the code in question: using (var file = MemoryMappedFile.OpenExisting(AIDA64_SensorValues)) { using (var
I have a project that has a fairly complicated, nested (references calling the referenced
I am trying to communicate between two processes. From MSDN Documentation, I came across
I'm working on a copy protection software and I'm trying to create a wrapper
I realize .NET 4.0 is in Beta, but I'm hoping someone has a resolution
I'm tasked with implementing a search function that will search through several large (couple
ok so ive been working on a program. i has 3 classes. 2 of
I would like to store a struct into a MemoryMappedFile, but this struct contains
I've made a simple test with a MemoryMappedFile as msdn says : 2 processes,
Under windows, this F# code will extend a file from say 12 bytes out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.