【问题标题】:Xbox One CopyAsync failing in Retail Mode onlyXbox One CopyAsync 仅在零售模式下失败
【发布时间】:2017-07-01 19:28:56
【问题描述】:

基本上,以下代码会将整个文件夹从应用安装复制到 LocalCacheFolder,以便对其进行操作/更新。在这种情况下,名为“Data”的文件夹的内容

此代码在开发模式下的移动设备、桌面和 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);
    }
}

【问题讨论】:

  • 失败怎么办?它会抛出异常还是什么?
  • 这就是问题所在,因为它只在零售模式下失败,无法连接调试器来查看发生了什么。我必须进行编辑、提交到商店、等待认证、安装在零售盒上并再次经历整个耗时的过程。我在屏幕上放置了一个文本框并在每个命令之后更新了它的值,所以我知道它在哪里不再工作并尝试使用 try{}catch(Exception e){} 块捕获任何异常,但它只是跳过了 catch 中的代码

标签: c# uwp xbox-one


【解决方案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();
            }

基本上将其分为 2 个单独的操作解决了问题,我的应用现在可以正常运行。这现在作为错误报告提交

更新:不是一个错误,而是一个功能,来自 Microsoft:

这里的问题是包安装文件夹在 Xbox 零售模式下是加密的。一个包有权读取它自己的文件,这就是 ReadTextAsync+WriteTextAsync 工作的原因。另一方面,CopyAsync 尝试使用与文件关联的所有属性(包括加密)复制文件。

【讨论】:

    【解决方案2】:

    我不确定是否会出现这种情况,因为您的代码看起来不错,但是 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 属性以检查现有项目 - 以防出现问题。

    【讨论】:

    • 感谢@Romasz,在本地运行良好,我现在已经编译了应用程序并提交到商店,所以我可以看看这是否适用于零售模式,我会尽快反馈跨度>
    • @StuAyton 您是否也想过/尝试过使用 LocalFolder 而不是 LocalCacheFolder
    • 按照docs.microsoft.com/en-us/uwp/api/… LocalFolder 的文档进行操作使用 LocalCacheFolder。
    • 我现在已经能够在零售模式下测试您的代码,但不幸的是,我遇到了同样的问题,即文件没有被复制
    • 我目前正在使用我的一个付费支持请求与 Microsoft 讨论此问题,也许如果它实际上是一个 Xbox 错误,他们会为我报销它,哈哈
    猜你喜欢
    • 2019-02-14
    • 2017-06-30
    • 1970-01-01
    • 2018-03-13
    • 2017-04-30
    • 2016-09-11
    • 2019-06-13
    • 2016-12-11
    • 2017-12-20
    相关资源
    最近更新 更多