Connecting Tech Pros Worldwide Forums | Help | Site Map

[VB.net] - File Locked = Good Code?

Member
 
Join Date: Sep 2008
Posts: 74
#1: Sep 20 '08
Expand|Select|Wrap|Line Numbers
  1. Shared Function FileIsLocked(ByVal fileFullPathName As String) As Boolean
  2.     Dim isLocked As Boolean = False
  3.     Dim fileObj As System.IO.FileStream
  4.  
  5.     Try
  6.         fileObj = New System.IO.FileStream(_
  7.                                 fileFullPathName, _
  8.                                 System.IO.FileMode.Open, _
  9.                                 System.IO.FileAccess.ReadWrite, _
  10.                                 System.IO.FileShare.None)
  11.     Catch
  12.         isLocked = True
  13.     Finally
  14.         If fileObj IsNot Nothing Then
  15.             fileObj.Close()
  16.         End If
  17.     End Try
  18.     Return isLocked
  19. End Function
  20.  
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

kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#2: Sep 20 '08

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
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#3: Sep 20 '08

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
#4: Sep 20 '08

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
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#5: Sep 20 '08

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
#6: Sep 21 '08

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
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#7: Sep 21 '08

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
#8: Sep 21 '08

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
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#9: Sep 22 '08

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....

Expand|Select|Wrap|Line Numbers
  1.  Private Sub btnCheckFileLocked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckFileLocked.Click
  2.         checkiffilesarelocked(My.Computer.FileSystem.SpecialDirectories.Desktop)
  3.     End Sub
  4.  
  5.     Private Sub checkiffilesarelocked(ByVal dir As String)
  6.         For Each file In IO.Directory.GetFiles(dir)
  7.             Dim myfile As IO.FileStream
  8.             Try
  9.                 myfile = New IO.FileStream(file, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None)
  10.  
  11.                 myfile.Close()
  12.  
  13.                 '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...
  14.  
  15.  
  16.             Catch ex As Exception
  17.                 ListBox1.Items.Add(file & " Is Not Accessible")
  18.             End Try
  19.  
  20.  
  21.         Next
  22.  
  23.        End Sub
...does it help ?


joedeene
Member
 
Join Date: Sep 2008
Posts: 74
#10: Sep 23 '08

re: [VB.net] - File Locked = Good Code?


Thanks alot joedeene

I tried your code it works.

But currently, i already have a similar function -

Expand|Select|Wrap|Line Numbers
  1.     Function isFileLocked(ByVal FileToCheck As String) As Boolean
  2.         Try
  3.             My.Computer.FileSystem.GetFileInfo(FileToCheck).Open(FileMode.Open, FileAccess.Write, FileShare.Delete)
  4.  
  5.         Catch e As Exception
  6.             Return True
  7.             'Log errors to file too
  8.         End Try
  9.  
  10.         Return False
  11.     End Function
  12.  
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?

Expand|Select|Wrap|Line Numbers
  1. A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
  2.  
One final thing :)

Expand|Select|Wrap|Line Numbers
  1.                 On Error GoTo MyError
  2.                 My.Computer.FileSystem.DeleteFile(FilesFound)
  3. MyError:
  4.                 DisplayBox.Text &= "File is in Use: " & FilesFound & Environment.NewLine
  5.  
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
Reply