asked on
Copy A File Using Async
How do you copy a file Async using the following code? It appears to only work with Non-Binary files (.txt, etc.). I'd like to be able to copy any kind of file.
Shared Async Function CopyFile(inSourceFile As String,
inTargetFolder As String,
Optional inRetainDateTime As Boolean = True) As Task
ExceptionThrown = False
ExceptionDefinition = ""
Dim TargetFile As String = inTargetFolder + "\" + GSFileInfo.ParseFileName(inSourceFile, False)
Dim SourceDateTime As Date = File.GetLastWriteTime(inSourceFile)
Try
Using SourceStream As FileStream = File.Open(inSourceFile, FileMode.Open)
Using DestinationStream As FileStream = File.Create(TargetFile)
Await SourceStream.CopyToAsync(DestinationStream)
End Using
End Using
If inRetainDateTime Then
File.SetLastWriteTime(TargetFile, SourceDateTime)
End If
Catch ex As Exception
ExceptionThrown = True
ExceptionDefinition = ex.Message
End Try
End Function
https://stackoverflow.com/questions/39591579/how-to-use-filestream-for-copying-files-in-c-sharp
ASKER
File.Open is used for text filesThat is not true. Were you thinking about File.OpenText ?
ASKER
Its a permission issue you'll have as well when you copy the file via Windows explorer
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
ASKER
The file should already include a line like this:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
Replace "asInvoker" with "requireAdministrator".
ASKER
ASKER
ASKER