Reputation: 11
I'm new with Hololens 2 programming. I'm developing an UWP app with Unity for Holo2 that use an XML configuration file to receive informations about the placing of 3D objects in relative position with the marker. It works fine when I try to read and process the file from Resources folder (Unity and Hololens) and from PC AppData (Unity), but I've some problems when I try to read it from an Hololens AppData folder (also when I try to read file from the special folders KnownFolders). I used the 'ApplicationData.Current.RoamingFolder.Path' as internal UWP folder (accessible from DevicePortal), and StorageFolder & StorageFile for await Get async method in a new Task. I also modified the code of package.appxmanifest with right FileTypeAssociation for .xml I hope that the Microsoft Account Email ([email protected]) used as Username in the path of ApplicationData.Current.RoamingFolder.Path is not the problem for async methods.
//...
using System.Xml.Linq;
using System.Threading.Tasks;
//...
#if WINDOWS_UWP
using Windows.Storage;
#endif
Here the loading of stream
#if WINDOWS_UWP
try
{
folderPathName = ApplicationData.Current.RoamingFolder.Path;
using (Stream s = openFileUWP(folderPathName, filenameWithExtension))
{
document = XDocument.Load(s);
}
}
catch (Exception e)
{
document = XDocument.Parse(targetFile.text); //the XML file in Resources folder
}
#else
//...
#endif
Here the openFileUWP function
#if WINDOWS_UWP
private Stream openFileUWP(string folderName, string fileName)
{
Stream stream = null;
Task task = new Task(
async () =>
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderName);
StorageFile file = await folder.GetFileAsync(fileName);
stream = await file.OpenStreamForReadAsync();
});
task.Start();
task.Wait();
return stream;
}
#endif
Upvotes: 1
Views: 534
Reputation: 816
For reading files on the UWP platform, you can refer Create, write, and read a file - UWP applications | Microsoft Learn and the following code.
#if ENABLE_WINMD_SUPPORT
using Windows.Storage;
using Windows.Storage.Streams;
#endif
#if ENABLE_WINMD_SUPPORT
private async void ReadFile()
{
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;
Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
//You can return the stream here. Note that the type of this stream is Windows.Storage.Streams.IRandomAccessStream.
ulong size = stream.Size;
using (var inputStream = stream.GetInputStreamAt(0))
{
using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
{
uint numBytesLoaded = await dataReader.LoadAsync((uint)size);
string text = dataReader.ReadString(numBytesLoaded);
}
}
}
#endif
Upvotes: 0
Reputation: 14846
You're using the Task
constructor, which is recommended not to do. See Task Constructors.
Also, the Task
constructor takes an Action
. So, there's not task to await and, when the task is started, it completes almost immediately.
Also, blocking may block, or even deadlock you UI.
Try this:
private async Task<Stream> openFileUWP(string folderName, string filename)
{
var folder = await StorageFolder.GetFolderFromPathAsync(folderName);
var file = await folder.GetFileAsync(filename);
var stream = await file.OpenStreamForReadAsync();
return stream;
}
Upvotes: 1