0

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

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");
    }
  }
}

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

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

谢谢?

4

2 回答 2

0

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

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

首先,您需要获取 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");
            }
        }
于 2019-10-08T00:03:24.463 回答
0

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

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

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]

于 2019-10-08T02:08:12.893 回答