Click here to Skip to main content
12,622,517 members (31,556 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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 31-Jul-11 23:45pm
Updated 31-Jul-11 23:52pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:

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
 
                   }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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)
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161128.1 | Last Updated 1 Aug 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100