简体   繁体   中英

Working with Files on Hololens 2 (UWP to .NET)

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM