删除运行时正在使用的文件 [英] Delete a file in use in runtime

查看:59
本文介绍了删除运行时正在使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除运行时某些进程正在使用/打开的文件.我正在将vb.net用于我的项目,并且图片显示在图片框中,并且应该删除该文件,而不关闭该文件.

How to delete a file which is in use/open by some process in runtime. I am using vb.net for my project and a image is shown in picturebox, and that should be deleted, without closing that file.

推荐答案

如果文件由另一个进程以独占模式打开,则不能-Windows不会允许您.在这种情况下,最好的办法是等待其他进程关闭文件然后将其删除,或者使用

If the file is opened by another process in exclusive mode, you can't -- Windows won't let you. In that case, the best you can do is to either wait for the other process to close the file and then delete it, or have it be deleted at the next reboot by using MoveFileEx() with the flag MOVEFILE_DELAY_UNTIL_REBOOT and a destination location of NULL.

如果通过另一个进程非专有地打开了文件,则可以仅调用 DeleteFile() 通常(假设您有权这样做).该文件将在另一个进程打开时保留,但是一旦另一个进程关闭,它将被删除.

If the file is opened non-exclusively by another process, you can just call DeleteFile() normally (assuming you have permission to do so). The file will remain while the other process has it open, but it will be deleted as soon as the other process closes it.

(是的,我知道这些链接是针对Win32 C API的;在VB .NET下应该可以使用相同的功能)

(And yes, I realize those links are for the Win32 C API; the same functions should be available under VB .NET)

这篇关于删除运行时正在使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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