如何删除在另一个程序VB.NET中打开的文件 [英] How to delete a file thats open in another program VB.NET

查看:101
本文介绍了如何删除在另一个程序VB.NET中打开的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个反病毒程序,它有实时保护功能,当它检测到病毒弹出一个对话框,并在标签中显示病毒文件路径,我想删除该文件但是当我尝试使用该文件时.delete方法,但我收到此消息:此文件在另一个程序中打开(它说它在我的程序中打开)并且无法删除。任何人都可以帮帮我吗?



我尝试过:



Ive尝试了所有但是所有方法都不起作用可以sombody请帮助我谢谢

解决方案

有办法强迫这个,但这是一个坏主意 - 你可能会导致更多通过强制用户知道并手动关闭应用程序来强制手柄过早关闭造成的损坏。



但是......你说它说它开放在我的程序中这意味着你已经打开了文件 - 这是有意义的,因为你正在扫描它 - 并且尚未关闭并使用交换机处理你正在阅读文件的流。在这种情况下,我在第一段中所做的评论生效 - 您将关闭您自己的应用程序正在使用的内容,并且您的应用程序很可能会因强制删除而崩溃。 />
相反,请检查您的代码,并确保在尝试删除它们之前,所有流,文件句柄或用于访问文件的任何内容都已正确处理。

  Dim  isSuspect  As  布尔 = 错误 
使用 fs As FileStream(path,FileAccess.Read)
isSuspect = ScanMyFile(fs)
结束 使用
如果 isSuspect 然后
' 您可以在此删除文件
...

结束 如果


Im trying to make an anti virus program and it has real time protection when it detects a virus a dialog pops up and in a label it shows the virus file path and i want to delete the file but when i try using the file.delete method but i get this message: this file is open in another program (its saying its open in my program) and cannot be deleted. can anyone help me out?

What I have tried:

Ive Tried Everything but all methods are not working can sombody please help me thanks

解决方案

There are ways to force this, but it's a bad idea - you will probably cause more damage by forcing the handle to close prematurely than you will save by lettign the user know and close the application manually.

But ... you say "its saying its open in my program" which implies you have opened the file - which makes sense since you are scanning it - and have not yet closed and disposed your stream with switch you are reading the file. In which case, the comment I made in the first paragraph comes into effect - you will be closing somthing your own application is using, and there is a very good chance that your app will crash as a result of your forcing the delete.
Instead, check your code, and make sure that all streams, file handles or whatever you are using to access files are correctly Disposed before you try to delete them.

Dim isSuspect As Boolean = False
Using fs As New FileStream(path, FileAccess.Read)
	isSuspect = ScanMyFile(fs)
End Using
If isSuspect Then
	' You can delete the file here
	...

End If


这篇关于如何删除在另一个程序VB.NET中打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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