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 273761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:30:22+00:00 2026-05-12T00:30:22+00:00

I am doing an archive service for logs. Since some files are open and

  • 0

I am doing an archive service for logs. Since some files are open and in use I can’t archive them until they are not in use anymore. The only way I can figure out how to see if the file is open by another process is the code below.

I hate the code there must be a better way. Is there a better way?

try
{
    using (FileStream stream = File.Open("SomeFile.txt", FileMode.Open, FileAccess.ReadWrite))
    {
        // Do nothing just seeing if I could open it.
        return true;
    }
}
catch (IOException)
{
    // Exception so File is in use and can't open
    return false;
}

EDIT: Please note, that I do not want to catch the exception. I would rather avoid the exception entirely. I would rather have some magic function for example IsFileInUser(string fileName) that would return a boolean WITHOUT catching exceptions underneath.

A way I have see is using OpenFile of PInvoke.

  • 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-12T00:30:22+00:00Added an answer on May 12, 2026 at 12:30 am

    Here is what I came up with.

    First here is the bad way of doing it by exception.

    public static bool IsFileInUseBadMethod(string filePath)
    {
        if (!File.Exists(filePath))
        {
            return false;
        }
    
        try
        {
            using (StreamReader reader = new StreamReader(File.Open(filePath, FileMode.Open, FileAccess.ReadWrite)))
            {
                return false;
            }
        }
        catch (IOException)
        {
            return true;
        }
    }
    

    On my machine this method took about 10 million ticks when the file was in use.

    The other way is using the kernel32.dll

    // Need to declare the function from the kernel32
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName, System.UInt32 dwDesiredAccess, System.UInt32 dwShareMode, IntPtr pSecurityAttributes, System.UInt32 dwCreationDisposition, System.UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);
    
    private static readonly uint GENERIC_WRITE = 0x40000000;
    private static readonly uint OPEN_EXISTING = 3;
    

    Here is the function

    public static bool IsFileInUse(string filePath)
    {
        if (!File.Exists(filePath))
        {
            return false;
        }
    
        SafeHandle handleValue = null;
    
        try
        {
            handleValue = FileHelper.CreateFile(filePath, FileHelper.GENERIC_WRITE, 0, IntPtr.Zero, FileHelper.OPEN_EXISTING, 0, IntPtr.Zero);
    
            bool inUse = handleValue.IsInvalid;
    
            return inUse;
        }
        finally
        {
            if (handleValue != null)
            {
                handleValue.Close();
    
                handleValue.Dispose();
    
                handleValue = null;
            }
        }
    }
    

    This ran in about 1.8 million ticks. Over an 8 million ticks difference from the exception method.

    However when file it not in use meaning no exception is thrown the speed of the “BAD” method is 1.3 million and the kernel32.dll is 2 million. About an 800k ticks better. So using the Exception method would be better if I knew the file would be not be in use about 1/10th of the time. However the one time it is, it is an extremely expensive ticks operation compared to the kernel32.dll.

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

Sidebar

Related Questions

In my main script, I am doing some archive manipulation. Once I have completed
How can I turn multiple archive files into one big archive file on Linux
after doing some research i'm still not sure if using mysql with .net desktop
I need to put several folders into artifact archive. Now I am doing it
Doing some jquery animation. I have certain divs set up with an attribute of
Doing some homework here (second assignment, still extremely green...). The object is to read
currently, I'm doing an update similar to as follows, because I can't see a
Some newbie questions. First, I would love to get this plugin: http://archive.plugins.jquery.com/project/getAttributes but I
I'm compiling some shared objects file into an archive.a : $ g++ -c -Iinclude/
I have a file that contain list of files I want to archive with

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.