1

I am developing an application for the HoloLens 2 with Unity. I am still very confused how to connect the UWP environment and the .NET API.

I want to read text files (.txt) as well as binary files (.raw). When working on the Hololens (UWP environment) i use from Windows.Storage the FileOpenPicker(). I have currently coded the processing of the files so that I can test them in the Unity editor (.NET environment). Therefore i use File.ReadAllLines(filePath) to get the txt File and get every line as String, for the Binary Files i use FileStream fs = new FileStream(filePath, FileMode.Open) and BinaryReader reader = new BinaryReader(fs). The Method File.ReadAllLines() from System.IO does not work on the Hololens and i imagine the File stream and the Binary reader will not work as well.

So my Questions is how can i load the data when using the Hololens through the specific UWP API and then use the System.IO API for the rest?

Example of picking files (to get path for later readers):

#if !UNITY_EDITOR && UNITY_WSA_10_0
    
            UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
                {
                    var filepicker = new FileOpenPicker();
                    filepicker.FileTypeFilter.Add("*");
    
                    var file = await filepicker.PickSingleFileAsync();
                    
                    UnityEngine.WSA.Application.InvokeOnAppThread(() =>
                    {
                        path = (file != null) ? file.Path : "Nothing selected";
                        name = (file != null) ? file.Name : "Nothing selected";
                        Debug.Log("Hololens 2 Picker Path = " + path);
                        
                    }, false);     
                }, false);
#endif

#if UNITY_EDITOR

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            path = openFileDialog1.FileName;
            ...
#endif

EDIT:

To make it more clear i have another class which uses the file path (from the picker) and reads the file, depending on the extension (.txt, .raw), as text file or binary file with the help of the System.IO methods.

// For text file
    string[] lines = File.ReadAllLines(filePath);
    string rawFilePath = "";
    
    foreach (string line in lines)
    {
    }
// For binary file
    FileStream fs = new FileStream(filePath, FileMode.Open);
    BinaryReader reader = new BinaryReader(fs);

But on the Hololens 2 the File.ReadAllLines(filePath) throws a DirectoryNotFoundException: Could not find a part of the path Exception. Can i use the Windows.Storage.StorageFile and change it so it works with the code which uses the System.IO methods?

Alex
  • 151
  • 10
  • Does it throw any exception when you use File.ReadAllLines System.IO api in above unity project ? – Nico Zhu Jul 28 '22 at 06:01
  • @NicoZhu-MSFT Yes. I load with the Filepicker and get the Path on the Hololens Device, which i use in File.ReadAllLines(pathOnHololens) and get the error DirectoryNotFoundException: Could not find a part of the path. I checked the path which looks like this `C:\Data\Users\userEmail\Documents\Datasets...` whit the userEmail which is the MS Account of the current Hololens User. – Alex Jul 28 '22 at 08:59
  • UWP run in sandbox, why not use windows storage api to read file? please refer to this [link](https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files) – Nico Zhu Jul 28 '22 at 09:07
  • 1
    @NicoZhu-MSFT Thank you for the help and the Link. If i read everything correctly the windows storage api does not work for the Unity Editor. So i would have to do the loading twice, one for the Hololens 2 and for Windows/Unity? Further this would mean for Binary Files i would need to use a windows storage buffer (IBuffer) instead of FileStream and BinaryReader? – Alex Jul 28 '22 at 09:34
  • 1
    If you want to use WinRT API in Unity, you may need to use Unity conditional compilation feature and refer to https://learn.microsoft.com/en-us/windows/mixed-reality/develop/unity/using-the-windows-namespace-with-unity-apps-for-hololens to add WinRT usage. If required, we can provide some sample code snippets of usage of FolderPicker and StreamWriter. The common solution is to create a Visual Studio project, then migrate codes to Unity and ensure they are wrapped in ENABLE_WINMD_SUPPORT section after being fully tested. – Seth DU - MSFT Jul 28 '22 at 09:53
  • So to use the Code which is written with the System.IO methods, i should use StreamWriter and StreamReader like in this similar [Question](https://stackoverflow.com/questions/59598401/how-to-use-system-io-serializers-in-uwp-with-windows-storage)? If you have some code snippets I would be very happy as I am having a hard time making the connection between UWP environment and .NET environment. – Alex Aug 01 '22 at 08:41

1 Answers1

0

I think i found an Answer and i hope it helps others with the same problem:

#if !UNITY_EDITOR && UNITY_WSA_10_0
    public async Task<StreamReader> getStreamReader(string path)
    {
        StorageFile file = await StorageFile.GetFileFromPathAsync(path);
        var randomAccessStream = await file.OpenReadAsync();
        Stream stream = randomAccessStream.AsStreamForRead();
        StreamReader str = new StreamReader(stream);

        return str;
    }
#endif

With this code i can get a stream from an Windows StorageFile and generate a StreamReader or a BinaryReader through which i can use the rest of my calculations written with System.IO.

Alex
  • 151
  • 10