martin_d_bell Posted November 24, 2004 Posted November 24, 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 Quote
Administrators PlausiblyDamp Posted November 24, 2004 Administrators Posted November 24, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
martin_d_bell Posted November 25, 2004 Author Posted November 25, 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. 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. Quote
Administrators PlausiblyDamp Posted November 25, 2004 Administrators Posted November 25, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
OthelloNYC Posted November 13, 2008 Posted November 13, 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 Quote
Nate Bross Posted November 13, 2008 Posted November 13, 2008 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? Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
MrPaul Posted November 13, 2008 Posted November 13, 2008 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: Quote Never trouble another for what you can do for yourself.
Administrators PlausiblyDamp Posted November 13, 2008 Administrators Posted November 13, 2008 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Maninder Kaur Posted November 20, 2008 Posted November 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()); } Quote
JoshuaReen Posted February 22, 2012 Posted February 22, 2012 Just try a useful tool that is "Long Path Tool". It can solve this type of errors very easily and quickly. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.