【问题标题】:UWP Copy Multiple Files from Assets to Specified LocationUWP 将多个文件从资产复制到指定位置
【发布时间】:2020-02-04 21:28:30
【问题描述】:

所以我已经做到了这一点:

private async void DownloadButton_Click(object sender, RoutedEventArgs e)
   {
    // Pick a location to create new folder in.

    FolderPicker picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.Downloads };
    picker.FileTypeFilter.Add("*");
    StorageFolder folder = await picker.PickSingleFolderAsync();

    // Create new folder with "custom name" + replaces existing.

    var projectFolderName = "New Folder 2";
    StorageFolder projectFolder = await folder.CreateFolderAsync(projectFolderName, CreationCollisionOption.ReplaceExisting);

    //Pick a file to be copied

    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/NewFolder1/File1.png"));
    StorageFile file2 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/File2.png"));

    //Paste copied file in custom folder.

    await file.CopyAsync(projectFolder, "File1.png");
    await file2.CopyAsync(projectFolder, "File2.png");
    }
  }
}

我不知道如何一次获取所有文件并将它们全部复制。

我可以为每个文件编写新的复制/粘贴行,但必须有更简单的方法将它们放在一起。

谢谢?

【问题讨论】:

    标签: uwp copy storagefile storagefolder


    【解决方案1】:

    我不知道如何一次获取所有文件并复制 他们都在一起。

    这是使用您的代码实现此目的的一种方法:

    首先,您需要获取 Assets (Source) 文件夹。

    您将能够使用此行来做到这一点:

    StorageFolder assetsFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");
    

    然后从Source枚举文件并复制到指定文件夹(Destination)

     foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
     {
         await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");
     }
    

    这是调整后的代码:

    private async void DownloadButton_Click(object sender, RoutedEventArgs e)
            {
                // Pick a location to create new folder in.
                FolderPicker picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.Downloads };
                picker.FileTypeFilter.Add("*");
                StorageFolder folder = await picker.PickSingleFolderAsync();
    
                // Create new folder with "custom name" + replaces existing.
    
                var projectFolderName = "New Folder 2";
                StorageFolder projectFolder = await folder.CreateFolderAsync(projectFolderName, CreationCollisionOption.ReplaceExisting);
    
                // Copy all files from assets folder and paste to destination.
                StorageFolder assetsFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");
    
                foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
                {
                    await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");
                }
            }
    

    【讨论】:

      【解决方案2】:

      非常感谢!这确实帮助了很多,但是。

      我必须回答我自己的问题,因为我调整了您的调整以准确了解我最初的目标。

      private async void DownloadButton_Click(object sender, RoutedEventArgs e)
              {
                  // Pick a location to create new folder in.
                  FolderPicker picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.Downloads };
                  picker.FileTypeFilter.Add("*");
                  StorageFolder folder = await picker.PickSingleFolderAsync();
      
                  // Create new folder with "custom name" + replaces existing.
      
                  var projectFolderName = "New Folder 2";
                  StorageFolder projectFolder = await folder.CreateFolderAsync(projectFolderName, CreationCollisionOption.ReplaceExisting);
      
                  // Copy all files from assets folder and paste to destination.
                  StorageFolder assetsFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets\NewFolder1");
      
                  foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
                  {
                      await assetFile.CopyAsync(projectFolder");
                  }
              }
      

      我实际上需要“资产”文件夹中的一个文件夹 (@"Assets\NewFolder1");

      还有await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");

      实际上将该文件夹中的所有内容保存为“.png”文件,名称为“1234-456-789”等随机名称

      因此,被替换为await assetFile.CopyAsync(projectFolder); 以保存具有原始扩展名和名称的所有内容。

      就是这样!

      再次感谢! x]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        • 1970-01-01
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多