Directory / File IO / Registry Topics include: IO, file system and the registry

Go Back  Xtreme .NET Talk > .NET > Directory / File IO / Registry > File is being used by another process...


Reply
 
Thread Tools Display Modes
  #1  
Old 11-24-2004, 11:01 AM
martin_d_bell martin_d_bell is offline
Newcomer
 
Join Date: Oct 2004
Posts: 17
martin_d_bell is on a distinguished road
Default File is being used by another process...



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
Reply With Quote
  #2  
Old 11-24-2004, 11:22 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Sep 2002
Location: Lancashire, UK
Posts: 6,463
PlausiblyDamp is on a distinguished road
Default

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.
__________________
Posting Guidelines FAQ Post Formatting

Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein
Reply With Quote
  #3  
Old 11-25-2004, 02:17 AM
martin_d_bell martin_d_bell is offline
Newcomer
 
Join Date: Oct 2004
Posts: 17
martin_d_bell is on a distinguished road
Default

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.

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.
Reply With Quote
  #4  
Old 11-25-2004, 04:42 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Sep 2002
Location: Lancashire, UK
Posts: 6,463
PlausiblyDamp is on a distinguished road
Default

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.
__________________
Posting Guidelines FAQ Post Formatting

Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein
Reply With Quote
  #5  
Old 11-12-2008, 07:32 PM
OthelloNYC OthelloNYC is offline
Newcomer
 
Join Date: Nov 2008
Posts: 1
OthelloNYC is on a distinguished road
Default Re: File is being used by another process...

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
Reply With Quote
  #6  
Old 11-13-2008, 12:31 PM
Nate Bross Nate Bross is offline
Contributor

Preferred language:
CSharp
 
Join Date: Apr 2005
Location: Chicago, IL
Posts: 601
Nate Bross is on a distinguished road
Default Re: File is being used by another process...

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.
Reply With Quote
  #7  
Old 11-13-2008, 02:06 PM
MrPaul's Avatar
MrPaul MrPaul is offline
Contributor

Preferred language:
C#
 
Join Date: Jun 2006
Location: Hampshire, UK
Posts: 428
MrPaul is on a distinguished road
Exclamation 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
__________________
Never trouble another for what you can do for yourself.
Reply With Quote
  #8  
Old 11-13-2008, 02:39 PM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Sep 2002
Location: Lancashire, UK
Posts: 6,463
PlausiblyDamp is on a distinguished road
Default Re: File is being used by another process...

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.
__________________
Posting Guidelines FAQ Post Formatting

Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein
Reply With Quote
  #9  
Old 11-20-2008, 03:37 AM
Maninder Kaur Maninder Kaur is offline
Newcomer
 
Join Date: Nov 2008
Posts: 2
Maninder Kaur is on a distinguished road
Thumbs up Re: File is being used by another process...

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());
}
Reply With Quote
  #10  
Old 02-22-2012, 11:49 AM
JoshuaReen JoshuaReen is offline
Newcomer
 
Join Date: Feb 2012
Posts: 1
JoshuaReen is on a distinguished road
Default Re: File is being used by another process...

Just try a useful tool that is "Long Path Tool". It can solve this type of errors very easily and quickly.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Download file via Browser: used by another process session101 ASP.NET 3 12-18-2008 04:00 AM
Process Kill lothos12345 General 3 10-30-2006 03:55 PM
The process cannot access the file because it is being used by another process... microkarl Directory / File IO / Registry 2 02-28-2006 06:11 AM
About Process.Start see07 ASP.NET 1 11-05-2004 03:24 AM
What is process used by? bri189a General 3 08-20-2004 11:06 PM

Advertisement:







Free Publications
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
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe