Some people think that file operations are very simple, but they are not. If you do not have the permission, you cannot change the file, but we do not discuss the permission issue today, we will. NET to copy and delete files.
VB. NET copy and delete file code:
VB. NET
Imports System. IO
Imports System. IO. Directory
========================================================== ====================
Implement a static method to copy all the content under the specified folder to the target folder
If the target folder is read-only, an error is returned.
========================================================== ====================
Public Shared Sub CopyDir (ByVal srcPath As String, ByVal aimPath As String)
Try
Check whether the target directory ends with a delimiter. If not, add the delimiter
If aimPath (aimPath. Length-1) <> Path. DirectorySeparatorChar Then
AimPath + = Path. DirectorySeparatorChar
End If
Determine whether the source directory exists. If the source directory does not exist, exit.
If (Not Directory. Exists (srcPath) Then Exit Sub
Determine whether the target directory exists. If it does not exist, create it.
If (Not Directory. Exists (aimPath) Then Directory. CreateDirectory (aimPath)
Obtain the file list of the source Directory, which is an array containing the file and directory path.
If you direct to the file under the copy target file without including the directory, use the following method:
String [] fileList = Directory. GetFiles (srcPath );
Dim fileList () As String = Directory. GetFileSystemEntries (srcPath)
Traverse all files and directories
For Each FileName As String In fileList
First, process the file as a directory. If this directory exists, recursively Copy the file under this directory.
If Directory. Exists (FileName) Then
CopyDir (FileName, aimPath + Path. GetFileName (FileName ))
Otherwise, Copy the file directly.
Else
File. Copy (FileName, aimPath + Path. GetFileName (FileName), True)
End If
Next
Catch ex As Exception
MessageBox. Show (ex. ToString ())
End Try
End Sub
========================================================== ====================
Implement a static method to Detele all content in the specified folder
Be careful when testing. After deletion, it cannot be restored.
========================================================== ====================
Public Shared Sub DeleteDir (ByVal aimPath As String)
Try
Check whether the target directory ends with a directory delimiter. If not, add it.
If (aimPath. Length-1) <> Path. DirectorySeparatorChar) Then
AimPath + = Path. DirectorySeparatorChar
End If
Check whether the directory to be deleted exists. If the directory does not exist, exit.
If (Not Directory. Exists (aimPath) Then Exit Sub
Obtain the file list of the source Directory, which is an array containing the file and directory path.
If you direct to the file in the target file of the Delete object without including the directory, use the following method:
String [] fileList = Directory. GetFiles (aimPath );
Dim fileList () As String = Directory. GetFileSystemEntries (aimPath)
Traverse all files and directories
For Each FileName As String In fileList
If (Directory. Exists (FileName) Then
First, as a directory. If this directory exists, recursively Delete the files under this directory.
DeleteDir (aimPath + Path. GetFileName (FileName ))
Else
Otherwise, Delete the file directly.
File. Delete (aimPath + Path. GetFileName (FileName ))
End If
Next
Delete folder
System. IO. Directory. Delete (aimPath, True)
Catch ex As Exception
MessageBox. Show (ex. ToString ())
End Try
End Sub
The above is a demo of a code for copying and deleting files in VB. NET. Run and try it!