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

.NET ProgrammingVisual Basic.NETC#

Avatar of undefined
Last Comment
RayT
ASKER CERTIFIED SOLUTION
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of RayT
RayT
Flag of United States of America image

ASKER

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

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
RayT
Flag of United States of America image

ASKER

I simply want to async copy a file from FolderA to FolderB.  And can this be done without throwing File Administrator Permission errors?
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

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
Avatar of louisfr
louisfr

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
RayT
Flag of United States of America image

ASKER

Where exactly should this code appear?
Avatar of louisfr
louisfr

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".
Avatar of it_saige
it_saige
Flag of United States of America image

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
RayT
Flag of United States of America image

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?
Avatar of louisfr
louisfr

If you try copying a file in the explorer, does it work?
Avatar of RayT
RayT
Flag of United States of America image

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.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo