Click here to Skip to main content
16,019,055 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am writing code with reads image files and displays them for a defined period. After the period has passed I want to delete the file from its directory. However, the program, which does not close, still has the handle to the file open and blocks attempts to delete the file directly. How can I close the handle programatically to allow deletion of the file?
Posted
Updated 13-Apr-12 5:21am
v2
Comments
wizardzz 13-Apr-12 14:15pm    
Nice question.

It depends on how you opened the file. Most likely, you forgot to dispose something. The disposal mechanism usually takes care of certain things you need to clean up in a controlled manner (in contrast to, say, Garbage Collector functionality), in some point of execution of your code; and some typical uses of it are cleaning up unmanaged resources and — what a surprise! — closing of file handles.

Basically, you need to watch yourself of using of any types implementing System.IDisposable and call System.IDisposable.Dispose in the appropriate points. Please see:
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[^].

It's important to makes sure this call is done even if exception is thrown, via a try-finally statement. The best fool-proof way of doing it is using the using statement (don't mix it up with using clause!). Please see:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

Let's consider a simple example. When you open a file with System.IO.StreamReader, you do it in the exclusive-use mode (which is the best thing to do anyway). You won't be able to read the same file in parallel until the instance of the reader is disposed:

C#
// you can access the file fileName before the next line
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName)) {
    // from this moment, file access is blocked; you can only read it with this reader
    string line = reader.ReadLine();
    //...
} //reader.Dispose is called here automatically
// and here you can access the file


You could have used different ways of accessing your files, you could get read, write, read/write access, work on lower levels of file or stream interfaces, etc., but the principle is the same.

[EDIT]

By the way, when you need an information of a process holding your file, you can use the Sysinternal's utility "handle.exe". Please see my recent answer for further detail:
Pause the use of a file by a process?[^].

—SA
 
Share this answer
 
v8
Comments
ProEnggSoft 13-Apr-12 13:29pm    
Nice answer. 5!
Sergey Alexandrovich Kryukov 13-Apr-12 13:46pm    
Thank you.
--SA
Bill Eckard 13-Apr-12 15:39pm    
Thanks... Great answer
Sergey Alexandrovich Kryukov 13-Apr-12 17:47pm    
You are very welcome. Hope you will be able to find the solution.
Good luck, call again.
--SA
VJ Reddy 17-Apr-12 8:07am    
Good answer. +5
It depends on how you read the file in the first place.
Some image loading operations lock the file for the lifetime of the Image object

There is a description of this, and a workaround on MSDN: http://support.microsoft.com/?id=814675[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900