Locked folders

Testing for locked files/folders and unlocking them

Page 1 of 1

1 Replies - 1874 Views - Last Post: 06 June 2008 - 04:43 PM Rate Topic: -----

#1 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 53
  • View blog
  • Posts: 1,186
  • Joined: 05-June 08

Locked folders

Post icon  Posted 06 June 2008 - 04:11 PM

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)


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



Is This A Good Question/Topic? 0
  • +

Replies To: Locked folders

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Locked folders

Posted 06 June 2008 - 04:43 PM

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


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


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 :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1