Click here to Skip to main content
14,599,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to perform a get file method but for some reason either the code is wrong or the program privlidges are not full. I figure the code to be correct but the Catch ex As UnauthorizedAccessException I think may be keeping the scan from going into the subfolders which is my main goal at this point for my scanning application.

I need the user to choose a root directory such as C:\ then the start scan button with use the get file method to search within the given root directory.

the start scan code is:

Dim searchForTheseFiles() As String = TextBox1.Text.ToUpper.Split(Environment.NewLine)
       Dim fileList As New List(Of String)(searchForTheseFiles)
       Dim myDir As New DirectoryInfo(CheckedComboBoxEdit1.Text)
       Try
           Dim files() As FileInfo = myDir.GetFiles("*.*", SearchOption.AllDirectories)
           For Each foundFile As FileInfo In files
               If fileList.Contains(foundFile.Name.ToUpper) Then
                   ListBox3.Text &= Environment.NewLine & foundFile.FullName
                   My.Computer.Audio.Play(My.Resources.fat_n_soft_button_4, AudioPlayMode.Background)
               End If
           Next
       Catch ex As UnauthorizedAccessException
           Debug.WriteLine(String.Format("Could not access directory '{0}'.", myDir.FullName))
       End Try


Please tell me if anything is wrong with this code and if not I truly need to know how to set the program to have full access to all folders and subfolders in the root directory thanks for taking a stab at this..
Posted
Updated 18-Oct-10 21:39pm
v2
Comments
Dalek Dave 19-Oct-10 3:39am
   
Edited for Code Blocks.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Another layer in program? I don't know...
This class seems like you should know in advance to which files you have permission and to which you does not in order to use it.

If i understood your code correctly you want to search recursively some path. Hm...
Can't you do something like this (I'm sorry for C# but i really hate languages with syntax BEGIN ... EN like in VB)?

public void search(string path)
{
string[] files = new string[0],
dirs = new string[0];
try
{
files = Directory.GetFiles(path,"*.*",SearchOption.TopDirectoryOnly);
dirs = Directory.GetDirectories(path);
}
catch(UnautorizedAccessExxeption e)
{
//do something with Exception
}
foreach(var file in files)
{
//list finded files or do something with them
}
foreach(var dir in dirs)
{
//go deeper in tree
search(dir);
}
}

This code will handle exception when you will try access files that you cant but not stop there and just go to another brunch in tree.

I hope it will help you!

#Edited

Try swapping a function i posted here with line of code
Dim files() As FileInfo = myDir.GetFiles("*.*", SearchOption.AllDirectories)


and in that fuction aggregate all files matching your parameters.
It should work fine :)
   
v2
Comments
Dale 2012 22-Oct-10 1:05am
   
I have tried to convert this code to VB but when I had tried to use the code there were errors, some spelling mistakes and also I need the code to excute when the user presses the start scan button supposing thats how it works?
n.podbielski 22-Oct-10 3:18am
   
Of course there was some errors and typos. I didn't write this in compiler but here.
I just wanted to present you possible solution of the problem more like give you fully working code, especially without knowledge of on what you working in that project :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

A TextBox? Is this a Win program or a WebPage?
I am not familiar enough with VB to tell this from this particular chunk of code, but if you trying do this in IIS: you can't, you shouldn't and you shouldn't even try to do that. It would cause a huge security risk.
Of course you can force it anyway, but why?
I suppose you don't want some anonymous user delete all files in root dir, do you?
   
Comments
Dale 2012 19-Oct-10 5:50am
   
this code is in VB and does not give the user the choice to delete any directory. It should give the user the choice to select a root directory and then the start button code tells it to search the root dir subfolders
n.podbielski 19-Oct-10 7:05am
   
I uderstood that, but a side of that, web pages (you didn't corrected me so I was right, right? this is web page) just don't have access to any directories except it's own.
It just work that way.
I don't say that your code allowed any malicious user activities but u just can't predict anything, so you can't be sure that it can't be done.

U can try running project on IIS as another user whom has access to every directory on server, but...
maybe it will be better to think of some other program running on system not connected directly with web page, returning result for given directory.

But it's just my advice :)
Dale 2012 19-Oct-10 8:02am
   
Again the code is VB and not webpage
n.podbielski 19-Oct-10 9:35am
   
Man it would save as a LOT of time if you said that before... huh...
(still VB is a language not a kind of project)

I don't know what directory u are browsing but you have to remeber that even as a user of admin role in windows
you don't have access to every directory. For example you cannot view 'System Volume Information' on every disk root dir. So you have to disable this from search somehow or this code never get deeper in any dir from root (it will stop in root because of this).
My guess is to try search for files in every directory, than search for directories, and get deeper in catalog tree.

I hope this will help you :)
Dale 2012 19-Oct-10 16:28pm
   
I think the answer lays within the FileIOPermission Class but thank you for the suggestion. If you have any info about how to apply a code such as the FileIOPermission Class please let me know..... thank you and sorry for my lack of being able to state a question lol :)
Dale 2012 19-Oct-10 16:28pm
   
I think the answer lays within the FileIOPermission Class but thank you for the suggestion. If you have any info about how to apply a code such as the FileIOPermission Class please let me know..... thank you and sorry for my lack of being able to state a question lol :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100