Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:32:23+00:00 2026-06-13T20:32:23+00:00

While calling File.Delete(file_path) for a file which is opened in another process, the attempt

  • 0

While calling File.Delete(file_path) for a file which is opened in another process, the attempt fails with cannot access error, but on exiting the app (from where the attempt was made) the file gets deleted automatically. After File.Delete attempt, the path gets inaccessible for any other operation.

I need to either fail on delete and let the path be accessible through the app or delete the file completely, not on app exit.

Well my code is this :

 private bool DeleteFilesAsync(string FileToDelete)
 {
    try
    {
            //Set file's attribute to normal if it is ReadOnly file
            File.SetAttributes(FileToDelete, FileAttributes.Normal);

            File.Delete(FileToDelete);
           //Some bussiness logic to update file's status in database

        return true;
    }
    catch (Exception ex)
    {
        //log the error
         return false;
    }
}

Something wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:32:23+00:00Added an answer on June 13, 2026 at 8:32 pm

    Windows does have a delete-on-last-close feature. All deletes are handled that way. The only reason that this almost never occurs in practice is because most apps do not open the file with FILE_SHARE_DELETE so it is not possible to delete a used file. But at the Kernel level all deletes are delete-on-close.

    There might be a way to clear the delete flag by calling NtSetInformationFile to set the FileDispositionInfo class. This assumes that it is possible to clear the delete flag. It might well be.

    Anyway, a more sane approach would be to open the file before deleting it to ensure that exclusive access is available:

    using(new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) { }
    

    After this line passes without exception, we know that the file was unused at the point of opening it. Of course, it might be opened by someone else immediately after this line, but maybe this solution is enough for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting this error while calling $.ajax methods Error: [Exception... Access to restricted
while calling readlines() on a .srt file , I got a list of characters
I am calling a .php file that lies within my 'uploads' directory to delete
While calling from my Web Application, WCF response i am getting is : <s:Envelope
I am observing a crash while calling the CMemFile::Detach() function The exception with the
It seems that calling a P-Invoke while the App is not completed loaded make
While following path for wait system call, noticed that before calling do_wait_thread we get
I have two questions: While editing a source file, it's very convenient to be
While trying to add an error message using add_to_base, I am getting an undefined
I am using jax-ws webservice hosted at weblogic 10.3, while calling the webservice i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.