HoloLens2- Access file names

Hi,

I am working on a HoloLens2 project where I have to access a file. In my unity project, I usually keep the file in StreamingAssets folder and can easily access it from the editor. But when I try to access the file for UWP, I don’t get any output in HoloLens.

 string folderPath = Application.streamingAssetsPath;
#if UNITY_EDITOR
        var files = Directory.GetFiles(folderPath);
        if (UnityEngine.Windows.File.Exists(files[0]) && (files[0].Contains(".bin")))
            voxel = File.OpenRead(files[0]);

#elif UNITY_WSA

                var files = new[]{folderPath + "/file_name.bin"};

                if (UnityEngine.Windows.File.Exists(files[0]) && (files[0].Contains(".bin")))
                    voxel = File.OpenRead(files[0]);

I can see the output in HoloLens when I specify the file name. That is what I am trying to avoid.

It would be helpful if you could suggest how to read files in UWP

No need for special casing; the System.IO namespace works in Editor and in UWP builds (at least after a regression was fixed in 2021.3.19).

string folderPath = Application.streamingAssetsPath;
var files = Directory.GetFiles(folderPath, "*.bin");

if (files.Length > 0)
{
    using (var stream = File.OpenRead(files[0]))
    {
        //read from stream here
    }
}

Thanks a lot!
It is working perfectly fine.

1 Like