Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,083 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,797 people online right now. Registration is fast and FREE... Join Now!




Locked folders

 
Reply to this topicStart new topic

Locked folders, Testing for locked files/folders and unlocking them

Damage
6 Jun, 2008 - 03:11 PM
Post #1

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
Hey, i've got a small search app that allows the user to specify a location to search on their local machine, if none is specified the app searches through all drives and folders on that machine. The problem is,i've got a folder that is locked.I was moving it and cancelled it half way or the power went off or something, i can't remember, bottom line is every time i try access it i get the "Access denied" message. I've tried renaming it(only managed to rename it once), booting in safe mode(Running XP), command prompt, third party software to force unlock it, scheduling it for deletion up startup, but nothing works. And now everytime i search through that drive i get an error. It's not serious and seeing as how i know the folder name i can exclude it from the search, but it is FREAKING annoying. And it just highlights the fact that it may occur on another machine where i don't know about it before execution.

So my question is, is there a way to test for folders/files that are denied and then exclude them from a search

this is part of the code i'm using, don't think i need to post it (remember absolute noob)

CODE


For Each d In allDrives
                    Dim drivename As String = d.Name
                    If d.DriveType.ToString = "Fixed" Then
                        Dim errorfolder As String = drivename + "System Volume Information"
                        If String.Equals(searchFolderName, "") Then   'checks to see user has not specified folder
                            'List each folder at the root of specified drive
                            For Each strFolder As String In My.Computer.FileSystem.GetDirectories(drivename)
                                'Add the item to the list
                                lstData.Items.Add(strFolder)
                                If strFolder <> errorfolder Or strFolder <> "f:\g" Then
                                    Me.SearchForFiles(strFolder, FileIO.SearchOption.SearchAllSubDirectories, filename)
                                End If
                            Next
                        Else
                            If searchFolderName <> errorfolder Or searchFolderName <> "f:\g" Then
                                searchFolderName = drivename + searchFolderName
                                Me.SearchForFiles(searchFolderName, FileIO.SearchOption.SearchAllSubDirectories, filename)
                            End If

                        End If
                    End If
                Next


not sure if i need to include this part but just in case
CODE

Protected Sub SearchForFiles(ByVal directory As String, ByVal searchOption As Microsoft.VisualBasic.FileIO.SearchOption, ByVal wildCards As String)
        wildCards = "1.1"

        Try
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(directory, searchOption, wildCards)
                lstBooks.Items.Add(foundFile)

                tempCount = tempCount + 1

            Next
        Catch ex As Exception
            MessageBox.Show("Exception thrown: " & ex.Message)
        End Try


    End Sub

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Locked Folders
6 Jun, 2008 - 03:43 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
The best solution I can think of is to create a small function that tries to open the file in question, if it's locked or already open an Exception will be thrown, then we can deal wit it from there. Something like this


vb

Public Function IsAccessible(ByVal File As System.IO.FileInfo) As Boolean
Try
'Try to open this file, if it is locked or in use an Exception will be thrown
'If no exception is thrown all is well we can proceed
Dim stream As New System.IO.FileStream(File.FullName, IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.None)
stream.Close()
Catch ex As Exception
'Oops, Exception so we need to skip this file
Return False
End Try
Return True
End Function



Then you can use it like


vb

If Not IsAccessible(YourFileInfo) Then
'Here we might want to make sure it's the file you think
'it is, or simply continue with the search
Else
'Do something else
End If


Hope that helps a little smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month