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

Seeking sample code to scan folders, files

P: n/a
I'm wanting to scan all folders and files under a specified path (e.g.
C:\Files). I know using System.IO I can use Directory.GetFiles to get a
list of files under a folder, but how do I get the name of the folder?

Is there any sample code out there that can kickstart me?

Thanks,
Ron
Dec 17 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ronald S. Cook <rc***@westinis.comwrote:
I'm wanting to scan all folders and files under a specified path (e.g.
C:\Files). I know using System.IO I can use Directory.GetFiles to get a
list of files under a folder, but how do I get the name of the folder?
Get the name of *which* folder?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 17 '06 #2

P: n/a
Hi

As I understand you wanted to get the name of directories in side a
directory.
The following code illustrates the navigation of all files inside a
directory.. including directories.

class Program
{
static void Main(string[] args)
{

string path =
Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments);
DirectoryInfo rootDirInfo = new DirectoryInfo(path);

DisplayFilesOfDir(rootDirInfo);
Console.ReadLine();
}

public static void DisplayFilesOfDir(DirectoryInfo di)
{
DirectoryInfo[] dirInfo = di.GetDirectories();
foreach (DirectoryInfo dirInfoindex in dirInfo)
{
Console.WriteLine(dirInfoindex.Name);
DisplayFilesOfDir(dirInfoindex);
}

FileInfo[] fileInfos = di.GetFiles();

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

Hope this is helpful to you.

Thanks
-Srinivas.
Ronald S. Cook wrote:
I'm wanting to scan all folders and files under a specified path (e.g.
C:\Files). I know using System.IO I can use Directory.GetFiles to get a
list of files under a folder, but how do I get the name of the folder?

Is there any sample code out there that can kickstart me?

Thanks,
Ron
Dec 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.