VB.NET 2008/VB - 파일 액세스

파일 액세스 - 파일/디렉토리 만들기, 삭제 및 이동

본클라쓰 2011. 12. 23. 16:10

특정 패턴의 파일을 디렉토리에 복사

 

My.Computer.FileSystem.GetFiles 메서드는 파일의 경로 이름을 나타내는 읽기 전용 문자열 컬렉션을 반환한다. Wildcards 매개 변수를 사용하여 특정 패턴을 지정할 수 있다. 일치하는 파일이 없으면 빈 컬렉션이 반환된다.

 

My.Computer.FileSystem.CopyFile 메서드를 사용하면 파일을 디렉토리에 복사할 수 있다.

 

For Each foundFile As String In My.Computer.FileSystem.GetFiles( _

    My.Computer.FileSystem.SpecialDirectories.MyDocuments, FileIO.SearchOption.SearchTopLevelOnly, "*.rtf")

 

    My.Computer.FileSystem.CopyFile(foundFile, "C:\testdirectory\" & foundFile)

Next

 

 

 

동일한 디렉터리에 파일의 복사본 만들기

 

My.Computer.FileSystem.CopyFile 메서드를 사용하여 파일을 복사한다. 이 메서드의 매개 변수를 사용하면 기존 파일을 덮어쓰고, 파일 이름을 바꾸고, 작업 진행 상태를 표시하고, 사용자가 작업을 취소하도록 허용할 수 있다.

 

My.Computer.FileSystem.CopyFile("C:\TestFolder\test.txt", _
"C:\TestFolder\test2.txt", Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)

 

 

파일 만들기

 

File 클래스의 Create 메서드를 사용하여 지정된 경로에 빈 텍스트 파일을 만든다.

 

Dim file As System.IO.FileStream
file = System.IO.File.Create("c:\test.txt")

 

파일 삭제

 

My.Computer.FileSystem 개체의 DeleteFile 메서드를 사용하면 파일을 삭제할 수 있다. 이 메서드에서는 삭제된 파일을 휴지통으로 보낼 것인지 여부, 파일 삭제를 사용자에게 확인할 것인지 여부, 사용자가 작업을 취소했을 때 수행할 작업 등의 옵션을 제공한다.

 

My.Computer.FileSystem.DeleteFile("C:\test.txt", _
        FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)

 

 

 

파일 이동

 

My.Computer.FileSystem.MoveFile 메서드를 사용하면 파일을 다른 폴더로 이동할 수 있다. 대상 구조가 없으면 새로 만들어진다.

 

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt""C:\TestDir2\test.txt")

 

 

파일 이름 바꾸기

 

My.Computer.FileSystem 개체의 RenameFile 메서드를 사용하여 현재 위치, 파일 이름 및 새 파일 이름을 지정함으로써 파일 이름을 바꾼다. 이 메서드는 파일을 이동하는 데 사용할 수 없으며, 파일을 이동하고 이름을 바꾸려면 MoveFile 메서드를 사용해야 한다.

 

My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")

 

 

 

디렉토리 이름 바꾸기

 

My.computer.FileSystem 개체의 RenameDirectory 메서드를 사용하면 디렉토리의 현재 위치와 이름을 디렉터리의 새 이름과 함께 지정하여 디렉터리 이름을 바꿀 수 있다.

 

My.Computer.FileSystem.RenameDirectory("C:MyDocuments\Test", "SecondTest")

 

 

 

디렉토리 만들기

 

My.Computer.FileSystem 개체의 CreateDirectory 메서드를 사용하여 디렉토리르 만든다.

 

My.Computer.FileSystem.CreateDirectory("C:\Documents and Settings\All Users\Documents\NewDirectory")

 

 

디렉토리 삭제

 

My.Computer.FileSystem 개체의 DeleteDirectory 메서드를 사용하여 디렉토리를 삭제한다. 디렉터리의 내용 삭제 엽, 삭제된 디렉토리를 휴지통으로 보낼지 여부, 삭제 진행률 표시 여부 등을 선택할 수 있다.

 

My.Computer.FileSystem.DeleteDirectory("C:\OldDirectory", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)

 

 

 

파일의 경로의 구문 분석

 

My.Computer.FileSystem 개체는 파일 경로를 구문 분석할 때 유용한 여러 가지 메서드를 제공한다.

CombinePath 메서드는 두 개의 경로를 사용하여 적절한 형식으로 조합된 경로를 반환하고, GetParentPath 메서드는 제공된 경로의 부모에 대한 절대 경로를 반환, GetFileInfo 메서드는 이름 및 경로 등과 같은 파일 속성을 확인하기 위해 쿼리할 수 있는 FileInfo 개체를 반환한다.

 

Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo("C:\TestFolder1\test1.txt")
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
Dim fileName As String = testFile.Name
MsgBox(fileName)