0

I am trying to load a file from a unity applications build for UWP, more specific for the HoloLens. I have a working 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

However, when trying to load the file in an other part of the code using

File.ReadAllBytes(filePath);

I get following exception, where is the actual file path of the file. Which exist as I picked it earlier using the file picker

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

Any help would be appreciated

theVortr3x
  • 70
  • 10
  • 1
    Afaik in UWP you can't use anything from `File` directly – derHugo Jan 26 '22 at 11:40
  • Do you have a workaround for this? – theVortr3x Jan 26 '22 at 11:43
  • Afaik you will need to go through [`FileIO`](https://learn.microsoft.com/uwp/api/windows.storage.fileio) – derHugo Jan 26 '22 at 11:45
  • Can you give an example how should look like if want to load the file as a byte[] – theVortr3x Jan 26 '22 at 12:04
  • You would use [`FileIO.ReadBufferAsync`](https://learn.microsoft.com/uwp/api/windows.storage.fileio.readbufferasync#windows-storage-fileio-readbufferasync(windows-storage-istoragefile)) and then simply `ToArray` see e.g. [here](https://stackoverflow.com/questions/36081740/uwp-async-read-file-into-byte) – derHugo Jan 26 '22 at 12:32

1 Answers1

1

You need to add the file to FutureAccessList in order to access it later by path (rather than the original Windows.Storage.StorageFile object). If you add it to that list after picking it, all System.IO file access operations should start working.

Sunius
  • 2,789
  • 18
  • 30