views:

57

answers:

2

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?

+3  A: 

Call Directory.GetFiles.

SLaks
A: 

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);
Joe Axon
You should use @ strings (`@"C:\Users\Joe\Music"`)
SLaks
@SLaks: Good point, thanks!
Joe Axon
This, http://www.codeproject.com/KB/cs/my_explorer.aspx, looks helpful. I downloaded it and had a look. It would be great if This, http://www.codeproject.com/KB/cs/my_explorer.aspx, looks helpful. I downloaded it and had a look. It would be great if this could be used to make a seperate "dialog box" (I mean "form" accoreding to C# lingo) and that what the user selects as a directory can be displayed in some fort of text field.
xarzu