File.Delete()方法删除文件时出错 [英] error in deleting a file with File.Delete() method

查看:153
本文介绍了File.Delete()方法删除文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Visual Studio 2010中编写了一个winform应用程序。在其中一种形式中,用户可以浏览本地系统并选择图像,因此应用程序会将图像(使用File.Copy()方法)复制到其文件夹中。 。
的问题是当用户想要删除该图像时(File.Delete()方法),我收到这样的错误:

I have written a winform application in visual studio 2010.In one of the forms the user can browse the local system and select an image, so the application will copy that image(with File.Copy() method) to its folder. the problem is when the user wants to delete that image(File.Delete() method),I receive an error like this :


无法删除此文件,因为它已被另一个进程使用。

cannot delete this file because it is used by another process.

我不知道该错误说明了什么,因为我没有在其他进程中使用图像文件。

I do not know what this error says because i do not use the image files in other processes.

推荐答案


由于该文件已被另一个进程使用,因此无法删除。

cannot delete this file because it is used by another process.

该消息对程序员不是很有帮助,因为当您在开发代码时发生该消息时,其他过程几乎总是您的 过程

The message isn't terribly helpful to programmers because when it happens when you develop code, that other process is almost always your process.

这很可能在图像文件中发生,从图像文件创建Image或Bitmap对象会在文件上放置 lock 。之所以创建该锁,是因为GDI +在文件内容上创建了内存映射视图,这是一种强大的优化功能,可将位图数据保留在页面文件之外。大图像非常重要,它们可以包含许多兆字节的像素数据。

This is very likely to occur with image files, creating a Image or Bitmap object from an image file puts a lock on the file. The lock is created because GDI+ creates a memory-mapped view on the file content, a strong optimization that keeps the bitmap data out of the paging file. Matters a great deal on large images, they can contain many megabytes worth of pixel data.

在您在代码中显式调用其Dispose()方法之前,将一直保持该锁定。因此,请确保已完成操作,然后再尝试将图像保存回去。在极少数情况下,您可能需要创建图像的副本以处理原始图像,请使用带有Image参数的Bitmap()构造函数重载。

That lock is kept until you explicitly call its Dispose() method in your code. So be sure that was done before you try to save the image back. In rare cases you may need to create a copy of the image to allow you to dispose the original, use the Bitmap() constructor overload that takes an Image argument.

这篇关于File.Delete()方法删除文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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