-
Jul 9th, 2016, 09:27 PM
#1
Thread Starter
PowerPoster
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.
-
Jul 11th, 2016, 11:52 AM
#2
Re: Example: Check if a file is in use by another process
Originally Posted by jumper77
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.
-
Aug 20th, 2016, 06:21 AM
#3
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|