In my windows app, I want to use memory mapped files. There is various article/blogs on net that has enough information for creating memory mapped files. I am creating 2 memory mapped file and now I want to do some actions on these files like reading its content, append some content into it, delete some content from it. There may be more info on net for all these, but unfortunately I couldn’t find anything.
Below is the function I am using to write a memory mapped file.
// 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);
}
}
If anyone can help me, that would be really appreciated.
I think the class you’re looking for is the
MemoryMappedViewAccessor. It provides methods for reading and writing to your memory-mapped file. A delete is nothing more than a series of carefully orchestrated writes.It can be created from your
MemoryMappedFileclass using theCreateViewAccessormethod.