Access Hololens Folders

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?

using UnityEngine;
using System.Collections.Generic;
using System;

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;
using System.Collections.Generic;

using System.IO;
using Excel;
using Windows.Storage;
using System.Data;
using System.Windows;
using System.Threading.Tasks;

namespace Windows.Storage
{
   
        public class MyExcel : MonoBehaviour
        {
            private string MySheetName = "Sheet_Test";

            public List<string> MyCellArray;
            public List<string> MyCellArray02;
            public List<string> MyString;
            private bool ReadExcelEnable_NPOI = false;
            private bool ReadExcelEnable_ExcelDataReader = false;


            public async void Test()
            {
            StorageFolder storageFolder =  KnownFolders.CameraRoll;

        }

Any help or insight would be greatly appreciated.

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: Unity - Manual: WinRT API in C# scripts for UWP

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
}
}
1 Like

Ah ok, Thank you for the clarification and the link @timke
Greatly appreciated.

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.

    void OnGUI()
    {
#if ENABLE_WINMD_SUPPORT

        GUIStyle style = new GUIStyle();

        GUI.Label(new Rect(10, 10, 100, 30), "Sheet Name:");
        MySheetName = GUI.TextField(new Rect(90, 10, 200, 30), MySheetName, 25);
        style.richText = true;
        Windows.Storage.StorageFolder storageFolder = Windows.Storage.KnownFolders.CameraRoll;
    
                FileStream MyAddress =  new FileStream(storageFolder + "NoseGearBox.xls" , FileMode.Open, FileAccess.Read, FileShare.Read);

        if (GUI.Button(new Rect(300, 10, 200, 30), "Create Excel Files With " + "<color=red>NPOI</color>"))
        {

            HSSFWorkbook MyWorkbook = new HSSFWorkbook();

            HSSFSheet Sheet01 = (HSSFSheet)MyWorkbook.CreateSheet(MySheetName);

            for (int i = 0; i < 5; i++)
            {

                HSSFRow Row = (HSSFRow)Sheet01.CreateRow((short)i);

                HSSFCell cell = (HSSFCell)Row.CreateCell((short)0);

                cell.SetCellValue(MyCellArray[i]);

                if (i < MyCellArray02.Count)
                {

                    HSSFCell cell02 = (HSSFCell)Row.CreateCell((short)1);

                    cell02.SetCellValue(MyCellArray02[i]);
                }
                else
                {

                    HSSFCell cell02 = (HSSFCell)Row.CreateCell((short)1);

                    cell02.SetCellValue("");
                }

                Row.RowStyle = MyWorkbook.CreateCellStyle();

                Row.RowStyle.BorderBottom = BorderStyle.Double;

                cell.CellStyle = MyWorkbook.CreateCellStyle();

                cell.CellStyle.BorderRight = BorderStyle.Thin;
                cell.CellStyle.BorderBottom = BorderStyle.Dashed;
                cell.CellStyle.BottomBorderColor = HSSFColor.Red.Index;

                HSSFFont MyFont = (HSSFFont)MyWorkbook.CreateFont();

                MyFont.FontName = "Tahoma";
                MyFont.FontHeightInPoints = 14;
                MyFont.Color = HSSFColor.Gold.Index;
                MyFont.Boldweight = (short)FontBoldWeight.Bold;

                cell.CellStyle.SetFont(MyFont);
            }
                MyWorkbook.Write(MyAddress);

                MyWorkbook.Close();

        }
#endif

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: Windows-universal-samples/Samples/FileAccess at main · microsoft/Windows-universal-samples · GitHub

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.

@timke thank you for the information I appreciate it.