Đỉnh NGUYỄN

life's a journey not a destination

Liệt kê thư mục ẩn trong .NET 4.0

Leave a comment


Dùng phương thức DirectoryInfo.EnumerateDirectories liệt kê tên các tập tin ẩn trong 1 phân vùng hay thư mục

using System;
// IO, Linq namespace
using System.IO;
using System.Linq;

namespace HiddenDirectories
{
    class Program
    {
        static void Main(string[] args)
        {
            // dir is a collection of IEnummerable<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();
        }
    }
}

Kết quả:

image

Advertisements

Author: dinhnn

Senior software developer, a technical leader. You can be reached at via email to dinhnguyenngoc@gmail.com, via my blog at dinhnguyenngoc.wordpress.com, and on Twitter @dinhnguyenngoc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s