所以我做到了这一步:
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");
}
}
}
我不知道如何一次获取所有文件并将它们一起复制。
我可以为每个文件编写新的复制/粘贴行,但必须有更简单的方法来将它们放在一起。
谢谢?
我不知道如何一次获取所有文件并复制 他们都在一起。
以下是使用代码实现此目的的一种方法:
首先,您需要获取资产(源(文件夹。
您可以使用以下行执行此操作:
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");
}
}
非常感谢!这确实帮助很大,但是。
我必须回答我自己的问题,因为我调整了您的调整以准确到达我最初要去的地方。
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(@"AssetsNewFolder1");
foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
{
await assetFile.CopyAsync(projectFolder");
}
}
我实际上需要在"资产"文件夹中有一个文件夹(@"AssetsNewFolder1");
和await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");
实际上将该文件夹中的所有内容都保存为".png"文件,带有随机名称,例如"1234-456-789">
因此,替换为await assetFile.CopyAsync(projectFolder);
保存具有原始扩展名和名称的所有内容。
就是这样!
再次感谢!