create array with paths of all files within a dire

Results 1 to 2 of 2

Thread: create array with paths of all files within a dire

  1. #1
    Join Date
    Dec 1969

    Default create array with paths of all files within a dire

    I know I can create an array with the files&#039; paths of a folder. Like below<BR><BR>string[] filenames = Directory.GetFiles("\\path\to\folder");<BR><BR>but can someone bring to my attention an easy way, of creating an array containing filepaths of "ALL" files within a folder? with "ALL" I mean also the files contained in sub-folders, sub-sub-folders etc..etc...<BR>I would be happy to be find a function like that...since I am really a beginner in c# and I probably will never be able to write a similar function within the next year ;-&#124

  2. #2
    Join Date
    Dec 1969

    Default RE: create array with paths of all files within a

    You have to create your own function that will recursively iterate through the subdirectories of the directory, getting all files...<BR><BR>For a bit of info on recursion, see:<BR><BR><BR>Essentially, you will need to do something like:<BR><BR><BR><BR>void GetAllFiles(string dir, ArrayList files) {<BR> files.AddRange(Directory.GetFiles(dir);<BR><BR> foreach(string subdir in Directory.GetDirectories(dir))<BR> GetAllFiles(subdir, files);<BR>}<BR><BR><BR><BR>You would call this function like so:<BR><BR>ArrayList myFileList = new ArrayList();<BR>GetAllFiles("\\path\to\folder", myFileList);<BR><BR><BR>and upon return, the myFileList ArrayList will contain the path to all the files in the directory and subdirectories. (The code is untested, but should work as-is or with some minor syntax tweaking... hth)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts