?
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
?
525 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.
1 Comment
 
LVL 11

Accepted Solution

by:
Craig Yellick 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 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