windows 删除运行时正在使用的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/301174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete a file in use in runtime
提问by PhiLho
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.
如何删除运行时某个进程正在使用/打开的文件。我在我的项目中使用 vb.net,图片显示在图片框中,应该删除,而不关闭该文件。
回答by Adam Rosenfield
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
.
如果文件由另一个进程以独占模式打开,则不能 - Windows 不会让您打开。在这种情况下,您能做的最好的事情是等待另一个进程关闭文件然后将其删除,或者在下次重新启动时使用MoveFileEx()
标志MOVEFILE_DELAY_UNTIL_REBOOT
和目标位置NULL
.
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.
如果文件被另一个进程以非独占方式打开,您可以DeleteFile()
正常调用(假设您有这样做的权限)。该文件将在其他进程打开时保留,但一旦其他进程关闭它,它将被删除。
(And yes, I realize those links are for the Win32 C API; the same functions should be available under VB .NET)
(是的,我意识到这些链接是针对 Win32 C API 的;在 VB .NET 下应该可以使用相同的功能)
回答by PhiLho
See delete locked files, Overcoming “It is being used by another person or program.”among others.
请参阅删除锁定的文件,克服“其他人或程序正在使用它”。其中。
回答by SomeDude
You can close the proccess down, then delete it, or delete on reboot. Use the above suggestions for information on using MoveFileEx() with Windows.
您可以关闭该进程,然后将其删除,或在重新启动时删除。使用上述建议获取有关在 Windows 中使用 MoveFileEx() 的信息。
To close down a process you can do the following:
要关闭进程,您可以执行以下操作:
For Each proc As Process In System.Diagnostics.Process.GetProcessesByName("process name here")
proc.Kill()
Next
回答by David Dibben
I don't think that it is possible. On windows, you cannot delete a file which has open handles. http://support.microsoft.com/kb/320081
我不认为这是可能的。在 Windows 上,您不能删除具有打开句柄的文件。http://support.microsoft.com/kb/320081
However, from your description you don't need to keep the file open in your application. Open the file, read the image then close the file. Then the file can be deleted but the application will still have the image data.
但是,根据您的描述,您不需要在应用程序中保持文件处于打开状态。打开文件,读取图像,然后关闭文件。然后可以删除文件,但应用程序仍将保留图像数据。