Bài tập - Làm việc với đường dẫn
Lớp dữ liệu .NET Path và Directory.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.
Trong trình soạn thảo, hãy chèn mã sau đây ở trên dòng đầu tiên
Program.cstệp. Mã này sử dụng phươngDirectory.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ớicurrentDirectory:var currentDirectory = Directory.GetCurrentDirectory();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ớistoresDirectory:var storesDirectory = Path.Combine(currentDirectory, "stores");Thay thế chuỗi
storestrong hàmFindFilesgọ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); }Nhấn Ctrl+S (hoặc Cmd+S macOS) để lưu tệp.
Chạy chương trình từ dòng lệnh:
dotnet runChươ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.jsonLư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.GetCurrentDirectoryphươ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.
Trong vòng lặp
foreachlặp quafoundFiles, hãy chèn dòng mã sau đây phía trên câu lệnhifđể xác định một biến sốextension. Mã này sử dụng phươngPath.GetExtensionđể có được phần mở rộng của mỗi tệp.var extension = Path.GetExtension(file);Thay đổi
iflệ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
foreachvò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); } }Nhấn Ctrl+S / Cmd+S để lưu tệp.
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;
}