【问题标题】:Loading file from file picker gives DirectoryNotFoundException从文件选择器加载文件会产生 DirectoryNotFoundException
【发布时间】:2022-02-24 08:40:43
【问题描述】:

我正在尝试从为 UWP 构建的统一应用程序加载文件,更具体地为 HoloLens。我有一个可用的 FilePicker

#if !UNITY_EDITOR && UNITY_WSA_10_0
        UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();
            picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
            picker.FileTypeFilter.Add(".bytes");
            Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
            UnityEngine.WSA.Application.InvokeOnAppThread(() => 
            {
                string name = (file != null) ? file.Name : "No data";
                string path = (file != null) ? file.Path : "No data";

                if( file != null )
                    CaseManager.Instance.ActiveUseCase = new Case(new CaseDataDiscriptor(name, path));

                SceneLoader.Instance.LoadSceneAsync("ARViewer");
                SceneLoader.Instance.UnLoadScene("CaseList");

            }, false);
        }, false);
#endif

但是,当尝试在代码的其他部分使用

加载文件时
File.ReadAllBytes(filePath);

我得到以下异常,文件的实际文件路径在哪里。正如我之前使用文件选择器选择的那样存在

DirectoryNotFoundException: Could not find a part of the path <MYPATH>.at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00000] in <00000000000000000000000000000000>:0 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <00000000000000000000000000000000>:0 at System.IO.File.OpenRead (System.String path) [0x00000] in <00000000000000000000000000000000>:0 at System.IO.File.ReadAllBytes (System.String path) [0x00000] in <00000000000000000000000000000000>:0 at Assets.Scripts.Utils.Parser.Parser.FileToByteArray (System.String filePath) [0x00000] in <000000000000000000000

任何帮助将不胜感激

【问题讨论】:

  • Afaik 在 UWP 中你不能直接使用来自 File 的任何东西
  • 你有解决办法吗?
  • Afaik 你需要通过FileIO
  • 你能举个例子,如果想将文件加载为字节[]
  • 您可以使用 FileIO.ReadBufferAsync 然后简单地使用 ToArray 参见例如here

标签: unity3d uwp hololens


【解决方案1】:

您需要将文件添加到FutureAccessList 以便以后通过路径(而不是原始的 Windows.Storage.StorageFile 对象)访问它。如果您在选择后将其添加到该列表中,则所有 System.IO 文件访问操作都应该开始工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多