所以我已经做到了:
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");
}
}
}
我不知道如何一次获取所有文件并将它们全部复制到一起。
我可以为每个文件写新的复制/粘贴行,但是必须有一种更简便的方法将所有这些放在一起。
谢谢?
答案 0 :(得分:0)
我不知道如何一次获取所有文件并复制 他们在一起。
这是使用您的代码实现这一目标的一种方法:
首先,您需要获取Assets(源)文件夹。
您将可以使用以下代码来做到这一点:
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");
}
这是经过调整的代码:
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");
}
}
答案 1 :(得分: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");
实际上将该文件夹中的所有内容保存为带有随机名称(例如“ 1234-456-789”)的“ .png”文件
因此,已被await assetFile.CopyAsync(projectFolder);
替换,以使用原始扩展名和名称保存所有内容。
就是这样!
再次感谢! x]