Home > C# > Expansion DirectoryInfo.GetFiles() method

Expansion DirectoryInfo.GetFiles() method


Function DirectoryInfo.GetFiles() hỗ trợ khá nhiều overload tuy nhiên vẫn khá thiếu sót khi search file với overload GetFile(string parttern) thì chỉ trả về 1 loại file. Việc combine file extension là điều không thể.
Ví dụ, bạn có thể get tất cả các file txt bằng method _directioryInfo.GetFiles(“*.txt”), tuy nhiên sẽ bó cái tay nếu muốn sử dụng _directoryInfo.GetFiles(“*.txt|*.doc”) chẳng hạn, đều này là Impossible ^^ ./

Đều đầu tiên bạn nghĩ đến có thể sẽ là tại sao ta ko inherite class DirectoryInfo và override lại method GetFiles() để có thể get được nhiều loại file hơn, tuy nhiên, điều này là không thể bởi class DirectoryInfo là sealed class !

Dường như đã bó tay và chấp nhận cách làm thủ công là get 2 loại file riêng biệt vào 2 array và combine lại với nhau thì bỗng “cái khó ló cái khôn” :) Chợt phát hiện ra rằng ở .Net 3.0 đã hỗ trợ 1 phương pháp mở rộng chức năng của một object bất kỳ bằng cách sử dụng Extension.

Lớp Extension là một lớp tĩnh, định nghĩa các method mở rộng cho từng đối tượng, được triệu gọi bằng “phương thức cú pháp” (method syntax), hiểu một cách đơn giản, nếu ta định nghĩa 1 method là extension lên 1 đối tượng thì đối tượng đó sẽ có thêm 1 overload nữa là cái mà ta vừa định nghĩa ra.

Xem một ví dụ đơn giản như sau :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Features
{
    public static class Extensions
    {
        public static int ToInt32(this string s)
        {
            return Int32.Parse(s);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string strTest = "1";
            Console.WriteLine(strTest.ToInt32()); //Will print 1
        }
    }
}

Focus vào cú pháp :

(this string s)

Điều này có nghĩa là tất các các biến có kiểu string đều có method này, like : strTest.ToInt32();

Quay trở lại với DirectoryInfo.GetFiles(), dễ thấy ta sẽ cần một overload của method GetFiles nhận vào tham số là một mảng các file extension và trả về mảng FileInfo[]

public static FileInfo[] GetFiles(this DirectoryInfo directoryInfo, string[] exts)
        {
            if (directoryInfo == null)
                return null;

            var finfo = new FileInfo[exts.Length][];
            int i = 0;

            foreach (string e in exts)
            {
                /* get files from the directory as a files collection */
                finfo[i++] = directoryInfo.GetFiles(e);
            }

            int tlength = 0;

            for (i = 0; i < finfo.Length; i++)
            {
                tlength += finfo[i].Length;
            }

            var res = new FileInfo[tlength];
            int j = 0;
            int rindex = 0;

            for (i = 0; i < finfo.Length; i++)
            {
                for (j = 0; j < finfo[i].Length; j++)
                {
                    res[rindex++] = finfo[i][j];
                }
            }

            return res;
        }

Method GetFiles(this DirectoryInfo directoryInfo, string[] ext) bây giờ đã trở thành một overload của object DirectoryInfo :

_dirSource.GetFiles(new[] {"*.INC", "*.ASP"});

Categories: C#
  1. No comments yet.
  1. No trackbacks yet.

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 )

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

Follow

Get every new post delivered to your Inbox.

Join 39 other followers