Results 1 to 3 of 3

Thread: Example: Check if a file is in use by another process

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Example: Check if a file is in use by another process

    I've noticed that people ask about this a lot on the net. I had my time struggling with it too and that's why I made this function.

    Code:
       Public Function IsFileInUse(sFile As String) As Boolean
            Try
                Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                End Using
            Catch Ex As Exception
                Return True
            End Try
            Return False
        End Function
    You should check to see if the file exists before calling this function.
    I know there are other and most likely better ways of doing this, but this works for me.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Example: Check if a file is in use by another process

    Quote Originally Posted by jumper77 View Post
    I've noticed that people ask about this a lot on the net. I had my time struggling with it too and that's why I made this function.

    Code:
       Public Function IsFileInUse(sFile As String) As Boolean
            Try
                Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                End Using
            Catch Ex As Exception
                Return True
            End Try
            Return False
        End Function
    You should check to see if the file exists before calling this function.
    I know there are other and most likely better ways of doing this, but this works for me.
    Should add the file exists to this function so it's self contained. Also note that this well return False if a file is marked as ReadOnly, you can add a check for that before trying to see if a file is in use and return arbitrarily return a True or False for ReadOnly files since your app wont be able to write to it anyways.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Example: Check if a file is in use by another process

    Hi, I apologize, I didn't see your reply until just now. Not sure how I missed it. You are correct about checking for file exists. And the check for "read-only" is a great idea and I did not think of that. Your post has made this function much better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width