2017-05-17 62 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