在hololens上创建txt文件

时间:2017-05-17 03:10:50

标签: c# unity3d hololens

我试图在hololens上创建一个应用程序,该应用程序创建并写入文本文件以记录来自用户的输入。目前,我一直试图制作文件并从文件浏览器或一个驱动器访问它。这是我的方法:

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
    }

这基本上就是我在这里找到的:https://forums.hololens.com/discussion/1862/how-to-deploy-and-read-data-file-with-app,但是当我尝试运行该方法时,hololens上的应用会冻结一点然后关闭。代码有问题吗?知道发生了什么事吗? 提前致谢

1 个答案:

答案 0 :(得分:5)

在Unity中,您可以使用Application.persistentDataPath:https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html

在Device Portal / File Explorer中,它映射到LocalAppData / YourApp / LocalState。下面的代码将写出" MyFile.txt"那里。

using System.IO;

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