?
Solved

The process cannot access the file because it is being used by another process. ASP.NET C#

Posted on 2016-10-04
1
Medium Priority
?
379 Views
Last Modified: 2016-10-04
We have a form where for first time when you login check if a file exist and delete like this:

string check = "file1.pdf";
        if (System.IO.File.Exists(check))
        {
            
            System.IO.File.Delete(check);
        }

Open in new window


We need to delete that file always for the purposes of the form but is very clear if processes are attached to that file of other instances or whatever then ASP.NET say this message: "The process cannot access the file because it is being used by another process."
Of course we now that with try and catch we can catch the exception and send the message but is not what we want we want FORCE DELETE the file no matter if used or not. How can we force to delete the file?  Sometimes inside the server could be users that are not closed properly and will never free the file that is the reason to kill the file literally.

Thank you
0
Comment
Question by:Alex E.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
CraigYellick earned 2000 total points
ID: 41828113
Does the file absolutely need to be named file1.pdf (or whatever) and if so, does it absolutely need to be stored in the exact same location? You can force-delete files but that almost always causes trouble elsewhere.

You could save the file to a file name that does not exist (because you test for that), and after creating it, re-name and move to some other location where the "stuck" file is not.  Another option is to detect the locked file and change the name slightly so that it's obvious it's the same file but indexed, like file1-1.pdf, file1-2.pdf.

Finally, one more solution is to mark the file you create a READ-ONLY so nobody can get a lock on it. They'd have to copy the file elsewhere to open it with a lock.

If you really-really want to kill that file, google for "force delete tool". There used to be one named ForceDelete.exe but I could not find it. There are others out there.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question