xarzu
xarzu

Reputation: 9479

Directory Search

This is a simple question and I am sure you C# Pros know it.

If you want to grab the contents of a directory on a hard drive (local or otherwise) in a C Sharp program, how do you go about it?

Upvotes: 0

Views: 678

Answers (2)

Joe Axon
Joe Axon

Reputation: 164

If you mean get a directory then it is very simple. The following code will give you a list of all the directories in a given directory and then list all the files in the directory:

string dir = @"C:\Users\Joe\Music";
DirectoryInfo dirInfo = new DirectoryInfo(dir);

FileInfo[] files = dirInfo.GetFiles();
DirectoryInfo[] directories = dirInfo.GetDirectories();

foreach (DirectoryInfo directory in directories)
    Console.WriteLine(directory.Name);

foreach (FileInfo file in files)
    Console.WriteLine (file.Name);

Upvotes: 0

SLaks
SLaks

Reputation: 887195

Call Directory.GetFiles.

Upvotes: 3

Related Questions