Share via


방법: Visual Basic에서 디렉터리의 모든 파일 삭제

업데이트: 2007년 11월

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

폴더의 모든 파일을 삭제하려면

  1. My.Computer.FileSystem.GetFiles 메서드를 사용하여 디렉터리의 파일을 나타내는 문자열 컬렉션을 반환합니다.

  2. DeleteFile 메서드에 For…Each 루프를 사용하여 각 파일을 차례로 삭제합니다.

    다음 예제에서는 내 문서 폴더의 모든 파일을 삭제합니다.

    For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
        My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
        FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    
        My.Computer.FileSystem.DeleteFile(foundFile, _
            FileIO.UIOption.AllDialogs, _
            FileIO.RecycleOption.DeletePermanently)
    Next
    

강력한 프로그래밍

다음 조건에서 예외가 발생합니다.

참고 항목

작업

방법: Visual Basic에서 파일 삭제

방법: Visual Basic에서 디렉터리 삭제

방법: Visual Basic에서 파일 이름 바꾸기

방법: Visual Basic에서 파일의 절대 경로 확인

참조

My.Computer.FileSystem 개체

My.Computer.FileSystem.DeleteFile 메서드

RecycleOption 열거형

UICancelOption 열거형