微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 读取,写入,追加,删除内存映射文件

在我的 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);
            }
        }

如果有人可以帮助我,那将非常感激.

解决方法

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

它可以使用CreateViewAccessor方法从MemoryMappedFile类创建.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐