【问题标题】:Read, Write, Append, Delete Memory Mapped File读取、写入、追加、删除内存映射文件
【发布时间】:2012-03-01 05:42:39
【问题描述】:

在我的 Windows 应用程序中,我想使用内存映射文件。网上有各种文章/博客有足够的信息来创建内存映射文件。我正在创建 2 个内存映射文件,现在我想对这些文件执行一些操作,例如读取其内容、将一些内容附加到其中、从中删除一些内容。网上可能有更多关于所有这些的信息,但不幸的是我找不到任何东西。 下面是我用来编写内存映射文件的函数。

 // Stores the path to the selected folder in the memory mapped file
        public void CreateMMFFile(string folderName, MemoryMappedFile mmf, string fileName)
        {
            // Lock
            bool mutexCreated;
            Mutex mutex = new Mutex(true, fileName, out mutexCreated);
            try
            {
                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    using (StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.Unicode))
                    {
                        try
                        {
                            string[] files = System.IO.Directory.GetFiles(folderName, "*.*", System.IO.SearchOption.AllDirectories);
                            foreach (string str in files)
                            {
                                writer.WriteLine(str);
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Unable to write string. " + ex);
                        }
                        finally
                        {
                            mutex.ReleaseMutex();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Unable to monitor memory file. " + ex);
            }
        }

如果有人可以帮助我,那将不胜感激。

【问题讨论】:

  • MSDN documentation examples 的哪一部分您在理解上特别有问题?
  • 我没有说那样的话。否决票??原因??
  • 文档(特别是文档中的示例)包含从/向 MMF 读取和写入的示例,所以我很困惑;你在问什么?
  • 糟糕,我没看到。
  • 它在我链接的页面底部。

标签: c# memory-mapped-files


【解决方案1】:

我认为您正在寻找的课程是MemoryMappedViewAccessor。它提供了读取和写入内存映射文件的方法。删除只不过是一系列精心编排的写入。

可以使用CreateViewAccessor 方法从您的MemoryMappedFile 类创建。

【讨论】:

    【解决方案2】:

    在这段代码中,我做了一些类似于你想要实现的事情。我每秒都会写入 MMF,您可以让其他进程从该文件中读取内容:

    var data = new SharedData
    {
        Id = 1,
        Value = 0
    };
    
    var mutex = new Mutex(false, "MmfMutex");
    
    using (var mmf = MemoryMappedFile.CreateOrOpen("MyMMF", Marshal.SizeOf(data)))
    {
         using (var accessor = mmf.CreateViewAccessor())
         {
              while (true)
              {
                  mutex.WaitOne();
                  accessor.Write(0, ref data);
                  mutex.ReleaseMutex();
    
                  Console.WriteLine($"Updated Value to: {data.Value}");
                  data.Value++;
                  Thread.Sleep(1000);
               }
         }
    }
    

    查看this article,了解如何使用 MMF 在进程之间共享数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2017-09-18
      • 2016-01-08
      相关资源
      最近更新 更多