Help With Saving Data to Text File in HoloLens Application

Hello, I am currently attempting to save data from a HoloLens application to a file which can then be later transferred to a PC. So far I have not found any particularly straightforward ways of doing this.

The best example code so far I have found is from this blog post: HoloLens File Transfer · Long Qian

I tried adapting that code and my project builds in both Unity and VS but when I try to run the application it doesn’t save the string to the text file. When I try adding any extra code to debug what is going wrong the application crashes when I try to run it on the HoloLens. I am new to C# development so I may be doing something obviously wrong here.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

#if !UNITY_EDITOR && UNITY_METRO
using System.Threading.Tasks;
using Windows.Storage;
#endif

public class DataSaveTest : MonoBehaviour
{
    void Start()
    {
        WriteString("Does this work?");
    }
 
    public void WriteString(string s)
    {
        #if !UNITY_EDITOR && UNITY_METRO
            using (Stream stream = OpenFileForWrite(ApplicationData.Current.RoamingFolder.Path, "filename.txt")) {
                byte[] data = Encoding.ASCII.GetBytes(s);
                stream.Write(data, 0, data.Length);
                stream.Flush();
            }
        #endif
    }

    private static Stream OpenFileForWrite(string folderName, string fileName)
    {
        Stream stream = null;
        #if !UNITY_EDITOR && UNITY_METRO
            Task task = new Task(
            async () => {
                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderName);
                StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                stream = await file.OpenStreamForWriteAsync();
                });
            task.Start();
            task.Wait();
        #endif
        return stream;
    }
}

Does anyone have an idea of why this code doesn’t work? If it makes a difference this is using Unity 5.6.2. I should also add that before running the application I created an empty text file named filename.txt which I uploaded to the RoamingFolder folder via the Windows device portal.

I believe you need to use FileSavePicker and FileOpenPicker, described (and linked to further documentation to both) here: https://developer.microsoft.com/en-us/windows/mixed-reality/App_model.html#file_pickers

I haven’t written that sort of thing on the HoloLens myself, so let me know if that helps.