我的问题是我无法打开文件。在另一个过程中或在同一过程中!
代码:
var path = @"c:\work\mmf.dat";
var map = "testmap123";
var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
if (fs.Length == 0)
{
fs.SetLength(1024);
}
var sec = new MemoryMappedFileSecurity();
var mem = MemoryMappedFile.CreateFromFile(fs, map, fs.Length, MemoryMappedFileAccess.ReadWrite, sec, HandleInheritability.Inheritable, false);
// Problem here System.UnauthorizedAccessException
var tmp = MemoryMappedFile.OpenExisting(map, MemoryMappedFileRights.FullControl, HandleInheritability.Inheritable);
答案 0 :(得分:1)
尝试以下方法:
var path = @"c:\diverse\mmf.dat";
var map = "testmap123";
using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
if (fs.Length == 0)
{
fs.SetLength(1024);
}
}
using (var mem = MemoryMappedFile.CreateFromFile(path, FileMode.Open, map, 1024, MemoryMappedFileAccess.Read))
{
using (var tmp = MemoryMappedFile.OpenExisting(map))
{
//work with tmp
}
}