Solved

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

Posted on 2016-10-04
1
318 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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 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