无法删除 MemoryMappedFile 的文件

发布于 2024-11-17 13:26:45 字数 811 浏览 3 评论 0原文

以下代码抛出此异常:

“该进程无法访问文件 '\filename',因为它正在被另一个进程使用。”

很公平,但是关闭阅读器和/或 mmf 以便删除文件的正确方法是什么?我认为 MemoryMappedFile 会有一个 close() 方法或类似的方法,但事实并非如此。

任何帮助将不胜感激。谢谢。

mmf = MemoryMappedFile.CreateFromFile(filename,
      System.IO.FileMode.OpenOrCreate,
      "myMap" + fileNo.ToString(),
      fileSize);

reader = mmf.CreateViewAccessor(0, accessorSize);

<do stuff>

File.Delete(filename);

编辑:

看起来只有在析构函数中我才遇到这个问题。当在其他地方调用 dispose() 时,它工作正常,但是当我执行以下操作时,它会抛出异常。 Reader和mmf显然是该类的成员。输入构造函数后,文件访问是否会发生隐含的情况?

~Class()
{
    try
    {
        if (File.Exists(filename))
        {
            reader.Dispose();
            mmf.Dispose();
            File.Delete(filename);
        }
    }
    catch (Exception e)
    {
    }
}

The following code throws this exception:

"The process cannot access the file '\filename' because it is being used by another process."

Fair enough, but what's the proper way to close the reader and/or mmf so that the file can be deleted? I would think that MemoryMappedFile would have a close() method or something similar, but it doesn't.

Any help would be greatly appreciated. Thanks.

mmf = MemoryMappedFile.CreateFromFile(filename,
      System.IO.FileMode.OpenOrCreate,
      "myMap" + fileNo.ToString(),
      fileSize);

reader = mmf.CreateViewAccessor(0, accessorSize);

<do stuff>

File.Delete(filename);

EDITS:

It looks like it's only in the destructor that I'm having this problem. When dispose() is called elsewhere it works fine, but when I do the following it throws the exception. Reader and mmf are obviously members of the class. Is something implicit happening to the file access once the constructor is entered?

~Class()
{
    try
    {
        if (File.Exists(filename))
        {
            reader.Dispose();
            mmf.Dispose();
            File.Delete(filename);
        }
    }
    catch (Exception e)
    {
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2024-11-24 13:26:45

如果可能,您应该使用 using 构造:

using (var mmf = MemoryMappedFile.CreateFromFile(filename,
                   System.IO.FileMode.OpenOrCreate,
                   "myMap" + fileNo.ToString(), fileSize))
{
    using (reader = mmf.CreateViewAccessor(0, accessorSize))
    {  
       ... <do stuff> ...
    }
}

File.Delete(filename);

否则,请在 readermmf 对象上调用 Dispose() using 将确保在 中抛出异常时将其清除。

You should utilize the using construct if possible:

using (var mmf = MemoryMappedFile.CreateFromFile(filename,
                   System.IO.FileMode.OpenOrCreate,
                   "myMap" + fileNo.ToString(), fileSize))
{
    using (reader = mmf.CreateViewAccessor(0, accessorSize))
    {  
       ... <do stuff> ...
    }
}

File.Delete(filename);

Otherwise call Dispose() on the reader and mmf objects, however using will make sure that it is cleaned up in case exceptions are being thrown in <do stuff>.

錯遇了你 2024-11-24 13:26:45

在删除文件之前,您必须处理映射:

reader.Dispose();
mmf.Dispose();
File.Delete(filename);

Prior to deleting the file, you must dispose of the mapping:

reader.Dispose();
mmf.Dispose();
File.Delete(filename);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文