Bài tập - Làm việc với đường dẫn

Đã hoàn thành

Lớp dữ liệu .NET PathDirectory.GetCurrentDirectory là hai cách để xác định và soạn đường dẫn hệ thống tệp.

Trong bài tập trước, bạn đã viết một chương trình lặp qua một thư mục để tìm bất kỳ tệp sales.json nào trong đó hoặc bất kỳ thư mục con nào.

Trong bài tập này, bạn sử dụng Path lớp học và Directory.GetCurrentDirectory để cải thiện chương trình để nó tìm thấy bất tệp với phần mở rộng .json khác.

Sử dụng thư mục hiện tại và kết hợp đường dẫn

Trong mã bản Program.cs hiện tại, bạn đang chuyển qua vị trí tĩnh của lưu trữ mục. Bây giờ, chúng ta sẽ thay đổi mã đó để sử dụng Directory.GetCurrentDirectory trị thay vì chuyển tên thư mục tĩnh.

  1. Trong trình soạn thảo, hãy chèn mã sau đây ở trên dòng đầu tiên Program.cs tệp. Mã này sử dụng phương Directory.GetCurrentDirectory để có được đường dẫn cho thư mục hiện tại và lưu trữ nó trong một biến mới currentDirectory:

    var currentDirectory = Directory.GetCurrentDirectory();
    
  2. Chèn mã sau đây sau dòng bạn vừa thêm vào. Mã này sử dụng phương pháp Path.Combine để tạo đường dẫn đầy đủ đến lưu trữ thư mục và lưu trữ nó trong một biến mới storesDirectory:

    var storesDirectory = Path.Combine(currentDirectory, "stores");
    
  3. Thay thế chuỗi stores trong hàm FindFiles gọi bằng hàm biến số storesDirectory:

    var salesFiles = FindFiles(storesDirectory);
    

    Phần trên cùng của tệp giờ đây sẽ trông giống như đoạn mã sau đây:

    var currentDirectory = Directory.GetCurrentDirectory();
    var storesDirectory = Path.Combine(currentDirectory, "stores");
    var salesFiles = FindFiles(storesDirectory);
    
    foreach (var file in salesFiles)
    {
        Console.WriteLine(file);
    }
    
  4. Nhấn Ctrl+S (hoặc Cmd+S macOS) để lưu tệp.

  5. Chạy chương trình từ dòng lệnh:

    dotnet run
    
  6. Chương trình sẽ hiển thị đầu ra sau đây:

    /home/username/dotnet-files/stores/sales.json  
    /home/username/dotnet-files/stores/201/sales.json  
    /home/username/dotnet-files/stores/202/sales.json  
    /home/username/dotnet-files/stores/203/sales.json  
    /home/username/dotnet-files/stores/204/sales.json  
    

    Lưu ý rằng tên tệp được trả về bao gồm đường dẫn hệ thống đầy đủ. Đường dẫn này được bao gồm vì Directory.GetCurrentDirectory phương pháp trả về đường dẫn đầy đủ đến vị trí hiện tại.

Tìm tất cả .json tệp

Thay vì chỉ tìm kiếm tệp sales.json, chương trình cần tìm kiếm bất kỳ tệp nào có phần mở rộng .json khác. Để thực hiện điều đó, bạn có thể sử dụng Path.GetExtension để kiểm tra phần mở rộng cho từng tệp.

  1. Trong vòng lặp foreach lặp qua foundFiles, hãy chèn dòng mã sau đây phía trên câu lệnh if để xác định một biến số extension. Mã này sử dụng phương Path.GetExtension để có được phần mở rộng của mỗi tệp.

        var extension = Path.GetExtension(file);
    
  2. Thay đổi if lệnh để trông giống như dòng mã sau đây. Điều khoản này kiểm tra xem phần mở rộng của tệp có bằng .json.

           if (extension == ".json")
    

    Vòng foreach vòng lặp sẽ trông giống như mã sau đây:

    foreach (var file in foundFiles)
    {
        var extension = Path.GetExtension(file);
        if (extension == ".json")
        {
            salesFiles.Add(file);
        }
    }
    
  3. Nhấn Ctrl+S / Cmd+S để lưu tệp.

  4. Chạy chương trình từ dòng lệnh:

    dotnet run
    

    Đầu ra bây giờ hiển thị tất .json tập tin trong mỗi thư mục ID lưu trữ:

    /home/username/dotnet-files/stores/sales.json  
    /home/username/dotnet-files/stores/201/sales.json
    /home/username/dotnet-files/stores/201/salestotals.json  
    /home/username/dotnet-files/stores/202/sales.json
    /home/username/dotnet-files/stores/202/salestotals.json    
    /home/username/dotnet-files/stores/203/sales.json  
    /home/username/dotnet-files/stores/203/salestotals.json  
    /home/username/dotnet-files/stores/204/sales.json  
    /home/username/dotnet-files/stores/204/salestotals.json  
    

Tuyệt! Bạn đã sử dụng lớp Path và phương pháp Directory.GetCurrentDirectory để làm cho chương trình mạnh mẽ hơn nhiều. Trong đơn vị tiếp theo, bạn sẽ tìm hiểu cách tạo thư mục và di chuyển tệp giữa các vị trí.

Bạn gặp sự cố?

Nếu bạn gặp sự cố ở bất kỳ điểm nào trong bài tập này, dưới đây là mã đã hoàn tất. Loại bỏ mọi thứ trong Program.cs và thay thế bằng giải pháp này:

var currentDirectory = Directory.GetCurrentDirectory();
var storesDirectory = Path.Combine(currentDirectory, "stores");

var salesFiles = FindFiles(storesDirectory);
    
foreach (var file in salesFiles)
{
    Console.WriteLine(file);
}

IEnumerable<string> FindFiles(string folderName)
{
    List<string> salesFiles = new List<string>();

    var foundFiles = Directory.EnumerateFiles(folderName, "*", SearchOption.AllDirectories);

    foreach (var file in foundFiles)
    {
        var extension = Path.GetExtension(file);
        if (extension == ".json")
        {
            salesFiles.Add(file);
        }
    }

    return salesFiles;
}