Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need code to get all the files of a directory (including all subdirectories) from a drive and store it into an array. At a later time I want to use that array and search for particular file and delete that file.

Thanks in advance.


[Edit: spelling, conciser title]
Posted
Updated 2-Jan-11 3:48am
v2
Comments
Sergey Alexandrovich Kryukov 2-Jan-11 14:39pm    
Girish, we're almost fighting to find a better solution for you, but you could be less lazy to spend just a few minutes searching of regular Microsoft help or Google to find a right answer. Ok, this time you do have an answer.

Use the class Directory (not DirectoryInfo), the method GetFiles with SearchOption parameter: AllDirectories for recursive directory search, TopDirectoryOnly for just one directory.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 2-Jan-11 14:49pm    
Good call! 5+
You caught me on that one. Mine is still a solution to OP's problem even if it is not as slick as yours though! ;)
Sergey Alexandrovich Kryukov 2-Jan-11 14:53pm    
Thanks for good words, Manfred. You always act as a good honest person (unlike some others :-( )
Manfred Rudolf Bihy 2-Jan-11 15:05pm    
Thanks to you for directing me to a better solution! I always appreciate it when others contribute. That is what this place was build for after all. Even the most seasoned developer can learn here and this is why I love this place. Cheers!
Espen Harlinn 28-Feb-11 15:56pm    
Nice and simple solution, my 5
Sergey Alexandrovich Kryukov 1-Mar-11 4:03am    
Thank you, Espen,
--SA
This page should cater to your needs. They show code for a function that recursively lists all files in directory and all sub directories:

Recursive directory function in VB.NET[^]

They return a List, but you can turn that into an array with the List.ToArray() method. I would not do that though because it's so easy to search in a List using the Predicate delegate. Look that up on MSDN: List.Find & List.FindAll



Please refer to SAKryukov's answer, it is the best solution in this case as it relies solely on already built in .NET functions plus you get your files in an array as per your request (or requirement).
If you already looked up List.Find and List.FindAll and would like to use it, you can create a list from an array and then back again should the need arise:

C#
List<String> myList = new List<String>(theArrayFromTheGetFilesCall);


Hope that helps you!

Best Regards,

Manfred
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 2-Jan-11 14:36pm    
Sorry Manfred, your reference is not a good solution. It ignores the fact, that System.IO already has a method of returning recursive search result in an array. Please see my answer.
(I did not vote to give you a change to improve. Please find a reference to System.IO.Directory.GetFiles in Microsoft help or somewhere if you wish.) Thank you
Go through the below link [DirectoryInfo]

Here[^]

There are thousnad of examples avaible in the internet., Search in Google/CP before posting a question
 
Share this answer
 
try writing a code:
IO.Directoryinfo()
Dim a() as String
Dim b() as string
Dim f as IO.filename()
a = IO.Directoryinfo.Fileinfo.File.path()
b = split(a,"\")
f = IO.Fileinfo.filename
IO.Filename.Delete(f)
the code might not be perfect, but it will give you an idea and might work.
Prasad :-D
 
Share this answer
 
Comments
fjdiewornncalwe 2-Jan-11 10:40am    
I'm sorry, but you don't have the vaguest idea what the OP is asking for with your answer.
As well, please wrap the code portion of your answer in the appropriate pre tags so that it is readable.
Manfred Rudolf Bihy 2-Jan-11 10:46am    
@Marcus: Come on, you can tell he's still a beginner. There should be period of grace before we start the blatently derogatory stuff and the usual abuse. ;)
Sergey Alexandrovich Kryukov 2-Jan-11 14:22pm    
Very, very bad answer, leading to a very, very bad code.
Sergey Alexandrovich Kryukov 2-Jan-11 14:26pm    
Hey people, stop giving credits to the beginners just for being beginners.
There is only one difference: a beginner needs smaller slower steps and more detailed elementary explanations.
Being a beginner is not an excuse for a code dirt.
And an attempt to give answers beyond the skills of the answered should be cut down -- such attempts hurt other, honest beginners. Seriously.
Manfred Rudolf Bihy 2-Jan-11 15:00pm    
@SAkryukov: That is not what I said. I just meant to say to stop bashing them. I'm always fo people being guided and led to be better programmers, but we shouldn't make them feel bad right from the start. There's alway enough time to do that if they show that they are not willing to take advice.
My 2ct!

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