티스토리 뷰
FileInfo 클래스와 DirectoryInfo 클래스
1. FileInfo클래스
파일 정보를 담을 수 있는 멤버들을 제공한다.
예를들어
Name이란 변수엔 파일이름이
FullName이라는 변수엔 디렉토리 Path를 포함한 파일 이름이,
Extension에는 확장자가,
CreationTime에는 파일이 생성된 날짜 등 각종 파일에 관련된 정보들이 들어간다.
생성자에는 파일의 FullPath가 들어간다 (당연히 디렉토리까지 포함)
FileInfo fInfo = new FileInfo("c:\\test.txt");
간단하다.
fInfo.Name에는 “test.txt”가,
fInfo.FullName에는 “c:\\test.txt” 가 들어가있겠지…
FileInfo의 모든 멤버는 다음과 같다.
Name |
Description |
Gets or sets the FileAttributes of the current FileSystemInfo. (Inherited from FileSystemInfo.) | |
Gets or sets the creation time of the current FileSystemInfo object. (Inherited from FileSystemInfo.) | |
Gets or sets the creation time, in coordinated universal time (UTC), of the current FileSystemInfo object. (Inherited from FileSystemInfo.) | |
Gets an instance of the parent directory. | |
Gets a string representing the directory's full path. | |
Gets a value indicating whether a file exists. (Overrides FileSystemInfo..::.Exists.) | |
Gets the string representing the extension part of the file. (Inherited from FileSystemInfo.) | |
Gets the full path of the directory or file. (Inherited from FileSystemInfo.) | |
Gets or sets a value that determines if the current file is read only. | |
Gets or sets the time the current file or directory was last accessed. (Inherited from FileSystemInfo.) | |
Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed. (Inherited from FileSystemInfo.) | |
Gets or sets the time when the current file or directory was last written to. (Inherited from FileSystemInfo.) | |
Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to. (Inherited from FileSystemInfo.) | |
Gets the size, in bytes, of the current file. | |
Gets the name of the file. (Overrides FileSystemInfo..::.Name.) |
아 물론 멤버변수 말고도 파일 생성/ 삭제 등을 할 수 있도록 메서드도 제공한다.
Name |
Description |
Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo. | |
Overloaded. Copies an existing file to a new file. | |
Creates a file. | |
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.) | |
Creates a StreamWriter that writes a new text file. | |
Decrypts a file that was encrypted by the current account using the Encrypt method. | |
Permanently deletes a file. (Overrides FileSystemInfo..::.Delete()()().) | |
Encrypts a file so that only the account used to encrypt the file can decrypt it. | |
Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) | |
Overloaded. Gets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileInfo object. | |
Serves as a hash function for a particular type. (Inherited from Object.) | |
Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.) | |
Sets the SerializationInfo object with the file name and additional exception information. (Inherited from FileSystemInfo.) | |
Gets the Type of the current instance. (Inherited from Object.) | |
Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.) | |
Overloaded. | |
Moves a specified file to a new location, providing the option to specify a new file name. | |
Overloaded. Opens a file with various read/write and sharing privileges. | |
Creates a read-only FileStream. | |
Creates a StreamReader with UTF8 encoding that reads from an existing text file. | |
Creates a write-only FileStream. | |
Refreshes the state of the object. (Inherited from FileSystemInfo.) | |
Overloaded. Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file, and creating a backup of the replaced file. | |
Applies access control list (ACL) entries described by a FileSecurity object to the file described by the current FileInfo object. | |
Returns the path as a string. (Overrides Object..::.ToString()()().) |
2. DirectoryInfo클래스
FileInfo에 파일과 관련된 정보가 담겨있다면 DirectoryInfo클래스에는 Directory에 관한 정보가 들어있을 것이다. FileInfo클래스와 마찬가지로 디렉토리 생성/삭제등을 할 수 있는 메소드를 제공한다.
파일과 다른점은 디렉토리인만큼 하위 파일목록과 하위 디렉토리 목록을 불러오는 메소드를 제공한다는 점이다.
리턴타입은 각각 FileInfo배열과 DirectoryInfo배열이다.
즉 다음과 같이 사용할 수 있다.
// C: 루트 디렉토리 정보를 가져옴
DirectoryInfo dInfo = new DirectoryInfo("c:\\");
// 하위 FileInfo(파일정보) 배열을 받아옴
FileInfo[] fInfoSub = dInfo.GetFiles();
// 하위 DirectoryInfo(디렉토리정보) 배열을 받아옴
DirectoryInfo[] dInfoSub = dInfo.GetDirectories();
DirectoryInfo에서 제공하는 멤버는 다음과 같다.
Name |
Description |
Gets or sets the FileAttributes of the current FileSystemInfo. (Inherited from FileSystemInfo.) | |
Gets or sets the creation time of the current FileSystemInfo object. (Inherited from FileSystemInfo.) | |
Gets or sets the creation time, in coordinated universal time (UTC), of the current FileSystemInfo object. (Inherited from FileSystemInfo.) | |
Gets a value indicating whether the directory exists. (Overrides FileSystemInfo..::.Exists.) | |
Gets the string representing the extension part of the file. (Inherited from FileSystemInfo.) | |
Gets the full path of the directory or file. (Inherited from FileSystemInfo.) | |
Gets or sets the time the current file or directory was last accessed. (Inherited from FileSystemInfo.) | |
Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed. (Inherited from FileSystemInfo.) | |
Gets or sets the time when the current file or directory was last written to. (Inherited from FileSystemInfo.) | |
Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to. (Inherited from FileSystemInfo.) | |
Gets the name of this DirectoryInfo instance. (Overrides FileSystemInfo..::.Name.) | |
Gets the parent directory of a specified subdirectory. | |
Gets the root portion of a path. |
그리고 메소드는 다음과 같다.
Name |
Description |
Overloaded. Creates a directory. | |
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.) | |
Overloaded. Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class. | |
Overloaded. Deletes a DirectoryInfo and its contents from a path. | |
Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) | |
Overloaded. Gets the access control list (ACL) entries for the current directory. | |
Overloaded. Returns the subdirectories of the current directory. | |
Overloaded. Returns a file list from the current directory. | |
Overloaded. Retrieves an array of strongly typed FileSystemInfo objects representing files and subdirectories of the current directory. | |
Serves as a hash function for a particular type. (Inherited from Object.) | |
Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.) | |
Sets the SerializationInfo object with the file name and additional exception information. (Inherited from FileSystemInfo.) | |
Gets the Type of the current instance. (Inherited from Object.) | |
Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.) | |
Overloaded. | |
Moves a DirectoryInfo instance and its contents to a new path. | |
Refreshes the state of the object. (Inherited from FileSystemInfo.) | |
Applies access control list (ACL) entries described by a DirectorySecurity object to the directory described by the current DirectoryInfo object. | |
Returns the original path that was passed by the user. (Overrides Object..::.ToString()()().) |
각각의 사용법과 내용은 참고하면 되겠다.