Udostępnij za pośrednictwem


How to: Move the Contents of a Directory in Visual Basic

Use the GetFiles method to get the list of files in a folder, and use the MoveFile method to move files between directories.

If the target structure does not exist when MoveFile is called, it will be created.

To move the contents of a directory

  • Use the GetFiles method to get the list of files in the directory, followed by the MoveFile method, supplying the source file and the directory to which to move it. This example moves all the files in the My Documents directory to the directory named StorageDir.

    Dim fileList = My.Computer.FileSystem.GetFiles(
        My.Computer.FileSystem.SpecialDirectories.MyPictures,
        Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    
    For Each foundFile In fileList
        My.Computer.FileSystem.MoveFile(foundFile,
            "C:\StorageDir\" & My.Computer.FileSystem.GetFileInfo(foundFile).Name)
    Next
    

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

How to: Rename a File in Visual Basic

How to: Move a File in Visual Basic

How to: Create a Copy of a File in a Different Directory in Visual Basic

How to: Parse File Paths in Visual Basic

How to: Move a Directory in Visual Basic

Troubleshooting: Reading from and Writing to Text Files (Visual Basic)

How to: Move a Collection of Files in Visual Basic

Reference

GetFiles

MoveFile