CSharp - Làm thế nào để lấy đường dẫn các tập tin trong thư mục và thư mục con? (How to get list files form directory and sub directory in CSharp?)


Bài viết này chúng ta sẽ tìm hiểu cách lấy đường dẫn(path) của một folder được xác định và bao gồm các tập tin trong các thư mục con(sub) của nó trong ngôn ngữ lập trình C#





Đầu tiên tôi có một thư mực(folder) với các tập tin(file) như sau:


Thực thi đoạn code sau để lấy đường dẫn(path) các tập tin.

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

namespace GetListFileInDirectoryAndSub
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] filePaths = Directory.GetFiles(@"D:\Z-Test\Directory\", "*.*", SearchOption.AllDirectories);

            foreach (string filePath in filePaths)
            {

                System.Console.WriteLine("- " + filePath);

            }

            Console.ReadLine();

        }
    }
}

Kết quả sau khi được thì ta sẽ được:














No comments:

Post a Comment