Seeking sample code to scan folders, files

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

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
 
Ronald S. Cook said:
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?
 
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.SpecialFolder.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top