UWP 将资产中的多个文件复制到指定位置
UWP Copy Multiple Files from Assets to Specified Location
所以我已经做到了:
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");
}
}
}
我不知道如何一次获取所有文件并将它们一起复制。
我可以为每个文件写新的一行 copy/paste,但必须有更简单的方法将它们放在一起。
谢谢?
What I can't figure out if how to get all the files at once and copy
them all together.
这是使用您的代码实现此目的的一种方法:
首先,您需要获取 Assets (Source) 文件夹。
您可以使用此行来完成此操作:
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(@"Assets\NewFolder1");
foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
{
await assetFile.CopyAsync(projectFolder");
}
}
我实际上需要 "Assets" 文件夹中的一个文件夹
(@"Assets\NewFolder1");
和await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");
实际上将该文件夹中的所有内容都保存为“.png”文件,随机命名为“1234-456-789”
因此,被替换为 await assetFile.CopyAsync(projectFolder);
以保存具有原始扩展名和名称的所有内容。
就是这样!
再次感谢! x]
所以我已经做到了:
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");
}
}
}
我不知道如何一次获取所有文件并将它们一起复制。
我可以为每个文件写新的一行 copy/paste,但必须有更简单的方法将它们放在一起。
谢谢?
What I can't figure out if how to get all the files at once and copy them all together.
这是使用您的代码实现此目的的一种方法:
首先,您需要获取 Assets (Source) 文件夹。
您可以使用此行来完成此操作:
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(@"Assets\NewFolder1");
foreach (StorageFile assetFile in await assetsFolder.GetFilesAsync())
{
await assetFile.CopyAsync(projectFolder");
}
}
我实际上需要 "Assets" 文件夹中的一个文件夹
(@"Assets\NewFolder1");
和await assetFile.CopyAsync(projectFolder, $"{Guid.NewGuid().ToString()}.png");
实际上将该文件夹中的所有内容都保存为“.png”文件,随机命名为“1234-456-789”
因此,被替换为 await assetFile.CopyAsync(projectFolder);
以保存具有原始扩展名和名称的所有内容。
就是这样!
再次感谢! x]