Search Unity

Access Hololens Folders

Discussion in 'Windows Mixed Reality' started by Jarediavionic, May 30, 2019.

  1. Jarediavionic

    Jarediavionic

    Joined:
    Jan 6, 2017
    Posts:
    71
    Hey guys,

    I am trying to create a script that load an excel file from a folder on the hololens. That being said I am having trouble with getting errors for StorageFolder and KnownFolders (I am not the most experienced programmer I am afraid). I am using the namespace Windows.Storage but I am still getting the following errors


    Error CS0103 The name 'KnownFolders' does not exist in the current context Assembly-CSharp C:\Users\jared.turner\Desktop\Hololens Intro\Assets\Uni-Excel\Plugins\MyExcel.cs 34 Active


    Error CS0246 The type or namespace name 'StorageFolder' could not be found (are you missing a using directive or an assembly reference?) Assembly-CSharp C:\Users\jared.turner\Desktop\Hololens Intro\Assets\Uni-Excel\Plugins\MyExcel.cs 34 Active

    Can they not be used under MonoBehaviour?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System;
    4.  
    5. using NPOI.HSSF.UserModel;
    6. using NPOI.SS.UserModel;
    7. using NPOI.HSSF.Util;
    8. using System.Collections.Generic;
    9.  
    10. using System.IO;
    11. using Excel;
    12. using Windows.Storage;
    13. using System.Data;
    14. using System.Windows;
    15. using System.Threading.Tasks;
    16.  
    17. namespace Windows.Storage
    18. {
    19.    
    20.         public class MyExcel : MonoBehaviour
    21.         {
    22.             private string MySheetName = "Sheet_Test";
    23.  
    24.             public List<string> MyCellArray;
    25.             public List<string> MyCellArray02;
    26.             public List<string> MyString;
    27.             private bool ReadExcelEnable_NPOI = false;
    28.             private bool ReadExcelEnable_ExcelDataReader = false;
    29.  
    30.  
    31.             public async void Test()
    32.             {
    33.             StorageFolder storageFolder =  KnownFolders.CameraRoll;
    34.  
    35.         }
    36.  
    Any help or insight would be greatly appreciated.
     
  2. timke

    timke

    Unity Technologies

    Joined:
    Nov 30, 2017
    Posts:
    80
    Code that uses WinRT APIs directly must be enclosed in #if ENABLE_WINMD_SUPPORT since Mono doesn't know about these types. See Unity documentation for details: https://docs.unity3d.com/Manual/windowsstore-scripts.html

    So in this case you'll need something like:

    using UnityEngine;
    public class MyExcel: MonoBehaviour
    {
    public void Test()
    {
    #if ENABLE_WINMD_SUPPORT
    Windows.Storage.StorageFolder storageFolder = Windows.Storage.KnownFolders.CameraRoll;
    #endif
    }
    }
     
    Jarediavionic likes this.
  3. Jarediavionic

    Jarediavionic

    Joined:
    Jan 6, 2017
    Posts:
    71
    Ah ok, Thank you for the clarification and the link @timke
    Greatly appreciated.
     
  4. Jarediavionic

    Jarediavionic

    Joined:
    Jan 6, 2017
    Posts:
    71
    So I have been trying to figure it out but sadly I am still getting the following error:

    DirectoryNotFoundException: FileNotFoundException: Could not find a part of the path
    "C;\Data\Users|DefaultAccount\AppData\Local\DevelopmentFiles\Ah-64GearboxVS.Release_Win32.jared.turner\Windoes.Storage.StorageFolderNoseGearBox.xls"

    Is this error meaning it is looking for a storage folder called nose gearbox? or is it something else?
    If I change the file path to my desktop and try it in the editor it works perfect just never on the hololens.

    Code (CSharp):
    1.     void OnGUI()
    2.     {
    3. #if ENABLE_WINMD_SUPPORT
    4.  
    5.         GUIStyle style = new GUIStyle();
    6.  
    7.         GUI.Label(new Rect(10, 10, 100, 30), "Sheet Name:");
    8.         MySheetName = GUI.TextField(new Rect(90, 10, 200, 30), MySheetName, 25);
    9.         style.richText = true;
    10.         Windows.Storage.StorageFolder storageFolder = Windows.Storage.KnownFolders.CameraRoll;
    11.    
    12.                 FileStream MyAddress =  new FileStream(storageFolder + "NoseGearBox.xls" , FileMode.Open, FileAccess.Read, FileShare.Read);
    13.  
    14.         if (GUI.Button(new Rect(300, 10, 200, 30), "Create Excel Files With " + "<color=red>NPOI</color>"))
    15.         {
    16.  
    17.             HSSFWorkbook MyWorkbook = new HSSFWorkbook();
    18.  
    19.             HSSFSheet Sheet01 = (HSSFSheet)MyWorkbook.CreateSheet(MySheetName);
    20.  
    21.             for (int i = 0; i < 5; i++)
    22.             {
    23.  
    24.                 HSSFRow Row = (HSSFRow)Sheet01.CreateRow((short)i);
    25.  
    26.                 HSSFCell cell = (HSSFCell)Row.CreateCell((short)0);
    27.  
    28.                 cell.SetCellValue(MyCellArray[i]);
    29.  
    30.                 if (i < MyCellArray02.Count)
    31.                 {
    32.  
    33.                     HSSFCell cell02 = (HSSFCell)Row.CreateCell((short)1);
    34.  
    35.                     cell02.SetCellValue(MyCellArray02[i]);
    36.                 }
    37.                 else
    38.                 {
    39.  
    40.                     HSSFCell cell02 = (HSSFCell)Row.CreateCell((short)1);
    41.  
    42.                     cell02.SetCellValue("");
    43.                 }
    44.  
    45.                 Row.RowStyle = MyWorkbook.CreateCellStyle();
    46.  
    47.                 Row.RowStyle.BorderBottom = BorderStyle.Double;
    48.  
    49.                 cell.CellStyle = MyWorkbook.CreateCellStyle();
    50.  
    51.                 cell.CellStyle.BorderRight = BorderStyle.Thin;
    52.                 cell.CellStyle.BorderBottom = BorderStyle.Dashed;
    53.                 cell.CellStyle.BottomBorderColor = HSSFColor.Red.Index;
    54.  
    55.                 HSSFFont MyFont = (HSSFFont)MyWorkbook.CreateFont();
    56.  
    57.                 MyFont.FontName = "Tahoma";
    58.                 MyFont.FontHeightInPoints = 14;
    59.                 MyFont.Color = HSSFColor.Gold.Index;
    60.                 MyFont.Boldweight = (short)FontBoldWeight.Bold;
    61.  
    62.                 cell.CellStyle.SetFont(MyFont);
    63.             }
    64.                 MyWorkbook.Write(MyAddress);
    65.  
    66.                 MyWorkbook.Close();
    67.  
    68.         }
    69. #endif
    70.  
     
    Last edited: Jun 6, 2019
  5. timke

    timke

    Unity Technologies

    Joined:
    Nov 30, 2017
    Posts:
    80
    So that's not the proper way to use StorageFolder. Instead you call StorageFolder's methods to find/enumerate files (e.g. GetFilesAsync) which returns StorageFile objects, from you actually open a file stream.

    Reference file access samples from here: https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/FileAccess

    My recommendation is to first learn basic WinRT API usage through the UWP samples on GitHub and then try to implement the functionality in Unity scripts.
     
  6. Jarediavionic

    Jarediavionic

    Joined:
    Jan 6, 2017
    Posts:
    71
    @timke thank you for the information I appreciate it.