473,382 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

79
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
Sep 20 '08 #1
9 9059
kenobewan
4,871 Expert 4TB
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
Sep 20 '08 #2
joedeene
583 512MB

'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
Sep 20 '08 #3
zubair1
79
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
Sep 20 '08 #4
joedeene
583 512MB
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
Sep 20 '08 #5
zubair1
79
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
Sep 21 '08 #6
joedeene
583 512MB
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
Sep 21 '08 #7
zubair1
79
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
Sep 21 '08 #8
joedeene
583 512MB
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
Sep 22 '08 #9
zubair1
79
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
Sep 23 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: DotNetIsHorrible | last post by:
I write CRUD database applications for a living for an audience of about 100 users per application using classic ASP. I maintain and frequently change on user's request 22 different applications...
1
by: Robert Halford | last post by:
On 4th May at 7.45 in the evening my asp.net web sites stopped working on my development server. The page that appears says: Server Application Unavailable The web application you are...
5
by: Fabio R. | last post by:
To support a webfarm scenario, I'd like to store a global array (serialized) in a file on a network share. In this array there is a list of pages "locked" by other users so I need to read this...
12
by: Peteroid | last post by:
I was creating my application just fine for the last 3 weeks or so. Then, starting this morning, IntelliSense seems to be having problems. It goes into a locked 'Updating IntelliSense..." mode....
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
5
by: cmay | last post by:
The only examples I have seen on how to check if a file is locked is to try to open it a catch an exception. MS has stated that you should never use error trapping in this manner. Is there no...
6
by: elake | last post by:
I found this thread about a pst file in Windows being locked and I am having the same issue. ...
12
by: Steve | last post by:
I've been building an application that will merge fields in a text file with a word template, save the resulting word file out to the user's hard drive, and then email the file as an attachment. ...
0
by: shamirza | last post by:
· What is view state and use of it? The current property settings of an ASP.NET page and those of any ASP.NET server controls contained within the page. ASP.NET can detect when a form is requested...
8
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.