0

I am currently writing a program which searches My Documents. Currently my program is able to search and copy the main my documents folder but I am unable to make it search sub directory's within the main my documents directory. I have tried multiple methods but none seem to be working out. Currently I am using the below code to dump the files location into an array called files. sourcePath is declared in an array before hand.

string[] files = System.IO.Directory.GetFiles(sourcePath[loopcounter]);

I then have a loop which copy's the files over to another directory

foreach (string s in files)

Any help as to how to fill the array files with details of files in the sub directories of a folder would be very handy. Thanks in advance!

Ben Collins
  • 221
  • 1
  • 4
  • 12

2 Answers2

2

Use research by pattern and specify you want use recursion :

var allFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                 "*",
                                 SearchOption.AllDirectories);

foreach (var item in allFiles)
{
    // Do Stuff...
}
Arnaud F.
  • 8,252
  • 11
  • 53
  • 102
  • This code has worked well, I had already looked at the links posted by some other contributors but thanks anyway. My only problem with this is it appears to search to far up the directory tree? When searching My Pictures(`Environment.SpecialFolder.MyPictures`) for instance it then wants to search My Music which throughs up an access error. There is no short-cut or link in My Picture to My Music so I cant see why it would be doing this? And even when UAC is raised and the Program is run as Administrator the same error occurs. Any help is greatly appreciated. Thanks! – Ben Collins Oct 04 '11 at 12:58
0

If you want details about each file, then GetFiles returns you array of names. Pass each name to FileInfo API.

Zenwalker
  • 1,883
  • 1
  • 14
  • 27