在我的Windows应用程序中,我想使用内存映射文件。网络上有各种文章/博客提供了足够的信息来创建内存映射文件。我正在创建两个内存映射文件,现在我想对这些文件执行一些操作,例如读取它的内容,追加一些内容到其中,从其中删除一些内容。可能有更多关于所有这些的信息在网络上,但不幸的是我找不到任何东西。
以下是我正在使用的编写内存映射文件的函数。
如果有人能帮忙,那将不胜感激。
// 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);
}
}
如果有人能帮忙,那将不胜感激。