June 17, 2010

Enumerate Hidden Directories in .NET 4.0




In one of my previous posts, I had discussed 7 New methods to Enumerate Directory and Files in .NET 4.0.

Let us see a practical example of using the DirectoryInfo.EnumerateDirectories to list the names of only hidden directories in a drive.

C#

static void Main(string[] args)
{
// dir is a collection of IEnumerable<string>
var dir = new DirectoryInfo(@"C:\")
.EnumerateDirectories()
.Where(x => x.Attributes.HasFlag(FileAttributes.Hidden))
.Select(x => x.Name);

foreach (var file in dir)
{
Console.WriteLine(file);
}

Console.ReadLine();
}

VB.NET

Shared Sub Main(ByVal args() As String)
' dir is a collection of IEnumerable<string>
Dim dir = New DirectoryInfo("C:\")_
.EnumerateDirectories()_
.Where(Function(x) x.Attributes.HasFlag(FileAttributes.Hidden))_
.Select(Function(x) x.Name)

For Each file In dir
Console.WriteLine(file)
Next file

Console.ReadLine()
End Sub

OUTPUT

image



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Enumerate Hidden Directories in .NET 4.0"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions