Rocshy
Rocshy

Reputation: 3509

Create directory structure

Is there any way I can create a list with all the folders and files that are in a directory? I will specify the path and I want to list all its child folders and files, and write them in a txt file, or maybe an xml file to make it easier to read.

Upvotes: 0

Views: 471

Answers (3)

David Brabant
David Brabant

Reputation: 43459

The Directory.GetFiles method should give you a list of all files, along with their full paths:

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.*", SearchOption.AllDirectories);

Upvotes: 2

Casper Thule Hansen
Casper Thule Hansen

Reputation: 1550

This is a good link to get all files: http://www.csharp-examples.net/get-files-from-directory/

And to get all directories: use Directories.GetFolders() instead.

Then you have to make a for loop or something to traverse them them. Perhaps a recursive method would be a good selection. Something like void PrintFilesAndFolders(string directory)...

Upvotes: 0

AD.Net
AD.Net

Reputation: 13399

Directory.GetFiles and Directory.GetDirectories methods should help

Upvotes: 1

Related Questions