Solved

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

Posted on 2016-10-04
1
69 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:
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now