Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

.Net Framework 4.0: Enumerating file system objects

In my last posting I introduced new ReadLines() method and new overloads for WriteAllLines() method of File class. But there are more new stuff in System.IO namespace. In .Net Framework 4.0 Directory and DirectoryInfo class are able to enumerate files, directories and file system entries. In this posting I will show you these new features.

Let’s see now how to enumerate file system objects using new static methods of Directory class. There is one thing you should know. If you have to handle files and directories in same context then you should use EnumerateFileSystemEntries() method that enumerates both files and directories. The following example shows you how to enumerate different file system objects.


static void Main(string[] args)

{

    var path = Path.GetPathRoot(Environment.CurrentDirectory);

 

    var files = Directory.EnumerateFiles(path);
    var directories = Directory.EnumerateDirectories(path);   
    var entries = Directory.EnumerateFileSystemEntries(path);

 

    Console.WriteLine("Files:");

    foreach (var file in files)

        Console.WriteLine(file);

 

    Console.WriteLine("\r\nDirectories:");

    foreach (var directory in directories)

        Console.WriteLine(directory);

 

    Console.WriteLine("\r\nEntries:");

    foreach (var entry in entries)

        Console.WriteLine(entry);

 

    Console.ReadLine();

}


You should get output like this (the path is root path of drive where application is located).

Files:
C:\autoexec.bat
C:\config.sys
C:\pagefile.sys
  
Directories:
C:\$Recycle.Bin
C:\Documents and Settings
C:\Install
C:\PerfLogs
C:\Program Files
C:\ProgramData
C:\Recovery
C:\System Volume Information
C:\Users
C:\Windows
Entries:
C:\$Recycle.Bin
C:\autoexec.bat
C:\config.sys
C:\Documents and Settings
C:\Install
C:\pagefile.sys
C:\PerfLogs
C:\Program Files
C:\ProgramData
C:\Recovery
C:\System Volume Information
C:\Users
C:\Windows

These enumerating methods work better than ones that return arrays because requests to file system are made only when concrete object is asked from enumerator. I think these methods may be very good load balancers in applications that make heavy use of file system.


kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Shout it!
Posted: Oct 27 2009, 12:14 PM by DigiMortal | with 14 comment(s)
Filed under: ,

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# October 27, 2009 5:29 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 27, 2009 5:37 AM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# October 27, 2009 5:40 AM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# October 27, 2009 5:45 AM

9eFish said:

9efish.感谢你的文章 - Trackback from 9eFish

# October 27, 2009 5:46 AM

Twitter Trackbacks for .Net Framework 4.0: Enumerating file system objects - Gunnar Peipman's ASP.NET blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 .Net Framework 4.0: Enumerating file system objects - Gunnar Peipman's ASP.NET blog         [asp.net]        on Topsy.com

# October 27, 2009 5:49 AM

Servefault.com said:

Thank you for submitting this cool story - Trackback from Servefault.com

# October 27, 2009 5:59 AM

Gunnar Peipman's ASP.NET blog said:

Here are my postings about Visual Studio 2010 and .Net Framework 4.0 that may be interesting to my readers

# October 27, 2009 6:04 AM

.Net Framework 4.0: Enumerating file system objects - Gunnar … said:

Pingback from  .Net Framework 4.0: Enumerating file system objects - Gunnar …

# October 27, 2009 6:08 AM

.Net Framework 4.0: Enumerating file system objects - Gunnar … Scripts Rss said:

Pingback from  .Net Framework 4.0: Enumerating file system objects - Gunnar … Scripts Rss

# October 27, 2009 10:20 AM

uberVU - social comments said:

This post was mentioned on Twitter by gpeipman: New blog post: http://tinyurl.com/yhrvref - .Net Framework 4.0: Enumerating file system objects

# October 27, 2009 12:16 PM

Webmaster Crap » Blog Archive » .Net Framework 4.0: Enumerating file system objects - Gunnar … said:

Pingback from  Webmaster Crap  » Blog Archive   » .Net Framework 4.0: Enumerating file system objects - Gunnar …

# October 27, 2009 6:38 PM

DotNetBurner - .net Framework said:

DotNetBurner - burning hot .net content

# October 27, 2009 8:17 PM

.Net Framework 4.0: Enumerating file system objects « Jasper Blog said:

Pingback from  .Net Framework 4.0: Enumerating file system objects « Jasper Blog

# October 28, 2009 4:52 AM