Search code examples
c#unity-game-engineuwphololensfile-writing

How to write file to Hololens from UWP app


I am developing a UWP app for the HoloLens 2 with Unity. I want to write to a log file (to record user interactions), and then later retrieve that file. I do not want the user to have to select a file location.

The following code works on Desktop UWP app (output from same code and same Unity project). It does NOT work on the HoloLens, but instead throws an "Unauthorized" Exception. How can I write to a file on the HoloLens? And where can I find that file later?

public class Logger : MonoBehaviour
{
    StreamWriter logFileWriter;

    // Start is called before the first frame update
    void Start()
    {
        string fileName = "mydatalog.csv";

        // Creates a file in the default location:
        //      "build\bin\x64\Release\AppX" on Desktop
        this.logFileWriter = new StreamWriter(fileName);
    }

    void Update()
    {
        string record = // Stuff here...

        if (this.logFileWriter != null)
        {
            this.logFileWriter.WriteLine(record);
        }
    }
}

Solution

  • Why don't you try setting the filepath to write to Application.persistentDataPath? In Unity's docs it says for Windows applications that the path points to

    %userprofile%\AppData\Local\Packages\<productname>\LocalState
    

    so that's the path where you would look for the files on your desktop later.

    I used to write persistent data there when I was working with Hololens. Afterwards, through the Hololens portal on desktop I could navigate to that file that I wrote.

    I haven't worked with Hololens 2 but I don't think that the process would be very different.