Click here to Skip to main content
15,916,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam Working upon email functionality for reports.I have nearly 10 reports in my project. I have a folder with name 'reports' in my project.When ever user sends the mail from my web page with attachements(in pdf format), automatically that particular attachment also should be saved within the 'reports' folder.It's fine working.But whenever user trying to send the samereport again,I would like to delete the earlier one that present within 'reports' folder and the current report should be placed there.

but it's showing error like below..

The process cannot access the file 'E:\Pradeep\Current Projects\SprintSafety\reports\Zachry-ConsumableReport.pdf' because it is being used by another process.

my code as follows for delete operation..
File.Delete(Server.MapPath("reports") + "\\" + ASPxDropDownEdit1.SelectedItem.Text + "-ConsumableReport.pdf"); 

any idea?

thank u!
A Pradeep
Posted
Updated 31-Jul-11 22:52pm
v2

You have got this error, you cannot delete a file which is used by some another process. To avoid this issue, whenever you are doing something with this file, Close the file handle.
There is a very good link on the same issue

have a look
 
Share this answer
 
the error says it all. Someone still has the file opened.

before you delete you have to check if you can open the file exclusivly.

your code could be:

C#
try
                   {
                       // check if the file can be opened exclusive. If that is possible, then the file is found otherwise in transfer
                       using (stream = File.Open(Server.MapPath("reports") + "\\" + ASPxDropDownEdit1.SelectedItem.Text + "-ConsumableReport.pdf", FileMode.Open, FileAccess.Write, FileShare.None))
                       {
                           if (stream.CanWrite)
                               File.Delete(Server.MapPath("reports") + "\\" + ASPxDropDownEdit1.SelectedItem.Text + "-ConsumableReport.pdf");

                       }
                   }
                   catch (Exception err)
                   {
// file cannot be opened exclusively so it cannot be deleted

                   }
 
Share this answer
 
You can find out which process is holding a file handle and kill this process by System.Diagnostics.Process.Kill. Not recommended. Better try to fix offending process, if you have an access to its source code.

—SA
 
Share this answer
 
A good tool for finding out what is locking files in Sysinternals Process Monitor[^]

This will let you put in the filename (e.g. ConsumableReport.pdf) and it will tell you which processes have a file lock on the object. It may be you've forgotten to close a handle from some other process, it will let you pin down exactly where the issue lies.

Obviously, you can only run this on a machine you have access to\rights on (e.g, no good for hosted environment)
 
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