1

我的问题是我无法打开文件。在另一个进程中或在同一个进程中!

代码:

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);
4

1 回答 1

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
    }
}
于 2012-12-14T23:15:20.707 回答