复制文件时Windows Universal App错误

时间:2016-04-20 12:53:13

标签: vb.net win-universal-app

我尝试将所有文​​件从一个文件夹复制到USB记忆棒上的文件夹中。 在源目录中有一些XML文件和一些JPG文件。

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

到目前为止,这是我的代码:

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

复制第一个文件(File1.xml)。 复制第二个文件(Picture1-1.jpg)时会引发异常

  

错误HRESULT E_FAIL已从调用COM组件返回。

这里有什么问题?当我重命名文件或添加另一个文件(a.xml),因此它按字母顺序放在File1.xml之前,然后复制a.xml并File1.raises错误。因此只复制第一个文件。

2 个答案:

答案 0 :(得分:1)

使用它:

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

而不是:

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

答案 1 :(得分:1)

您是否检查了目标USB驱动器?在损坏的USB驱动器上写入时出现此错误。阿伯格式化它就像一个魅力。 检查您是否可以使用Windows资源管理器访问文件或从cmd shell复制