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

  • SEARCH
  • Home
  • 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 6387563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:10:02+00:00 2026-05-25T03:10:02+00:00

To avoid exceptions like (1) Process cannot access the file because it was used

  • 0

To avoid exceptions like

(1) Process cannot access the file because it was used by another process

I used the following method to test the accessibility of the file before any further processing.

private bool CheckIfFileBeingUsed(string FilePath)
    {
        FileStream Fs = null;
        try
        {
            Fs = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.None);
            Fs.Close();
        }
        catch (Exception)
        {
            return true; //Error File is being used
        }
        return false; //File is not being used.
    }

Could anyone advise me there’s any Windows API or other solutions for such testing of file accessibility instead of the above File.Open Method ?

  • 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-05-25T03:10:03+00:00Added an answer on May 25, 2026 at 3:10 am

    It seems like you’re duplicating effort here. Since trying to open the file for processing will throw the same exception you’re catching here, why not just proceed with an attempt at processing the file, and handle the exception if it comes up?

    Assuming that’s not possible for some reason, testing like this still isn’t totally reliable, since you now have a race condition. Even if this test passes, there’s no guarantee that the file will be in the same state when you go to run your other code on the file. That’s why it’s easier to just attempt to do what you want, and handle any exceptions at that point.

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

Sidebar

Related Questions

In an effort to avoid potentially unexpected exceptions from a method (in the interest
I always try to avoid to return string literals, because I fear they aren't
I'm trying to avoid code like this when reusing the same ViewUserControl in ASP.NET
In the pursuit of elegant coding, I'd like to avoid having to catch an
I'm used to the Delphi VCL Framework, where TStreams throw exceptions on errors (e.g
Trying to avoid the FxCop warning 'Do not raise reserved exceptions' from a C++/CLI
Elmah recovers from routine exceptions (like the DB not configured for Elmah) quite well
My program basically looks like this: using(XmlReader reader = XmlReader.Create(File.Open(path, FileMode.Open, FileAccess.Reader, FileShare.Read)) {
I begin by adding data to table events , field exceptions like this function
I have seen the trend that many people don't like to use exceptions in

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.