By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Is there a way to get a list of file names from within a directory

P: n/a
I want to display a list of filenames that exist in a certain
directory. How do I go about loading this list?

Jun 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
jpabich wrote:
I want to display a list of filenames that exist in a certain
directory. How do I go about loading this list?


Look into:
System.IO.Directory
(http://msdn2.microsoft.com/en-us/lib...directory.aspx)
System.IO.DirectoryInfo
(http://msdn2.microsoft.com/en-us/sys...ctoryinfo.aspx)

string[] files = System.IO.Directory.GetFiles(@"c:\directory");

Dan Manges
Jun 7 '06 #2

P: n/a
See DirectoryInfo/FileInfo classes in MSDN

public static void Main()
{
// Create a reference to the current directory.
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
// Create an array representing the files in the current directory.
FileInfo[] fi = di.GetFiles();
Console.WriteLine("The following files exist in the current
directory:");
// Print out the names of the files in the current directory.
foreach (FileInfo fiTemp in fi)
Console.WriteLine(fiTemp.Name);
}
}

"jpabich" wrote:
I want to display a list of filenames that exist in a certain
directory. How do I go about loading this list?


--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Jun 7 '06 #3

P: n/a

"jpabich" <jp*****@eisincorporated.com> wrote...
I want to display a list of filenames that exist in a certain
directory. How do I go about loading this list?


If it's just in order to "display" the list of filenames, you can simply do:

DirectoryInfo dirInfo = new DirectoryInfo("directoryPath");
FileInfo[] fileInfos = dirInfo.GetFiles();

foreach (FileInfo fi in fileInfos)
{
Console.WriteLine(fi);
}

If you want to display it in some other way, you can use each FileInfo as
you like, e.g. to extract only the short filenames as strings, as the above
will display the full path of the files.

/// Bjorn A
Jun 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.