File is being used by another process...

martin_d_bell

Newcomer
Joined
Oct 8, 2004
Hi all,

I am really struggling with this one!!

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:

Try
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
Try
ILFrames.Dispose()
LVBResults.Items.Clear()
System.IO.File.OpenWrite(strFileName)
System.IO.File.Delete(strFileName)


Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try

Else
Me.lblVerified.Text = "Sorry but the Document you are trying to delete does not exist"
End If

SqlDeleteCommand1.CommandText = "DELETE FROM tblListerDoc WHERE (ID = '" & strFileID & "')"

SqlDeleteCommand1.Connection = SqlCDocSearch

DsVerifyDoc1.tblListerDoc.Rows(0).Delete()

SqlDAVerifyDoc.Update(DsVerifyDoc1.tblListerDoc)
DsVerifyDoc1.tblListerDoc.Clear()
Else
End If

LVBResults.Items.Clear()
End If
Catch ex As Exception
MsgBox("SQL ERROR: Please try again")
End Try


Any help would be appreciated as I am tearing my hair out over this one.

Martin
 

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
You are opening the file for write access and then attempting to delete it - your code itself is locking the file. If you want to delete the file you do not need to open it.
 

martin_d_bell

Newcomer
Joined
Oct 8, 2004
The line of code that says

System.IO.File.OpenWrite(strFileName)

makes no difference. I have deleted it and it still gives the same error.

PlausiblyDamp said:
You are opening the file for write access and then attempting to delete it - your code itself is locking the file. If you want to delete the file you do not need to open it.
 

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
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.
 

OthelloNYC

Newcomer
Joined
Nov 12, 2008
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?
Thanks,
Stephen
 

Nate Bross

Contributor
Joined
Apr 6, 2005
Location
Chicago, IL
It would appear that whatever dll you are creating via Assembly.CreateInstance is not cleaning up after itself. Do you have any control over the dll you are using?
 

MrPaul

Contributor
Joined
Jun 28, 2006
Location
Hampshire, UK
Assemblies cannot be unloaded

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.

Good luck :cool:
 

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
IIRC once an assembly is loaded into an application domain it cannot be unloaded without unloading the entire domain, this might be the reason the file is still locked...

Probably the easiest fix is to load the dll into it's own AppDomain and unload that when you no longer require the dll.
 

Maninder Kaur

Newcomer
Joined
Nov 20, 2008
As u r opening the file before deleting the same file so as u open the file for writting then obviously it is used by the process ,so just delete that line

I m using following code for deleting the file and it is working very fine

string fileName = textBox1.Text.Trim();
try
{
if (File.Exists(fileName))
{
//File.OpenWrite(fileName);
File.Delete(fileName);
MessageBox.Show("File Deleted Successfully");
}
else
{
MessageBox.Show("File does not exists");
}
}
catch (FileNotFoundException ex)
{
MessageBox.Show(ex.Message.ToString());
}
 
Top Bottom