[Sample of Nov 30th] List files in directory in .NET

 

Homepage image
RSS Feed

Sample Download :

CS Version: https://code.msdn.microsoft.com/CSListFilesInDirectory-3b2430dc

VB Version: https://code.msdn.microsoft.com/VBListFilesInDirectory-6c5ea8b8

This code sample demonstrates how to implement an IEnumerable<string> that utilizes the Win32 File Management functions to enable application to get files and sub-directories in a specified directory.

The FileEnumerator class in this sample project solves a common problem in the System.IO.Directory.GetFiles method - if a directory contains a largenumber of items, this it will take a long time for this method to returnbecause it will enumerate all the files and put the names in an array asan entire operation. This will also cause a very high memory load if the array gets huge.

The FileEnumerator class works differently. It returns one file at a time.And the enumeration can be canceled or reset at anytime.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage https://1code.codeplex.com/.