gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:50:17 29 4
gpt4 key购买 nike

在我的 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 类创建方法。

关于c# - 读取、写入、追加、删除内存映射文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9511199/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com