The Mixed Reality Forums here are no longer being used or maintained.
There are a few other places we would like to direct you to for support, both from Microsoft and from the community.
The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.
For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.
If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.
And always feel free to hit us up on Twitter @MxdRealityDev.
How to deploy and read data file with app.

Someone reached out and asked me how to package a simple xml data file with their 3D Unity UWP project and how to programmatically find and read the file from within the HoloLens app on the device.
They seemed happy with the answer so I thought I would share it in case others are looking to do something similar.
Windows Holographic User Group Redmond
WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
WinHUGR YouTube Channel -- live streamed meetings
Best Answers
-
HoloSheep mod
In the Visual Studio (Universal Windows) project that gets generated from the Unity build process you can add your XML or text file either to the root of the project or you can create a subdirectory in the project and put the file there, you will just need to adjust the Uri path accordingly.
You will want to include the following conditional using statements at the top of the script that you plan to insert the file read logic into:
#if WINDOWS_UWP using Windows.Storage; using System.Threading.Tasks; using Windows.Data.Xml.Dom; using System; #endif
And then inside the body of that script where you wish to read the file:
string xmlText; #if WINDOWS_UWP Task task = new Task( async () => { var xmlFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///xmlData/Test.xml")); XmlDocument xdoc = await XmlDocument.LoadFromFileAsync(xmlFile); xmlText = xdoc.GetXml(); }); task.Start(); task.Wait(); #endif
The above sample is expecting your xml file to be in a subdirectory in your project called:
xmlData
and the file in this case is named
Text.xmlIn Visual Studio, if you right click on the file and bring up the Properties window for the file make sure it is marked as:
Build Action – Content
Copy to Output Directory – Copy if neweralternatively, if you want to do something similar with a plain text file you could instead use:
string plainText; #if WINDOWS_UWP Task task = new Task( async () => { StorageFile textFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Text.txt")); plainText = await FileIO.ReadTextAsync(textFile); txtTextMesh.text = plainText; }); task.Start(); task.Wait(); #endif
and something similar if it contained JSON that you wanted to parse.
The person who originally asked wanted a simple way to store some meta data in a file in the project that they could read at runtime.
HTH.
Windows Holographic User Group Redmond
WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
WinHUGR YouTube Channel -- live streamed meetings6 -
HoloSheep mod
Files in the package installation folder are read only, so the above sample works for a data file that you have at design time but want to read at runtime.
If you want to read and write at runtime you could do so in the local application folder with code like:
#if WINDOWS_UWP //Get local folder StorageFolder storageFolder = ApplicationData.Current.LocalFolder; //Create file StorageFile textFileForWrite = await storageFolder.CreateFileAsync("LocalText.txt"); //Write to file await FileIO.WriteTextAsync(textFileForWrite, "Text written to file from code"); ..... //Get file StorageFile textFileForRead = await storageFolder.GetFileAsync("LocalText.txt"); //Read file string plainText = ""; plaintext = await FileIO.ReadTextAsync(textFileForRead); #endif
Depending on your needs, you could also combine the two approaches for a scenario where say you had default settings that you wanted to push out from design time in the package folder that gets read on first run and written into the local folder as user settings that you could then programmatically both read and write as required.
HTH.
Windows Holographic User Group Redmond
WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
WinHUGR YouTube Channel -- live streamed meetings5 -
HoloSheep mod
Few things you could try:
- do a try catch and handle the FileNotFoundException for the GetFileAsync
- TryGetItemAsync might be a cleaner way to go
- or perhaps take a look at FileInfo.Exists property if you want to go the CreateFileQuery route
Please let us know if any of those work out for you.
Windows Holographic User Group Redmond
WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
WinHUGR YouTube Channel -- live streamed meetings6
Answers
In the Visual Studio (Universal Windows) project that gets generated from the Unity build process you can add your XML or text file either to the root of the project or you can create a subdirectory in the project and put the file there, you will just need to adjust the Uri path accordingly.
You will want to include the following conditional using statements at the top of the script that you plan to insert the file read logic into:
And then inside the body of that script where you wish to read the file:
The above sample is expecting your xml file to be in a subdirectory in your project called:
xmlData
and the file in this case is named
Text.xml
In Visual Studio, if you right click on the file and bring up the Properties window for the file make sure it is marked as:
Build Action – Content
Copy to Output Directory – Copy if newer
alternatively, if you want to do something similar with a plain text file you could instead use:
and something similar if it contained JSON that you wanted to parse.
The person who originally asked wanted a simple way to store some meta data in a file in the project that they could read at runtime.
HTH.
Windows Holographic User Group Redmond
WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
WinHUGR YouTube Channel -- live streamed meetings
What would be the method for logic to write the text file using FileIO?
@Terry_Bennett_Dev
Files in the package installation folder are read only, so the above sample works for a data file that you have at design time but want to read at runtime.
If you want to read and write at runtime you could do so in the local application folder with code like:
Depending on your needs, you could also combine the two approaches for a scenario where say you had default settings that you wanted to push out from design time in the package folder that gets read on first run and written into the local folder as user settings that you could then programmatically both read and write as required.
HTH.
Windows Holographic User Group Redmond
WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
WinHUGR YouTube Channel -- live streamed meetings
Great Information! Thank you!
I'm stuck on load a saveData StorageFile if it exists or create a new StorageFile if it doesn't. I've tried StoreFile.GetFileAsync, & CreateFileAsync("saveData", CreationCollisionOption.OpenIfExists) with no luck. When loading Save.txt, the Json string always returns "". Any Advice?
var saveData = ScriptableObject.CreateInstance();
saveData.PlayerExperience = 0;
var storageFolder = ApplicationData.Current.LocalFolder;
StorageFileQueryResult results = storageFolder.CreateFileQuery();
IReadOnlyList filesInFolder = await results.GetFilesAsync();
foreach (StorageFile item in filesInFolder)
{
if (item.Name == "Save.txt")
{
StorageFile tempFile = await storageFolder.GetFileAsync("Save.txt");
saveTextFile = tempFile;
_jsonString = await FileIO.ReadTextAsync(saveTextFile);
}
}
if (_jsonString != "")
{
saveData = JsonUtility.FromJson(_jsonString);
}
else
{
// For Testing
saveData.PlayerExperience = 7;
}
_jsonString = JsonUtility.ToJson(saveData);
saveTextFile = await storageFolder.CreateFileAsync("Save.txt",CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(saveTextFile, _jsonString);
Happy Holidays!!
@Terry_Bennett_Dev
Few things you could try:
Please let us know if any of those work out for you.
Windows Holographic User Group Redmond
WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
WinHUGR YouTube Channel -- live streamed meetings
It works!! Thank you so much!!
try
{
if (await storageFolder.TryGetItemAsync("Save.txt") != null)
{
saveTextFile = await storageFolder.GetFileAsync("Save.txt");
_jsonString = await FileIO.ReadTextAsync(saveTextFile);
var serializedData = CreateFromJson(_jsonString);
saveData = serializedData;
}
}
catch (Exception e)
{
Debug.Log(e);
throw;
}
If I do not use Unity (use DirectX and c++/cx) and want to load a big binary data, can I load it from network at run time?
Thanks.
Hello,
I also tried in the unity Forums, if anybody wants to have a look:
https://forum.unity3d.com/threads/still-huge-problems-saving-data-permanent-folders-inside.487530/
I have my app on the hololens. It should save an XML file containing Texts and data (it is a new file, not deployed or something) to the hololens itself (not One Drive).
It has links to Pictures and Videos which I can record with the app myself or maybe upload additional files. These must also be stored on the hololens, I would prefer the same Folder like the XML file. If possible, it should be stored under a user given "assistant Name".
Like suggested here I tried the LocalFolder and others:
if !UNITY_EDITOR && UNITY_METRO
else
endif
But if I install an update of the app, or completly shut down the hololens all files are gone! Or cannot be seen anymore. I could see them with the filebrowser on my PC linked to the hololens, I cannot find them anymore after shutdown or app update.
Any help please?
PS:
When I use
Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path;
it seems to survive the shutdown and restart of the hololens. But when I deploy the app again, all files are gone...
Hello,
This is exactly what I need! I just have two quick follow-up questions:
Is the correct location at AppName/GeneratedProjects/UWP/
or somewhere further down the line in the Assembly-CSharp Folders?
And will I be able to see the file somewhere on the file explorer?
Best Wishes
What if we wand to read an asset bundle(.unity3d) file that is stored in hololens known folders?