James Wilson
James Wilson

Reputation: 5150

Crawling a directory

I have this test path:

private static string dCrawler = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "TestLetters";

Is there a way to say:

foreach (item in dCrawler)
{
     if (item.isFile)
     {
          // check file info date modified code
     } else
     {
          foreach (fileinfo file in ...
     }
}

so far I have only found ways to check a file in a directory. Is the only way to do it by having two separate loops one for files and one for folders?

Upvotes: 0

Views: 332

Answers (1)

user2509901
user2509901

Reputation:

You can use Directory.GetFiles(); that returns a string[] and use the string value to create your FileInfo. Like this

foreach (string n in Directory.GetFiles(dCrawler))
{
    FileInfo b = new FileInfo(n);
}

To get directories, you can similarly use Directory.GetDirectories();

foreach (string n in Directory.GetDirectories(dCrawler))
{
    DirectoryInfo b = new DirectoryInfo(n);
}

Upvotes: 5

Related Questions