Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question HoloLens2- Access file names

Discussion in 'Windows' started by camroo29, Mar 24, 2023.

  1. camroo29

    camroo29

    Joined:
    Feb 7, 2020
    Posts:
    3
    Hi,

    I am working on a HoloLens2 project where I have to access a file. In my unity project, I usually keep the file in StreamingAssets folder and can easily access it from the editor. But when I try to access the file for UWP, I don't get any output in HoloLens.

    Code (CSharp):
    1.  string folderPath = Application.streamingAssetsPath;
    2. #if UNITY_EDITOR
    3.         var files = Directory.GetFiles(folderPath);
    4.         if (UnityEngine.Windows.File.Exists(files[0]) && (files[0].Contains(".bin")))
    5.             voxel = File.OpenRead(files[0]);
    6.  
    7. #elif UNITY_WSA
    8.  
    9.                 var files = new[]{folderPath + "/file_name.bin"};
    10.  
    11.                 if (UnityEngine.Windows.File.Exists(files[0]) && (files[0].Contains(".bin")))
    12.                     voxel = File.OpenRead(files[0]);
    I can see the output in HoloLens when I specify the file name. That is what I am trying to avoid.

    It would be helpful if you could suggest how to read files in UWP
     
  2. CarePackage17

    CarePackage17

    Joined:
    Dec 6, 2014
    Posts:
    11
    No need for special casing; the System.IO namespace works in Editor and in UWP builds (at least after a regression was fixed in 2021.3.19).

    Code (CSharp):
    1.  
    2. string folderPath = Application.streamingAssetsPath;
    3. var files = Directory.GetFiles(folderPath, "*.bin");
    4.  
    5. if (files.Length > 0)
    6. {
    7.     using (var stream = File.OpenRead(files[0]))
    8.     {
    9.         //read from stream here
    10.     }
    11. }
    12.  
     
  3. camroo29

    camroo29

    Joined:
    Feb 7, 2020
    Posts:
    3
    Thanks a lot!
    It is working perfectly fine.
     
    CarePackage17 likes this.