DESKTOP

Enumerate Directories and Files

9/25/2010 3:17:04 PM
Problem : You need to get a (possibly recursive) list of all directories or files.

Solution:
DirectoryInfo can retrieve a list of subdirectories and files inside of it.


string root = @"C:\";
//old way--get all strings up front
DirectoryInfo di = new DirectoryInfo(root);
DirectoryInfo[] directories = di.GetDirectories("*",
SearchOption.AllDirectories);

//new in .Net 4--use an enumerator
di = new DirectoryInfo(root);
//to get files, use di.EnumerateFiles with the same type of arguments
IEnumerable<DirectoryInfo> dirInfo = di.EnumerateDirectories("*",
SearchOption.AllDirectories);
foreach (DirectoryInfo info in dirInfo)
{
Console.WriteLine(info.Name);
}

Note

Be aware that you can easily get a SecurityException while enumerating files and folders, especially if you’re not running as an administrator.

Other  
 
Video
Top 10
-  Refrigerator Is Calling (Part 2)
-  Refrigerator Is Calling (Part 1)
-  Samsung NX200
-  Samsung MV800
-  Panasonic Lumix DMC-GX1
-  Upgrade Power - Guidelines For PSU Buyers (Part 6) - Apex AL-500EXP, Logisys PS575XBK 575W
-  Upgrade Power - Guidelines For PSU Buyers (Part 5) - Azzatek Titan 1000W, PC Power & Cooling Silencer Mk III 600W
-  Upgrade Power - Guidelines For PSU Buyers (Part 4) - Cooler Master Silent Pro Gold 800W, Corsair Enthusiast Series Modular TX850M
-  Upgrade Power - Guidelines For PSU Buyers (Part 3) - Corsair Professionial Series HX1050, Topower PowerBird 1100W
-  Upgrade Power - Guidelines For PSU Buyers (Part 2) - Antec HCP-850, Cooler Master Silent Pro Hybrid 1050W
Most View
-  Managing and Administering SharePoint 2010 Infrastructure : Using Additional Administration Tools for SharePoint
-  Binding Application Data to the UI objects in Silverlight
-  iPhone Application Development : Getting the User’s Attention - Generating Alerts
-  Understanding and Using Windows Server 2008 R2 UNIX Integration Components (part 2)
-  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 3) - Finishing the Interface
-  Cisco Linksys X3000 - The Link to Connectivity
-  HP LaserJet Pro CM1415fnw - Print from The Clouds
-  Building Your First Windows Phone 7 Application (part 2) - Using Your First Windows Phone Silverlight Controls
-  Determine Your Need for Server Core
-  Mobile Application Security : Bluetooth Security - Overview of the Technology
-  Using System Support Tools in Vista
-  Windows 7 : Using Windows Live Calendar (part 3) - Scheduling Appointments and Meetings & Viewing Agendas and Creating To-Do Lists
-  Advanced ASP.NET : The Entity Framework (part 3) - Handling Errors & Navigating Relationships
-  Graham Barlow: the Apple view
-  Ipad : Presentations with Keynote - Adding Transitions (part 2) - Object Transitions
-  Windows Server 2003 : Troubleshooting Group Policy
-  Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
-  Building the WinPE Image
-  Programming the Mobile Web : HTML 5 (part 3) - Offline Operation
-  Windows Phone 7 Development : Using Culture Settings with ToString to Display Dates, Times, and Text