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.
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.
makes no difference. I have deleted it and it still gives the same error.
Quote:
Originally Posted by PlausiblyDamp
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.
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?
Thanks,
Stephen
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?
__________________ ~Nate™
___________________________________________
Please use the [vb]/[cs] tags on posted code.
Please post solutions you find somewhere else.
Follow me on Twitter here.
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
__________________
Never trouble another for what you can do for yourself.
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.
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());
}
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET. subscribe