Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have a project that create a pdf and send it via email,

at first, i delete all the pdf's and then create the needed one

i get all the file in a cetain path, check if the file name is one of the pdf wanted to be deleted, and then delete it

C#
string[] filePaths = Directory.GetFiles(Application.StartupPath);
foreach (string filename in filePaths)
{
    if (filename.Contains("FILE NAME") )
    {
          try
          {
              File.Delete(filename);
          }
          catch (SystemException ex)
          {
              exception.SendExceptionEMail(ex.ToString());
          }
     }
}



it some times generates an exception

The process cannot access the file "File Path" because it is being used by another process.

i want to know how to close the file if this exception produced to be able to delete it
Posted
Updated 6-May-12 5:31am
v4

1 solution

You cannot, unless you terminate the process which keeps this file open.

First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In same cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>


This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,
—SA
 
Share this answer
 
v2
Comments
VJ Reddy 6-May-12 11:38am    
Good answer and good recommendation regarding Sysinternals Suite. 5!
Sergey Alexandrovich Kryukov 6-May-12 12:43pm    
Thank you, VJ.
--SA
Espen Harlinn 7-May-12 6:02am    
Good reply :-D
Sergey Alexandrovich Kryukov 7-May-12 12:23pm    
Thank you, Espen.
--SA
bakinam Ahmed 21-May-12 3:28am    
really thank you for your great answer, I handled it, thanks alot

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