基本上,以下代码将整个文件夹从应用程序安装复制到LocalCacheFolder中,以便可以对它们进行操作/更新。在这种情况下,文件夹的内容称为“数据”
此代码在开发模式下可在移动设备,桌面设备和Xbox上正常运行,但在零售模式下,此行在Xbox上失败:
await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.FailIfExists);
这也是全新安装,所以我知道文件尚不存在。
在零售模式下是否有不同的方法可以实现这一点,尽管从理论上讲,所有UWP代码都可以跨设备工作。
private async Task setupdatabase()
{
StorageFolder destinationContainer = Windows.Storage.ApplicationData.Current.LocalCacheFolder;
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Data";
StorageFolder sfolder = await StorageFolder.GetFolderFromPathAsync(path);
await CopyFolderAsync(sfolder, destinationContainer);
}
public static async Task CopyFolderAsync(StorageFolder source, StorageFolder destinationContainer, string desiredName = null)
{
StorageFolder destinationFolder = null;
destinationFolder = await destinationContainer.CreateFolderAsync(desiredName ?? source.Name, CreationCollisionOption.OpenIfExists);
foreach (var file in await source.GetFilesAsync())
{
await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.FailIfExists);
}
foreach (var folder in await source.GetFoldersAsync())
{
await CopyFolderAsync(folder, destinationFolder);
}
}
答案 0 :(得分:1)
看起来像零售模式下Xbox上的CopyAsync错误
更换:
await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.FailIfExists);
使用:
StorageFile sourcefile = null;
string sourcefiletext = null;
try
{
sourcefile = await source.GetFileAsync(file.Name);
sourcefiletext = await FileIO.ReadTextAsync(sourcefile);
}
catch (Exception e)
{
Debug.WriteLine "Read source error:" + e.ToString();
}
try
{
StorageFile destfile = await destinationFolder.CreateFileAsync(file.Name, CreationCollisionOption.FailIfExists);
await Windows.Storage.FileIO.WriteTextAsync(destfile, sourcefiletext);
}
catch (Exception e)
{
Debug.WriteLine "Write dest error:" + e.ToString();
}
基本上把它分成两个单独的操作修复了问题,我的应用程序现在正常运行。现在这是作为错误报告提交的
更新:不是一个错误,而是来自Microsoft的一个功能:
此处的问题是程序包安装文件夹在Xbox零售模式下加密。一个包有权读取它自己的文件,这就是ReadTextAsync + WriteTextAsync工作的原因。另一方面,CopyAsync尝试使用与文件关联的所有属性(包括加密)复制文件。
答案 1 :(得分:0)
我不确定是否会出现这种情况,因为您的代码看起来没问题,但once I've stepped in a situation在本地运行应用程序时授予了一些权限。也许在这种情况下还有不同的权限(对于设备/零售?) - 因此,您是否可以尝试不通过其路径访问该文件夹,而是直接使用 StorageFolder ?像这样:
private async Task setupdatabase()
{
StorageFolder sfolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Data");
await CopyFolderAsync(sfolder, ApplicationData.Current.LocalCacheFolder);
}
public static async Task CopyFolderAsync(StorageFolder source, StorageFolder destinationContainer, string desiredName = null)
{
StorageFolder destinationFolder = await destinationContainer.CreateFolderAsync(desiredName ?? source.Name, CreationCollisionOption.OpenIfExists);
var existingItems = await destinationFolder.GetFilesAsync(); // to check if files are already there
foreach (var file in (await source.GetFilesAsync()).Where(x => !existingItems.Any(y => y.Name == x.Name)))
{
await file.CopyAsync(destinationFolder, file.Name);
}
foreach (var folder in await source.GetFoldersAsync())
{
await CopyFolderAsync(folder, destinationFolder);
}
}
在第二种方法中,我已经更改 FailIfExists 属性以检查现有项目 - 以防出现问题。