Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I am trying to retrieve a list of all files, but for some reason it is telling me access denied on some files and the program ends.

i know i can use a catch exception.. but i dont want to do this, otherwise it ignores those files. I need the program to display EVERY file.

the code i am using is:

MIDL
// Gets all files in the directory selected and any sub directories
           string[] files = Directory.GetFiles(textBoxDirectory.Text, "*", SearchOption.AllDirectories);


it kicks back the error:

Access to the path "blah blah blah" is denied.

I have attempted running the program as Administrator and i am still unable to get it to work.

does anyone have any ideas on how i can get around this issue?

Many thanks,
Hyps
Posted

 
Share this answer
 
Comments
HypeH 5-Mar-11 15:27pm    
Perfect, thank you very much. :) Solved :)
Sergey Alexandrovich Kryukov 5-Mar-11 18:58pm    
Good, my 5, but there is one more subtle problem; please see my answer.
--SA
I want to give you a big warning about System.IO.Directory.GetFiles. It does not work like you expect it.

See this discussion: Directory.Get.Files search pattern problem[^].

Pay attention to this fragment which fixes the problem of this method: Where(item => item.ToLower().EndsWith(ExtExe));

—SA
 
Share this answer
 
Comments
HypeH 5-Mar-11 19:04pm    
my intention is to pull back a list of every file on the directory the user has selected. then i will be using checking the file signatures of all the files and only searching for specific ones. e.g. a.jpg so i will be looking for the Hex Value FF D8 FF E0 00 in the first 8 bytes. if the file matches that, which i have stored, I will then display it in an images category.

if that makes sense?

its all for a project at university, but my programming skills are not that great. i just need to design it, so no one can take a image.jpg and rename it as image.pdf. and i thought checking the file signature/magic number was the best way.
Sergey Alexandrovich Kryukov 5-Mar-11 19:42pm    
It does not make sense, see my Answer to your previous Question; I gave you detailed explanation why, let's keep that line of discussion.
If you have a different idea of "magic number" which can disprove my reasoning then please explain it. I don't think there are magic numbers.

Also, I think you need to explain your _ultimate_ goal. What's the use of your application, in general, and, in particular, the categorization feature. (If I would be the user, I would be satisfied with just file name patterns.)

My warning in this Answer is very important, please read and accept my answer is you can (and how about that previous Question and my Answers?)

--SA
HypeH 5-Mar-11 21:33pm    
Ok right. i study forensic computing. so i need to make a simple search tool that will search a drive for all files, and then categorise them into tabs. so 1 tab says "images" "Videos" "music" and so on. the search tool is not intended to give results to EVERY file. but i need to pull all the files so i can check them first. now, i do forensics and we look at files in quite a bit of depth, and look at the file signature which is Generally (not always!) the first 8 bytes of the file, and in most cases, it is unique to that file. for example "FF D8 FF E0" is unique to a jpg file and stored in the header. now i have worked out how to retrieve that information.

the purpose behind me doing this is to make the search tool more accurate. its a requirement. I am REQUIRED to do a check on the file signature so i can ensure the file is a .jpg for example. now i have a full list of all the file types i intend to be using, and i have their hex values and i know they are all at the start of the file. i have now worked out how to do get it, and i have tested getting the file signature from a file and comparing it to a hex value i have stored in an array and it works fine.

i know how to check 1 file at a particular location, but how can i get it to run the check on all files in the directory, and then output all the matches to a listbox?

Im sorry if what i want isnt to how u may think it should be programmed, i am only going by what my requirements are. its all to be able to identify a file, so if someone renames a image.jpg to image.bob i can still establish that it is a .jpg file.

many thanks
Sergey Alexandrovich Kryukov 6-Mar-11 1:39am    
This is, strictly speaking, impossible. The requirements should specify an accepted probability for false positives. Who told you first 8 bytes are unique? They a not. For some standards, they are never the same (let's say, a signature is only 4 bytes). And the same identical bytes can match your signature just by a chance. The key is: there is no standard identification of file types, so nobody is responsible for valid signatures. Just a fact of life.

You see, you cannot simply say "this is my requirements". Requirements can be valid or not, and the valid ones can be reasonable or not; another fact of life. What to do is your personal choice. I tend to said "stick your stupid requirements..." and deal with consequences. If you accepted the requirements, now it's your problem.

Also, let's imagine you still doing this. I simply don't understand what seems to be a problem. What, you don't know how to read N first byte? Well, read System.IO first :-)

Good luck,
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900