UnauthorizedAccessException对MemoryMappedFile在C#4 [英] UnauthorizedAccessException on MemoryMappedFile in C# 4

查看:492
本文介绍了UnauthorizedAccessException对MemoryMappedFile在C#4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想玩弄使用MemoryMappedFile访问现有二进制文件。如果这甚至在所有可能的还是我一个疯狂的人?

I wanted to play around with using a MemoryMappedFile to access an existing binary file. If this even at all possible or am I a crazy person?

我们的想法是将现有的二进制文件直接映射到内存中以便一些preferably更高速度的操作。还是要ATLEAST看到这些东西是如何运作的。

The idea would be to map the existing binary file directly to memory for some preferably higher-speed operations. Or to atleast see how these things worked.

        using System.IO.MemoryMappedFiles;


        System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\testparsercap.pcap");
        MemoryMappedFileSecurity sec = new MemoryMappedFileSecurity();
        System.IO.FileStream file = fi.Open(System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
        MemoryMappedFile mf = MemoryMappedFile.CreateFromFile(file, "testpcap", fi.Length, MemoryMappedFileAccess.Read, sec, System.IO.HandleInheritability.Inheritable, true);
        MemoryMappedViewAccessor FileMapView = mf.CreateViewAccessor();
        PcapHeader head = new PcapHeader();
        FileMapView.Read<PcapHeader>(0, out head);

我得到System.UnauthorizedAccessException的是未处理(消息=访问路径被拒绝)对mf.CreateViewAccessor()行。

I get System.UnauthorizedAccessException was unhandled (Message=Access to the path is denied.) on the mf.CreateViewAccessor() line.

我不认为这是文件的权限,因为我正在为一个不错的不安全管理员用户,并且没有任何其它程序打开,可能对文件的读锁。这是Vista的UAC的残疾人。

I don't think it's file-permissions, since I'm running as a nice insecure administrator user, and there aren't any other programs open that might have a read-lock on the file. This is on Vista with UAC disabled.

如果这是根本不可能的,我错过了一些文档中,请让我知道。我几乎无法找到在.NET 4.0的所有引用这个功能什么

If it's simply not possible and I missed something in the documentation, please let me know. I could barely find anything at all referencing this feature of .net 4.0

谢谢!

推荐答案

我知道这是一个老问题,但我只是碰到了同样的错误,并能解决这个问题。

I know this is an old question, but I just ran into the same error and was able to solve it.

尽管我打开MemoryMappedFile为只读(MemoryMappedFileRights.Read)有你在,我还需要创建视图访问为只读,以及:

Even though I was opening the MemoryMappedFile as read-only (MemoryMappedFileRights.Read) as you are, I also needed to create the view accessor as read-only as well:

var view = mmf.CreateViewAccessor(offset, size, MemoryMappedFileAccess.Read);

然后它的工作。希望这可以帮助别人。

Then it worked. Hope this helps someone else.

这篇关于UnauthorizedAccessException对MemoryMappedFile在C#4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆