Nelson Yi
Nelson Yi

Reputation: 49

Creating txt file on hololens

I'm trying to create an app on the hololens that creates and writes to a text file to log inputs from the user. Currently, I'm stuck on trying to make the file and access it from the file explorer or the one drive. This is the method I have:

public void createFile()
    {
#if WINDOWS_UWP
        Task task = new Task(
        async () =>
        {
        testText.text="hi";
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        StorageFile textFileForWrite = await storageFolder.CreateFileAsync("Myfile.txt");
        });
        task.Start();
        task.Wait();
#endif
    }

It's basically what I found here: https://forums.hololens.com/discussion/1862/how-to-deploy-and-read-data-file-with-app, but when I try to run that method, the app on the hololens freezes for a bit then closes. Is there something wrong with the code? Any idea what is going on? Thanks in advance

Upvotes: 3

Views: 5983

Answers (1)

RCYR
RCYR

Reputation: 1492

In Unity, you can use Application.persistentDataPath: https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html

In Device Portal/File Explorer, it is mapped to LocalAppData/YourApp/LocalState. The code below would write "MyFile.txt" there.

using System.IO;

string path = Path.Combine(Application.persistentDataPath, "MyFile.txt");
using (TextWriter writer = File.CreateText(path))
{
    // TODO write text here
}

Upvotes: 7

Related Questions