Stefan Meyer
Stefan Meyer

Reputation: 928

Windows Universal App Error while copying files

I try to copy all files from one folder to a folder on my USB stick. In the source directory are some XML files and some JPG files.

File1.xml
Picture1-1.jpg
Picture1-2.jpg
File2.xml
Picture2.jpg

This is my code so far:

Private Async Sub btnCopyToUSB_Click(sender As Object, e As RoutedEventArgs) Handles btnCopyToUSB.Click
        Dim FolderPick As Windows.Storage.Pickers.FolderPicker = New Windows.Storage.Pickers.FolderPicker
        Dim USBDirectory As String
        FolderPick.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder
        FolderPick.FileTypeFilter.Add(".xml")
        Dim Folder As Windows.Storage.StorageFolder = Await FolderPick.PickSingleFolderAsync
        Dim DestinationFolder As Windows.Storage.StorageFolder
        Dim existingFile As StorageFile
        Dim copiedFile As StorageFile
        If Not (Folder Is Nothing) Then
            Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PlatinaFolderToken", Folder)
            USBDirectory = ProjectID & "-" & DocumentType ' ProjectID="ABC"   DocumentType="XYZ" 'Directory "ABC-XYZ" already exists
            DestinationFolder = Await Folder.GetFolderAsync(USBDirectory)
            Dim existingFiles As IReadOnlyList(Of StorageFile) = Await Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync
            For Each existingFile In existingFiles
                copiedFile = Await existingFile.CopyAsync(DestinationFolder, existingFile.Name, NameCollisionOption.ReplaceExisting)
            Next
        End If
    End Sub

The first file is copied (File1.xml). When copying the second file (Picture1-1.jpg) it raises an exception

Error HRESULT E_FAIL has been returned from a call to a COM component.

What is my problem here? When I rename the files or add another file (a.xml) so it is alphabetically in front of File1.xml then a.xml is copied and File1.raises the error. So only the first file is copied.

Upvotes: 1

Views: 68

Answers (2)

Paul Binzel
Paul Binzel

Reputation: 26

Have you checked your destination USB drive? I had this error while writing on a corrupt USB drive. Aber formatting it worked like a charm. Check if you can access the files using Windows Explorer or copy from cmd shell

Upvotes: 1

Claudius
Claudius

Reputation: 1911

use this:

File.Copy(Path.Combine(existingFile.Name, DestinationFolder, True)

instead of :

copiedFile = Await existingFile.CopyAsync(DestinationFolder, existingFile.Name, NameCollisionOption.ReplaceExisting)

Upvotes: 1

Related Questions