Phần này tìm hiểu về hệ thống File, thư mục, ổ đĩa làm việc với .NET bằng C#. Như đọc thông tin ổ đĩa, đóng - mở file, copy file, xóa file, ghi dữ liệu ra file ... Show Đọc thông tin ổ đĩa - DriveInfoThư viện .NET cung cấp lớp Các thuộc tính của lớp
using System; using System.IO; namespace CS016_FilesDirectories { public class GetDriveInfomation { /// <summary> /// In các thông tin ổ đĩa trong máy /// </summary> public static void GetDrivesInfo () { DriveInfo[] allDrives = DriveInfo.GetDrives (); foreach (DriveInfo d in allDrives) { Console.WriteLine ("Drive {0}", d.Name); Console.WriteLine (" Drive type: {0}", d.DriveType); if (d.IsReady == true) { Console.WriteLine (" Volume label: {0}", d.VolumeLabel); Console.WriteLine (" File system: {0}", d.DriveFormat); Console.WriteLine (" Available space to current user:{0, 15} bytes", d.AvailableFreeSpace); Console.WriteLine (" Total available space: {0, 15} bytes", d.TotalFreeSpace); Console.WriteLine (" Total size of drive: {0, 15} bytes ", d.TotalSize); } } } } } Khi áp dụng chạy: GetDriveInfomation.GetDrivesInfo(); Kết quả in ra là các thông tin về các ổ đĩa trên máy: Drive / Drive type: Fixed Volume label: / File system: apfs Available space to current user: 222470746112 bytes Total available space: 224261296128 bytes Total size of drive: 499455598592 bytes Drive /dev Drive type: Ram Volume label: /dev File system: devfs Available space to current user: 0 bytes Total available space: 0 bytes Total size of drive: 193536 bytes Lớp Path - Hỗ trợ làm việc với đường dẫnĐể hỗ trợ quản lý, tạo các đường dẫn đến file, thư mục - nhất là hỗ trợ cross-platform thì lớp tĩnh
Để lấy đường dẫn đến một số thư mục đặc biệt của hệ thống, có thể dùng phương thức var path_mydoc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); Lớp File.WriteAllTextHàm này tạo ra file mới (nếu đã có file bị ghi đè), ghi vào nó một nội dung text, rồi đóng file luôn. Ví dụ, lưu vào file test.txt một nội dung, file đó ở đường dẫn đến thư mục MyDocument của hệ thống. static void testWriteAllText () { var filename = "test.txt"; string contentfile = "Xin chào! xuanthulab.net"; // Lấy thư mục Document của User trên hệ thống var directory_mydoc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var fullpath = Path.Combine(directory_mydoc, filename); File.WriteAllText (filename, contentfile); Console.WriteLine ($"File lưu tại {directory_mydoc}{Path.DirectorySeparatorChar}{filename}"); } Với static void testAppendAllText() { var filename = "test.txt"; string contentfile = "\nXin chào! xuanthulab.net - " + DateTime.Now.ToString (); var directory_mydoc = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); var fullpath = Path.Combine (directory_mydoc, filename); if (File.Exists (fullpath)) { // File đã tồn tại - nối thêm nội dung File.AppendAllText (fullpath, contentfile); } else { // tạo mới vì chưa tồn tại file File.WriteAllText (fullpath, contentfile); } // Đọc nội dung File Console.WriteLine (fullpath); string s = File.ReadAllText (fullpath); Console.WriteLine (s); } File.ReadAllTextHàm này thì đọc nội dung file. string s = File.ReadAllText(fullpath); Console.Write(s); Ngoài ra bạn cũng có thể sử dụng các phương thức
Làm việc với lớp DirectoryLớp
Ví dụ lấy tất cả các thư mục, file trong một thư mục var directory_mydoc = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); String[] files = System.IO.Directory.GetFiles(directory_mydoc); String[] directories = System.IO.Directory.GetDirectories(directory_mydoc); foreach (var file in files) { Console.WriteLine(file); } foreach(var directory in directories) { Console.WriteLine(directory); } Ví dụ đệ quy liệt kê tất cả các file, thư mục con trong một thư mục static void ListFileDirectory(string path) { String[] directories = System.IO.Directory.GetDirectories(path); String[] files = System.IO.Directory.GetFiles(path); foreach (var file in files) { Console.WriteLine(file); } foreach (var directory in directories) { Console.WriteLine(directory); ListFileDirectory(directory); // Đệ quy } } Chạy thử var directory_mydoc = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); ListFileDirectory(directory_mydoc); Tham khảo mã nguồn CS016_FilesDirectories(git) hoặc tải về ex016 |