Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 11-Aug-17 4:56am
Comments
F-ES Sitecore 11-Aug-17 9:15am    
Release references to the file in your code before trying to delete it. You haven't posted the code so I'm not sure how you think we can help with code we can't see, but at a guess you're not called "Dispose" on whatever object referenced the file.
Afzaal Ahmad Zeeshan 11-Aug-17 9:20am    
You cannot. That program first must release the file, then other processes can access it. That is how Windows works.
Brandon Williams 11-Aug-17 9:53am    
how can i get my program to release it? i used a open file dialog if thats any help. Thanks in advance.
Afzaal Ahmad Zeeshan 11-Aug-17 11:28am    
If it is your program, then use any of the Close or Flush or Dispose functions, to properly release it, then delete it.

If it is another program, then you don't have any chances.

1 solution

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.
VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900