2017-05-17 65 views
2

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

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应用程序冻结了一会儿,然后关闭。代码有问题吗?任何想法是怎么回事? 在此先感谢

+0

执行此操作:Project - > Properties - > Debug。在“调试器类型”下更改为“混合”。这应该阻止应用程序因抛出异常而关闭,从而允许您检查它。 – Draco18s

+0

@Draco18s的建议我很好。另外,请检查Hololens Device Portal是否已成功创建此文件。还捕获崩溃转储,参考:https://developer.microsoft.com/en-us/windows/mixed-reality/using_the_windows_device_portal#file_explorer和https://developer.microsoft.com/en-us/windows/mixed-reality/using_the_windows_device_portal#app_crash_dumps –

回答

2

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

在设备门户网站/文件资源管理器,它被映射到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 
} 
+0

一个令人难以置信的有用和正确的答案。非常感谢你 – Dan