Parsing a directory

  • Thread starter Thread starter John Young
  • Start date Start date
J

John Young

I'm trying to parse a directory, but am not sure of the best way of
doing it. Preferably using only .net instructions. Can anyone give me an
idea of how to do this?

Thanks in advance for any help that anyone can give me...

John
 
What do you mean "parsing a directory"? Do you mean parsing a directory
path, or enumerating the files in a directory?

Well, the Path class will help parsing the path. The Directory and
DirectoryInfo will help enumerate the files.
 
using System.IO;

Write a separate method (so it can be used recursively), and pass it a
parameter that represents the directory where you want things to start.

In that method, create a DirectoryInfo object, passing the directory's path
in the ctor. That object will have its own collection of DirectoryInfo
objects called Directories, and its own collection of FileInfo objects
called Files.

Typically, you would do what you want to do for each FileInfo in Files, and
for each DirectoryInfo in Directories, call the method recursively. That
way, you can process all the files and directories in a subtree of arbitrary
depth. The order in which you do these things will determine whether you do
a depth-first or breadth-first traversal of the directory tree.
 
Back
Top