[VB.net] - File Locked = Good Code? | Member | | Join Date: Sep 2008
Posts: 74
| | -
Shared Function FileIsLocked(ByVal fileFullPathName As String) As Boolean
-
Dim isLocked As Boolean = False
-
Dim fileObj As System.IO.FileStream
-
-
Try
-
fileObj = New System.IO.FileStream(_
-
fileFullPathName, _
-
System.IO.FileMode.Open, _
-
System.IO.FileAccess.ReadWrite, _
-
System.IO.FileShare.None)
-
Catch
-
isLocked = True
-
Finally
-
If fileObj IsNot Nothing Then
-
fileObj.Close()
-
End If
-
End Try
-
Return isLocked
-
End Function
-
I found the above code on a forum, i was wondering if its the right and best method to do this.
i want to see if a file is locked
|  | Moderator | | Join Date: Dec 2006
Posts: 4,745
| | | re: [VB.net] - File Locked = Good Code?
I had to read your post twice, to see whether it was a joke or my calendar to see whether it was April. You seriously want us to review your code you found on the internet? Suggest review and test it yourself.
I notice many of your posts are of similar quality. Please review the Posting Guidelines and improve your posts. Thanks.
MODERATOR
|  | Site Addict | | Join Date: Jul 2008 Location: US of A
Posts: 587
| | | re: [VB.net] - File Locked = Good Code? Quote:
Originally Posted by zubair1
'code...
I found the above code on a forum, i was wondering if its the right and best method to do this.
i want to see if a file is locked
well, for one, if you found it yourself, how about putting it into action and testing it, what can we assist in having you try a code ?
two, im sure there are many ways of finding or testing to see if a file is locked, its just a matter of thinking of a decent code, it should not be that hard. just try and use different methods in the system.io namespace....
joedeene
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: [VB.net] - File Locked = Good Code? Quote:
Originally Posted by joedeene well, for one, if you found it yourself, how about putting it into action and testing it, what can we assist in having you try a code ?
two, im sure there are many ways of finding or testing to see if a file is locked, its just a matter of thinking of a decent code, it should not be that hard. just try and use different methods in the system.io namespace....
joedeene thanks
i wasn't asking to test it, i was asking if you think this code is right = correct
i already tested the code - it works.
but i'm not sure if its the best method - i dont think its that stable either i've researched about it a little and saw people saying the best way is to Open a file and see it can be written then go further and on .. and throw exception stuff.
JoeDeene - do you know of a good method on how to do this - i simply just want to see if a certain file is locked or not.
Thanks for the feedback
|  | Site Addict | | Join Date: Jul 2008 Location: US of A
Posts: 587
| | | re: [VB.net] - File Locked = Good Code? Quote:
Originally Posted by zubair1 thanks
i wasn't asking to test it, i was asking if you think this code is right = correct
i already tested the code - it works.
but i'm not sure if its the best method - i dont think its that stable either i've researched about it a little and saw people saying the best way is to Open a file and see it can be written then go further and on .. and throw exception stuff.
JoeDeene - do you know of a good method on how to do this - i simply just want to see if a certain file is locked or not.
Thanks for the feedback
well what do u mean locked? like in use by another program?
and try taking a look over on a simliar thread here... http://bytes.com/forum/thread509282.html
joedeene
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: [VB.net] - File Locked = Good Code? Quote:
Originally Posted by joedeene well what do u mean locked? like in use by another program?
and try taking a look over on a simliar thread here... http://bytes.com/forum/thread509282.html
joedeene Thanks for link
but theres no solution there :(
could you provide a working example please :(
Thank you
Regards
|  | Site Addict | | Join Date: Jul 2008 Location: US of A
Posts: 587
| | | re: [VB.net] - File Locked = Good Code? Quote:
Originally Posted by zubair1 Thanks for link
but theres no solution there :(
could you provide a working example please :(
Thank you
Regards well, could u kindly answer my other question? what do u mean by locked? like if its not accessible? or encrypted?
joedeene
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: [VB.net] - File Locked = Good Code?
Hi,
By locked i mean - the file is in use either by another application or windows itself.
To make it more clear - i have a program which is clearing the cache and i am looping through all the files in many different folders including TEMP, cache, cookies, etc, etc.
I want to check and see a file is locked / in use by another program.
Hope that makes it more clearer :)
Thanks in advance
Regards
|  | Site Addict | | Join Date: Jul 2008 Location: US of A
Posts: 587
| | | re: [VB.net] - File Locked = Good Code? Quote:
Originally Posted by zubair1 Hi,
By locked i mean - the file is in use either by another application or windows itself.
To make it more clear - i have a program which is clearing the cache and i am looping through all the files in many different folders including TEMP, cache, cookies, etc, etc.
I want to check and see a file is locked / in use by another program.
Hope that makes it more clearer :)
Thanks in advance
Regards heres a simple code...it works but im sure there are better ways but this works with full file permissions as a user.... - Private Sub btnCheckFileLocked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckFileLocked.Click
-
checkiffilesarelocked(My.Computer.FileSystem.SpecialDirectories.Desktop)
-
End Sub
-
-
Private Sub checkiffilesarelocked(ByVal dir As String)
-
For Each file In IO.Directory.GetFiles(dir)
-
Dim myfile As IO.FileStream
-
Try
-
myfile = New IO.FileStream(file, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None)
-
-
myfile.Close()
-
-
'basically it makes a stream for each file in the directory with full access rights and when it catches an exception, mainly an ioexception because it is being used by another process, it will add it to a listbox and say not accessible...there are other methods, like just adding it to an array or however but this works, **it prob will not work for a user with less permissions than Admin or without all file permission rights because that is how it checks if its used by another process...
-
-
-
Catch ex As Exception
-
ListBox1.Items.Add(file & " Is Not Accessible")
-
End Try
-
-
-
Next
-
-
End Sub
...does it help ?
joedeene
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: [VB.net] - File Locked = Good Code?
Thanks alot joedeene
I tried your code it works.
But currently, i already have a similar function - -
Function isFileLocked(ByVal FileToCheck As String) As Boolean
-
Try
-
My.Computer.FileSystem.GetFileInfo(FileToCheck).Open(FileMode.Open, FileAccess.Write, FileShare.Delete)
-
-
Catch e As Exception
-
Return True
-
'Log errors to file too
-
End Try
-
-
Return False
-
End Function
-
I was wondering, is this code better or the one you posted?
They both seem to work? :o
i am also getting first chance exceptions on it - is that okay? -
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
-
One final thing :) -
On Error GoTo MyError
-
My.Computer.FileSystem.DeleteFile(FilesFound)
-
MyError:
-
DisplayBox.Text &= "File is in Use: " & FilesFound & Environment.NewLine
-
Currently at the moment i am using the above code - which doesn't produce any sort of weird stuff such as those first chance exceptions.
Is this a better method?
Waiting for your kind response :)
Regards
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|