简体   繁体   中英

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

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
}

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