Wednesday, July 11, 2018

Làm việc với tệp và thư mục

Lớp Directory chứa đựng rất nhiều phương thức tĩnh cho phép tạo, dịch chuyển…thư mục. Tất cả các phương thức của lớp Directory là các phương thức tĩnh. Bởi vậy chúng ta có thể gọi các phương thưc này mà không cần tạo ra thể hiện của ...

Lớp Directory chứa đựng rất nhiều phương thức tĩnh cho phép tạo, dịch chuyển…thư mục. Tất cả các phương thức của lớp Directory là các phương thức tĩnh. Bởi vậy chúng ta có thể gọi các phương thưc này mà không cần tạo ra thể hiện của lớp.
Lớp DirectoryInfo giống như lớp Directory, các phương thức của lớp được thực hiện qua thể hiện của lớp. Lớp DirectoryInfo xuất phát từ lớp FileSystemInfo. Lớp FileSystemInfo có chứa một số các thuộc tính và phương thức về file và thư mục.
Sau đây là một số phương thức chính của lớp Directory
Phương thứcCách sử dụng
CreateDirectory( )Creates all directories and subdirectories specified by its path parameter
GetCreationTime( )Returns and sets the time the specified directory was created.
GetDirectories( )Gets named directories.
GetLogicalDrives( )Returns the names of all the logical drives in the form <drive>:.
GetFiles( )Returns the names of files matching a pattern.
GetParent( )Returns the parent directory for the specified path.
Move( )Moves a directory and its contents to a specified path.
Sau đây là một số phương thức và thuộc tính chính của lớp DirectoryInfo
Phương thức và thuộc tínhCách sử dụng
AttributesInherits from FileSystemInfo; gets or sets the attributes of the current file.
CreationTimeInherits from FileSystemInfo; gets or sets the creation time of the current file.
ExistsPublic property Boolean value, which is True if the directory exists.
ExtensionPublic property inherited from FileSystemInfo; i.e., the file extension.
FullNamePublic property inherited from FileSystemInfo; i.e., the full path of the file or directory.
LastAccessTimePublic property inherited from FileSystemInfo; gets or sets the last access time.
LastWriteTimePublic property inherited from FileSystemInfo; gets or sets the time when the current file or directory was last written to.
NamePublic property name of this instance of DirectoryInfo.
ParentPublic property parent directory of the specified directory.
RootPublic property root portion of the path.
Create( )Public method that creates a directory.
CreateSubdirectory( )Public method that creates a subdirectory on the specified path.
Delete( )Public method that deletes a DirectoryInfo and its contents from the path.
GetDirectories( )Public method that returns a DirectoryInfo array with subdirectories.
GetFiles( )Public method that returns a list of files in the directory.
GetFileSystemIn fos( )Public method that retrieves an array of FileSystemInfo objects.
MoveTo( )Public method that moves a DirectoryInfo and its contents to a new path.
Refresh( )Public method inherited from FileSystemInfo; refreshes the state of the object.
Để mở một tập tin trên đĩa cho việc đọc và viết tập tin văn bản, chúng ta cần phải sử dụng cả hai lớp File và FileInfo.
Làm thế nào để chúng ta có thể biết được khi nào sử dụng lớp File chính xác hơn là sử dụng lớp FileInfo nếu chúng cùng chứa những phương thức tương tự với nhau. Thật ra hai lớp này có nhiều sự khác biệt. Lớp File chứa tất cả các phương thức tĩnh, thêm vào đó lớp File tự động kiểm tra permission trên một tập tin. Trong khi đó nếu muốn dùng lớp FileInfo thì phải tạo thể hiện của lớp này. Nếu muốn mở một tập tin chỉ một lần thì tốt nhất là sử dụng lớp File, còn nếu chúng ta tổ chức việc sử dụng tập tin nhiều lần bên trong chương trình, tốt nhất là ta dùng lớp FileInfo. Hoặc nếu không chắc chắn cách sử dụng thì chúng ta có thể sử dụng lớp FileInfo.
Sau đây là một số phương thức chính của lớp file
Phương thứcCách sử dụng
AppendText( )Creates a StreamWriter that appends text to the specified file.
Copy( )Copies an existing file to a new file.
Create( )Creates a file in the specified path.
CreateText( )Creates a StreamWriter that writes a new text file to the specified file.
Delete( )Deletes the specified file.
Exists( )Returns true if the specified file exists.
GetAttributes(), SetAttributes( )Gets and sets the FileAttributes of the specified file.
GetCreationTime( ), SetCreationTime( )Returns and sets the creation date and time of the file.
GetLastAccessTime( ), SetLastAccessTime( )Returns and sets the last time the specified file was accessed.
GetLastWriteTime( ), SetLastWriteTime( )Returns and sets the last time the specified file was written to.
Move( )Moves a file to a new location; can be used to rename a file.
OpenRead( )Public static method that opens a FileStream on the file.
OpenWrite( )Creates a read/write Stream on the specified path.
Sau đây là một số phương thức và thuộc tính chính của lớp fileInfo
Phương thức và thuộc tínhCách sử dụng
Attributes( )Inherits from FileSystemInfo; gets or sets the attributes of the current file.
CreationTimeInherits from FileSystemInfo; gets or sets the creation time of the current file.
DirectoryPublic property that gets an instance of the parent directory.
ExistsPublic property Boolean value that is True if the directory exists.
ExtensionPublic property inherited from FileSystemInfo; i.e., the file extension.
FullNamePublic property inherited from FileSystemInfo; i.e., the full path of the file or directory.
LastAccessTimePublic property inherited from FileSystemInfo; gets or sets the last access time.
LastWriteTimePublic property inherited from FileSystemInfo; gets or sets the time when the current file or directory was last written to.
LengthPublic property that gets the size of the current file.
NamePublic property Name of this DirectoryInfo instance.
AppendText( )Public method that creates a StreamWriter that appends text to a file.
CopyTo( )Public method that copies an existing file to a new file.
Create( )Public method that creates a new file.
Delete( )Public method that permanently deletes a file.
MoveTo( )Public method to move a file to a new location; can be used to rename a file.
Open( )Public method that opens a file with various read/write and sharing privileges.
OpenRead( )Public method that creates a read-only FileStream.
OpenText()Public method that creates a StreamReader that reads from an existing text file.
OpenWrite( )Public method that creates a write-only FileStream.

No comments:

Post a Comment