Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
467 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:
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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