Link to home
Create AccountLog in
Avatar of RayT
RayTFlag for United States of America

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

ASKER CERTIFIED SOLUTION
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of RayT

ASKER

It looks like BinaryReader/BinaryWriter is what I need.  Can you show me an example copy FileA To FileB?
Avatar of RayT

ASKER

Thanks
Avatar of louisfr
louisfr

File.Open is used for text files
That is not true. Were you thinking about File.OpenText ?
Avatar of RayT

ASKER

I simply want to async copy a file from FolderA to FolderB.  And can this be done without throwing File Administrator Permission errors?
File Administrator Permission errors are not caused by the function used.
Its a permission issue you'll have as well when you copy the file via Windows explorer
You might need to have the program ask for administrator privilege by adding this line to your manifest:
<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

Open in new window


Avatar of RayT

ASKER

Where exactly should this code appear?
If you don't have an app.manifest file in your project, create it (right-click on the project, add > new item, and select Application Manifest File).
The file should already include a line like this:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />

Open in new window

Replace "asInvoker" with "requireAdministrator".
It would appear in an application manifest.  You can add one to your project by right clicking and choosing Add -> New Item, then select Application Manifest File from the list of addable files.
User generated image
User generated image
HTH,

-saige-
Avatar of RayT

ASKER

It still does not work.  It appears that I need to set the permissions on the target drive to administrator.  How do i do that?
If you try copying a file in the explorer, does it work?
Avatar of RayT

ASKER

I found out what the problem was.  The rights on the target drive was set for read only.  The program started working fine once i changed the rights to read/write.  I also switched the code to something more robust Robocopy.