티스토리 뷰


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

Attributes

Gets or sets the FileAttributes of the current FileSystemInfo. (Inherited from FileSystemInfo.)

CreationTime

Gets or sets the creation time of the current FileSystemInfo object. (Inherited from FileSystemInfo.)

CreationTimeUtc

Gets or sets the creation time, in coordinated universal time (UTC), of the current FileSystemInfo object. (Inherited from FileSystemInfo.)

Directory

Gets an instance of the parent directory.

DirectoryName

Gets a string representing the directory's full path.

Exists

Gets a value indicating whether a file exists. (Overrides FileSystemInfo..::.Exists.)

Extension

Gets the string representing the extension part of the file. (Inherited from FileSystemInfo.)

FullName

Gets the full path of the directory or file. (Inherited from FileSystemInfo.)

IsReadOnly

Gets or sets a value that determines if the current file is read only.

LastAccessTime

Gets or sets the time the current file or directory was last accessed. (Inherited from FileSystemInfo.)

LastAccessTimeUtc

Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed. (Inherited from FileSystemInfo.)

LastWriteTime

Gets or sets the time when the current file or directory was last written to. (Inherited from FileSystemInfo.)

LastWriteTimeUtc

Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to. (Inherited from FileSystemInfo.)

Length

Gets the size, in bytes, of the current file.

Name

Gets the name of the file. (Overrides FileSystemInfo..::.Name.)

 

아 물론 멤버변수 말고도 파일 생성/ 삭제 등을 할 수 있도록 메서드도 제공한다.

Name

Description

AppendText

Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.

CopyTo

Overloaded. Copies an existing file to a new file.

Create

Creates a file.

CreateObjRef

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)

CreateText

Creates a StreamWriter that writes a new text file.

Decrypt

Decrypts a file that was encrypted by the current account using the Encrypt method.

Delete

Permanently deletes a file. (Overrides FileSystemInfo..::.Delete()()().)

Encrypt

Encrypts a file so that only the account used to encrypt the file can decrypt it.

Equals

Determines whether the specified Object is equal to the current Object. (Inherited from Object.)

Finalize

Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)

GetAccessControl

Overloaded. Gets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileInfo object.

GetHashCode

Serves as a hash function for a particular type. (Inherited from Object.)

GetLifetimeService

Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)

GetObjectData

Sets the SerializationInfo object with the file name and additional exception information. (Inherited from FileSystemInfo.)

GetType

Gets the Type of the current instance. (Inherited from Object.)

InitializeLifetimeService

Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)

MemberwiseClone

Overloaded.

MoveTo

Moves a specified file to a new location, providing the option to specify a new file name.

Open

Overloaded. Opens a file with various read/write and sharing privileges.

OpenRead

Creates a read-only FileStream.

OpenText

Creates a StreamReader with UTF8 encoding that reads from an existing text file.

OpenWrite

Creates a write-only FileStream.

Refresh

Refreshes the state of the object. (Inherited from FileSystemInfo.)

Replace

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.

SetAccessControl

Applies access control list (ACL) entries described by a FileSecurity object to the file described by the current FileInfo object.

ToString

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

Attributes

Gets or sets the FileAttributes of the current FileSystemInfo. (Inherited from FileSystemInfo.)

CreationTime

Gets or sets the creation time of the current FileSystemInfo object. (Inherited from FileSystemInfo.)

CreationTimeUtc

Gets or sets the creation time, in coordinated universal time (UTC), of the current FileSystemInfo object. (Inherited from FileSystemInfo.)

Exists

Gets a value indicating whether the directory exists. (Overrides FileSystemInfo..::.Exists.)

Extension

Gets the string representing the extension part of the file. (Inherited from FileSystemInfo.)

FullName

Gets the full path of the directory or file. (Inherited from FileSystemInfo.)

LastAccessTime

Gets or sets the time the current file or directory was last accessed. (Inherited from FileSystemInfo.)

LastAccessTimeUtc

Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed. (Inherited from FileSystemInfo.)

LastWriteTime

Gets or sets the time when the current file or directory was last written to. (Inherited from FileSystemInfo.)

LastWriteTimeUtc

Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was last written to. (Inherited from FileSystemInfo.)

Name

Gets the name of this DirectoryInfo instance. (Overrides FileSystemInfo..::.Name.)

Parent

Gets the parent directory of a specified subdirectory.

Root

Gets the root portion of a path.

 

그리고 메소드는 다음과 같다.

Name

Description

Create

Overloaded. Creates a directory.

CreateObjRef

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)

CreateSubdirectory

Overloaded. Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.

Delete

Overloaded. Deletes a DirectoryInfo and its contents from a path.

Equals

Determines whether the specified Object is equal to the current Object. (Inherited from Object.)

Finalize

Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)

GetAccessControl

Overloaded. Gets the access control list (ACL) entries for the current directory.

GetDirectories

Overloaded. Returns the subdirectories of the current directory.

GetFiles

Overloaded. Returns a file list from the current directory.

GetFileSystemInfos

Overloaded. Retrieves an array of strongly typed FileSystemInfo objects representing files and subdirectories of the current directory.

GetHashCode

Serves as a hash function for a particular type. (Inherited from Object.)

GetLifetimeService

Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)

GetObjectData

Sets the SerializationInfo object with the file name and additional exception information. (Inherited from FileSystemInfo.)

GetType

Gets the Type of the current instance. (Inherited from Object.)

InitializeLifetimeService

Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)

MemberwiseClone

Overloaded.

MoveTo

Moves a DirectoryInfo instance and its contents to a new path.

Refresh

Refreshes the state of the object. (Inherited from FileSystemInfo.)

SetAccessControl

Applies access control list (ACL) entries described by a DirectorySecurity object to the directory described by the current DirectoryInfo object.

ToString

Returns the original path that was passed by the user. (Overrides Object..::.ToString()()().)

 

각각의 사용법과 내용은 참고하면 되겠다.

반응형
댓글