Copies an existing file to a new file
'''
''' Copies an existing file to a new file. Overwriting a file of the same name is allowed
''' http://msdn.microsoft.com/en-us/library/9706cfs5%28v=vs.110%29.aspx
''' it is not highly recommende to us this method with files are locked or in use by another process
'''
''' Destination file name string
''' a boolean to overwrite the file
'''
Public Sub CopyFile(sourceFile As String, backupFile As String, overwrite As Boolean)
If String.IsNullOrEmpty(sourceFile) Then
Throw New ArgumentNullException("sourceFile")
End If
If String.IsNullOrEmpty(backupFile) Then
Throw New ArgumentNullException("backupFile")
End If
' According to MSDN Certain file attributes, Hidden and ReadOnly, can be combined. Other attributes,
' such as Normal, must be used alone.
' http://msdn.microsoft.com/en-us/library/system.io.file.setattributes%28v=vs.110%29.aspx
If IO.File.Exists(backupFile) Then
IO.File.SetAttributes(backupFile, IO.FileAttributes.Normal)
End If
' Since the the file might being in use by another process, shall we repeat the copy process several times.?
For index As Integer = 0 To 9
Try
IO.File.Copy(sourceFile, backupFile, overwrite)
Exit Try
Catch ex As IO.FileNotFoundException
Throw
Catch ex As Exception
If Not overwrite Then
Throw
End If
If index = 9 Then
Throw
End If
Threading.Thread.Sleep(1000)
End Try
Next
End Sub
Comments
Post a Comment