|
-
Jun 6th, 2009, 10:38 AM
#1
Thread Starter
Frenzied Member
Searching All Files (Access Denied)
Hello here is my code:
vb.net Code:
Module FileCheck
Public BW_AntiCorrupt As New Threading.Thread(AddressOf AntiCorrupt)
Dim THardDrive As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim HardDrive As String = THardDrive.Substring(0, THardDrive.IndexOf("\") + 1) 'C:\
Public ContinueSearch As Boolean = True
Public Results As New List(Of String)
Public DoneGather As Boolean = False
Public DiagnosticReport As String
Public Sub AntiCorrupt()
Dim DirInfo As New IO.DirectoryInfo(HardDrive)
DoneGather = False
Results.AddRange(IO.Directory.GetFiles(HardDrive, "*.*", IO.SearchOption.AllDirectories))
DoneGather = True
For Each file As String In Results
If ContinueSearch = True Then
CheckFile(file)
End If
Next
End Sub
Private Sub CheckFile(ByVal file As String)
Dim finfo As New IO.FileInfo(file)
End Sub
End Module
This code is being called wit this code:
vb.net Code:
BW_AntiCorrupt.IsBackground = True
BW_AntiCorrupt.Start()
frmFileCheckResults.Show()
I am having a problem. The IO.Directory.GetFiles code is resulting in error do to the fact that it is trying to access the Temp folder in the Windows folder on drive C:\. Apparently that folder is restricted... When the code encounters that folder the code stops retrieving files.
Is there a way around this? Is there somewhere i can give my program administrator access? If not then am i able to skip the files in that location?
Last edited by noahssite; Jun 6th, 2009 at 10:43 AM.
-
Jun 6th, 2009, 10:50 AM
#2
Thread Starter
Frenzied Member
Re: Searching All Files (Access Denied)
Would i put this code at the first line of the AntiCorrupt sub?
Code:
Dim i As Security.Permissions.FileIOPermission
i.AllFiles = Security.Permissions.FileIOPermissionAccess.AllAccess
I didn't want to test it because of my next question.
Does this just change the access MY program has? Or does it change the access permently on the computer in general? Or does this code not even work?
-
Jun 6th, 2009, 12:34 PM
#3
Re: Searching All Files (Access Denied)
When i tried your code at one point i got an exception when the search got to the folder of the program, i think it got something when the program try to "touch" the file while it in use.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Jun 6th, 2009, 01:31 PM
#4
Thread Starter
Frenzied Member
Re: Searching All Files (Access Denied)
If that is the case what should i do about it? Also i think it might also be admin access (im on a vista). Is there a way to run the code as administrator?
-
Jun 6th, 2009, 01:47 PM
#5
Thread Starter
Frenzied Member
Re: Searching All Files (Access Denied)
I ran a few tests and i am sure it is the access. I went to the debug exe in my application's folder and ran it as admin and it worked up until a system folder. So my program doesn't just need admin access? It needs system access? or is it just something with file permissions?
-
Jun 6th, 2009, 02:27 PM
#6
Re: Searching All Files (Access Denied)
Unless you're the Admin, you won't get access. So if you want your code to avoid folders/files that it cannot access, you'll need to handle that in code. The DirectoryInfo command will fail, so you will need to implement your own recursion.
Here's an example;
Code:
Dim FolderFailedAccessList As List(Of String)
Dim FolderList As List(Of String)
Private Sub RecurseFolders(ByVal path As String)
' This sub recurses down a folder tree (path) and finds all accessible folder paths
' It also updates a list of folders where an error was incurred getting access
Try
Dim subfolders() As String = IO.Directory.GetDirectories(path, "*.*", IO.SearchOption.TopDirectoryOnly)
For Each folder As String In subfolders
Try
FolderList.Add(folder)
RecurseFolders(folder)
Catch ex As Exception
FolderFailedAccessList.Add(path)
End Try
Next
Catch ex As Exception
FolderFailedAccessList.Add(path)
End Try
End Sub
Private Sub GatherFolders(ByVal path As String)
' This sub gathers all the folders in the path and updates two lists
' FolderList = A list of all accessible folder paths
' FolderFailedAccessList = A list that could not be inspected due to access problems
If System.IO.Directory.Exists(path) Then
Dim searchpath As String
If System.IO.Path.GetExtension(path) = ".lnk" Then
searchpath = ShellMethods.GetShortcutPath(path)
Else
searchpath = path
End If
FolderFailedList = New List(Of String)
FolderList = New List(Of String)
FolderList.Add(searchpath)
RecurseFolders(searchpath)
Else
MessageBox.Show("Error: Folder does not exist", _
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
If you call this sub, it will generate a list of folders that were accessible (FolderList) and will also generate a list of folders (FolderFailedAccessList) that could not be traversed.
Using the FolderList, you can then loop through each folder and inspect the files within it.
To handle a shortcut, you'll also need this Class, which I called ShellMethods;
Code:
Option Strict Off
Friend Class ShellMethods
Public Shared Function GetShortcutPath(ByVal FileName As String) As String
Dim WSH As Object = CreateObject("WScript.Shell")
Dim sc As Object = WSH.CreateShortcut(FileName)
GetShortcutPath = sc.TargetPath
End Function
End Class
If you want to know how to then loop through all the files, here's an example of that which you can adapt to your own purpose;
Code:
Dim FolderIgnoredList As List(Of String)
Public Function GetFileInformation(ByVal target As IO.DirectoryInfo) As IO.FileInfo()
'This sub returns the FileInfo for files in the target path
Dim files As New List(Of IO.FileInfo)
files.AddRange(target.GetFiles("*.*", IO.SearchOption.TopDirectoryOnly))
Return files.ToArray()
End Function
Private Sub WorkOnFiles(ByVal path As String)
' This sub does whatever you want to do with each file found
' It updates lists indicating files/folders that were ignored because of access problems
GatherFolders(path)
For Each folder As String In FolderList
Dim ProblemPath As String
FolderIgnoreList = New List(Of String)
Try
Dim folderinfo As New IO.DirectoryInfo(folder)
Dim fileinfo() As IO.FileInfo = GetFileInformation(folderinfo)
If fileinfo.Length > 0 Then
For Each file As IO.FileInfo In fileinfo
Try
'Do stuff with the file
Catch ex As IO.***********Exception
ProblemPath = IO.Path.GetFullPath(folder.ToString)
If Not FolderIgnoredList.Contains(ProblemPath) Then FolderIgnoredList.Add(ProblemPath)
Catch ex As System.UnauthorizedAccessException
FolderFailedAccessList.Add(folder)
Catch ex As Exception
FolderFailedAccessList.Add(folder)
End Try
Next
End If
Catch ex As IO.***********Exception
ProblemPath = IO.Path.GetFullPath(folder.ToString)
If Not FolderIgnoredList.Contains(ProblemPath) Then FolderIgnoredList.Add(ProblemPath)
Catch ex As System.UnauthorizedAccessException
FolderFailedAccessList.Add(folder)
Catch ex As Exception
FolderFailedAccessList.Add(folder)
End Try
Next
End Sub
Last edited by Bulldog; Jun 6th, 2009 at 02:30 PM.
-
Jun 6th, 2009, 03:25 PM
#7
Thread Starter
Frenzied Member
Re: Searching All Files (Access Denied)
Thank you but:
 Originally Posted by Bulldog
Unless you're the Admin, you won't get access.
I am the admin.
-
Jun 6th, 2009, 04:57 PM
#8
Re: Searching All Files (Access Denied)
ok, but your comment was;
If not then am i able to skip the files in that location?
So I showed you how write code to skip that location.
If you're the Admin and running on Vista then take a look at this thread http://www.vbforums.com/showthread.php?t=556963
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|