Hololens 2 create, open, read, write files via coding C#

ANTONIO DE MIGUEL 0 Reputation points
2023-02-06T18:08:26.0633333+00:00

Hi,

To start i would like to say that I,m a really new with this matter, so any help would be wellcome.

I'm trying to create a little applicacion for the Hololens 2 where I just want to register the location of some point in a file. So, in the Unity`s emulator everything works correctly, the file are created, edited, writted as I wish. But when i try the application on the Hololens it doesnt work (I've changed the path). The problem might come because I didn,t use Windows.Storage, but I cant use it cause Visual Studio says that there's an error, so I,ve been using System.IO, but as I said it worked on the emulator, but not in Hololens.

I supose that it might be a quite simple error that I miss, due to my unexperience, but I really need the help.

Other point would be cause there is a problem or something that i dont undertand about UWP, but the Unity app is correctly set to UWP.

Captura de pantalla 2023-02-06 190407

HoloLens Development
HoloLens Development
HoloLens: A family of Microsoft self-contained, holographic devices that enable engagement with digital content and interaction with holograms in the surrounding environment.Development: The process of researching, productizing, and refining new or existing technologies.
379 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zuocheng Wang - MSFT 3,081 Reputation points Microsoft Vendor
    2023-02-07T08:28:43.08+00:00

    Hello, Welcome to Microsoft Q&A,

    Please note that HoloLens App is also a UWP app and UWP apps can only access certain file system locations by default. Your app does not have access to most of the file system. For more details, please refer to File access permissions - UWP applications | Microsoft Learn. For UWP apps, we recommend using Windows Storage APIs to access files. Please refer to Create, write, and read a file - UWP applications | Microsoft Learn. And since Unity does not support WinRT APIs, you need to use preprocessing directives on Unity, please refer to WinRT APIs with Unity for HoloLens - Mixed Reality | Microsoft Learn. Here is an example for your reference.

    #if ENABLE_WINMD_SUPPORT
    using Windows.Storage;
    using Windows.Storage.Streams;
    #endif
    
        private async void ReadFile()
        {
    #if ENABLE_WINMD_SUPPORT
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
            string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
    #endif
        }
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments