I.Introduction
FileInfo Và DirectoryInfo là thành phần rất quan trọng thuộc namespace System.IO, giúp cho lập trình viên dể dàng làm việc với các thao tác trên file và folder.
II.FileInfo
FileInfo giúp lập trình viên có thể lấy thông tin của File
- Thuộc tính
| S.No | Property | Description |
| 1 | CreationTime | This property returns the creation of the file date and time. |
| 2 | CreationTimeUtc | It returns the creation of the file in Universal time |
| 3 | Directory | It returns the parent directory name |
| 4 | DirectoryInfo | It returns the full directory path |
| 5 | Exists | It returns whether the file exists or not as Boolean result. |
| 6 | Extension | It returns the type of the file in the extension name |
| 7 | FullName | It returns the full name of the file from the root directory. |
| 8 | IsReadOnly | It returns the file is read only or not in the Boolean result. |
| 9 | Name | It returns the name of the file |
| 10 | LastAccessTime | It returns the date and time of the last access time |
| 11 | LastAccessTimeUtc | It returns the last accessing date and time in universal format. |
| 12 | LastWriteTime | It returns the last file saving date and time |
| 13 | LastWriteTimeUtc | It returns the last write in the universal format |
| 14 | Length | It returns the size of the file content in the bytes format. |
2.Những Phương Thức Của Lớp FileInfo:
FileInfo cung cấp nhiều phương thức giúp lập trình viên thao tác trên File một cách dễ dàng
CreateText: Dùng để viết đoạn Text vào File
1: FileInfo fileInfo = new FileInfo(@"E:\Test.txt");
2: StreamWriter writer = fileInfo.CreateText();
3: writer.WriteLine("---------------------");
4: writer.WriteLine("Sample File Creation");
5: writer.WriteLine("Pham Nguyen");
6: writer.Close()
Trong ví dụ phía trên chúng ta đã tạo mới một file và thêm nội dung cho file đó
OpenText: Đọc nội dung từ một file
1: FileInfo fileInfo = new FileInfo(@"E:\Test.txt");
2: StreamReader Reader = fileInfo.OpenText();
3: Console.WriteLine(Reader.ReadToEnd());
4: Reader.Close();
Create: Tạo mới một File
1: FileInfo fileInfo = new FileInfo(@"E:\Test.txt");
2: FileStream fileStream = fileInfo.Create();
3: fileStream.Close()
Delete: Xóa File
1: FileInfo fileInfo = new FileInfo(@"E:\Test.txt");
2: if(fileInfo.Exists)
3: {
4: fileInfo.Delete();
5: }
6: fileInfo.Close();
CopyTo và MoveTo:
- CopyTo: dùng để copy một file đã có vào một file khác
- MoveTo: dùng để di chuyển file đến một nơi khác
III.DirectoryInfo
Là lớp giúp lập trình viên có thể lấy thông tin về Directory và các thao tác trên Directory như thêm, xóa….
1.Thuộc Tính:
| S.No | Property | Description |
| 1 | CreationTime | This property returns the creation of the file date and time. |
| 2 | CreationTimeUtc | It returns the creation of the file in Universal time |
| 3 | Root | It returns the Root directory. |
| 4 | Parent | It returns the Parent directory of the current directory |
| 5 | Exists | It returns whether the file exists or not as Boolean result. |
| 6 | Extension | It returns the type of the file in the extension name |
| 7 | FullName | It returns the full name of the file from the root directory. |
| 8 | IsReadOnly | It returns the file is read only or not in the Boolean result. |
| 9 | Name | It returns the name of the directory. |
| 10 | LastAccessTime | It returns the date and time of the last access time |
| 11 | LastAccessTimeUtc | It returns the last accessing date and time in universal format. |
| 12 | LastWriteTime | It returns the last file saving date and time |
| 13 | LastWriteTimeUtc | It returns the last write in the universal format |
| 14 | Length | It returns the size of the file content in the bytes format. |
2.Những Phương Thức Của Lớp DirectoryInfo:
Create: phương thức này dùng để tạo mới một Directory.
1: DirectoryInfo DirInfo = DirectoryInfo(@"e:\Sample");
2: if(DirInfo.Exists)
3: {
4: Console.WriteLine("Đã Tồn Tại");
5: }
6: DirInfo.Create();
7: Console.WriteLine("Tạo Thư Mục Thành Công");
Delete: Phương thức này dùng để Xóa Directory
1: DirectoryInfo DirInfo = new DirectoryInfo(@"E:\Sample");
2: if(DirInfo.Exists)
3: {
4: DirInfo.Delete();
5: Console.WriteLine("Xóa Thư Mục Thành Công") ;
6: }
7: else Console.WriteLine("Thư Mục ko tồn tại") ;
8:
GetFiles: Lấy thông tin File từ Folder
1: DirectoryInfo DirInfo = new DirectoryInfo(@"C:\Windows");
2: if(DivInfo.Exists)
3: {
4: FileInfo[] fileInfo = DirInfo.GetFiles();
5: foreach(object fileName in fileInfo)
6: {
7: Console.WriteLine(fileName.ToString());
8: }
9: }
GetDirectories: Lấy những thư mục con của thư mục hiện tại
1: DirectoryInfo dirInfo = new DirectoryInfo(@"C:\windows");
2: if(DirInfo.Exists)
3: {
4: DirectoryInfo[] Dir = DirInfo.GetDirectories();
5: foreach(object DirName in Dir)
6: {
7: Console.WriteLine(DirName.ToString());
8: }
9: }
10: else
11: Console.WriteLine("Không tồn tại");
MoveTo : Phương thưc này dùng để di chuyển Folder đến một vị trí khác
Life Run On Code !