Avatar of RayT
RayT
Flag 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

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Michael Pfister

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
RayT

ASKER
It looks like BinaryReader/BinaryWriter is what I need.  Can you show me an example copy FileA To FileB?
Michael Pfister

RayT

ASKER
Thanks
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
louisfr

File.Open is used for text files
That is not true. Were you thinking about File.OpenText ?
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?
Michael Pfister

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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


RayT

ASKER
Where exactly should this code appear?
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".
Your help has saved me hundreds of hours of internet surfing.
fblack61
it_saige

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.


HTH,

-saige-
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?
louisfr

If you try copying a file in the explorer, does it work?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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.