I need to let users delete a bitmap file from a location on a server and also delete a record relating to this bitmap from a database at the same time. Every time I click on the delete button it throws an exception message saying "The process cannot access the file "\\server\blah\blah\myfile.bmp" because it is being used by another process"
I think I need to close the process somehow but have no clue how to do this.
My code is shown below:
If MessageBox.Show("Are you sure you want to Delete this document?" & vbLf & vbLf & "NOTE: If you click Yes you will not be able to retrieve this Document.", "Delete Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
strFileID = DsVerifyDoc1.tblListerDoc.Rows(0).Item(0)
If docVerified = 1 Then
If System.IO.File.Exists(strFileName) = True Then
Catch ex As Exception
Me.lblVerified.Text = "Sorry but the Document you are trying to delete does not exist"
SqlDeleteCommand1.CommandText = "DELETE FROM tblListerDoc WHERE (ID = '" & strFileID & "')"
SqlDeleteCommand1.Connection = SqlCDocSearch
Catch ex As Exception
MsgBox("SQL ERROR: Please try again")
Any help would be appreciated as I am tearing my hair out over this one.
If you are running your app can you manually delete the file via explorer? You may want to put a breakpoint on the System.IO.File.Delete(strFileName) line and when it is reached just fire up explorer and try to delete the file - if you still can't then the error is elsewhere (are you opening this file anywhere else in code?) or somebody is using the file.
I have a similar problem, but need a definite .net solution. I am loading a DLL with Assembly.LoadFrom("liblocation") then creating an instance of an object with Assembly.CreateInstance("libobjectname")
I then run the assembly against a transaction folder.
when I am done I set the object to nothing, then I dispose and set the assembly to nothing.
As part of the routine, I've been trying to clear out the transaction folder. Despite having set the object and the assembly to nothing, I still get one file being held on as locked by my current process. Is there some way to force an unlock if you are the owner of the locking process?
Once an assembly is loaded into an application domain it cannot be unloaded. If you need the ability to unload an assembly, load it into a separate application domain, and when the application domain ends the assembly will be unloaded from the process.
Never trouble another for what you can do for yourself.