hololenscsvhelperwindows-mixed-reality

Find the path to a folder Hololens2


I'm using csvHelper to create and save some csv file (containing data from the hololens' motion detection).

Thing is, I need to save the file somewhere into the HoloLens2 : I've seen some doc about file pickers, but as I am using csvHelper (and i'm also automatically naming the file), what I really need is a path, a string path.

I can't find anywhere the syntax of a path in HoloLens...

What works for now is to execute the script remotely on unity (so that the file is saved directly on my computer), but for the accuracy of my data, I would've like to deploy the app, and so save this file where the HoloLens2 have access.

Here's the bit of code that saves the file :

    /// <summary>
    /// Method <c>WriteInCsv</c> writes the records in a .csv file. Files are named by corresponding hand, and the dateTime of writing.
    /// </summary>
    /// <param name="records">List of all the saved data</param>
    /// <param name="hand">Which hand has the data been recorded from : 0 for right hand, 1 for left hand. </param>
    public void WriteInCsv(List<CoordCSV> records, int hand=0)
    {
        if (records.Count > 0)
        {
            string path = "C:\\Users\\AT04760\\Documents\\2022_HOLOLENS\\CSV Export\\test2_"; //"C:\\Data\\Users\\Mikael Sauriol\\Pictures\\test_";
            string dateTime = System.DateTime.Now.Day + "-" + System.DateTime.Now.Month + "_" + System.DateTime.Now.Hour + "-" + System.DateTime.Now.Minute + "-" + System.DateTime.Now.Second;
            if (hand == 1)
            {
                path += "LeftHand_" + dateTime + ".csv";
            }
            else
            {
                path += "RightHand_" + dateTime + ".csv";
            }
            using (var writer = new StreamWriter(path))
            using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
            {
                csv.Context.RegisterClassMap<CoordMap>();
                csv.WriteRecords(records);
            }
        }
    }

Can anyone help ?

Thanks

--------------------EDIT---------------------- Here's what worked after Jay's Response :

    public void WriteInCsv(List<CoordCSV> records, string folderName, int hand=0)
    {
        if (records.Count > 0)
        {
            string path = "test_"; //"C:\\Users\\AT04760\\Documents\\2022_HOLOLENS\\CSV Export\\test2_";
            string dateTime = System.DateTime.Now.Day + "-" + System.DateTime.Now.Month + "_" + System.DateTime.Now.Hour + "-" + System.DateTime.Now.Minute + "-" + System.DateTime.Now.Second;
            if (hand == 1)
            {
                path += "LeftHand_" + dateTime + ".csv";
            }
            else
            {
                path += "RightHand_" + dateTime + ".csv";
            }

            List<string> lines = new List<string>();
            lines.Add("Finger,TimeStamp,Position,Rotation(Quaternion)");
            foreach (CoordCSV data in records)
            {
                lines.Add(data.Finger +"," + data.Timestamp + "," + data.Position + "," + data.Rotation);
            }

#if !UNITY_EDITOR && UNITY_WSA_10_0
UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
        {
                Windows.Storage.StorageFolder folder;
                if(Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.ContainsItem(folderName)){
                    folder =
 await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync(folderName);
                }
                else{
                    var folderPicker = new Windows.Storage.Pickers.FolderPicker();
                    folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
                    folderPicker.FileTypeFilter.Add("*");
                    folder = await folderPicker.PickSingleFolderAsync();
                }
UnityEngine.WSA.Application.InvokeOnAppThread(async () => 
            {
                if (folder != null)
                {
                    // Application now has read/write access to all contents in the picked folder
                    // (including other sub-folder contents)
                    Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace(folderName, folder);
                    using (StreamWriter writer = new StreamWriter(await folder.OpenStreamForWriteAsync(path, CreationCollisionOption.OpenIfExists)))
                    {
                        foreach(string line in lines){
                            await writer.WriteLineAsync(line);
                        }
                    }
                }
                        }, false);
        }, false);
#endif

Solution

  • 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 such as the "Documents" folder you've used. For more details, please see File access permissions.

    It's recommended to let the user select the folder location by using a file picker. For more info, see Open files and folders with a picker and in particular the SuggestedStartLocation property which can be set to DocumentsLibrary. When your app retrieves the folder via picker, you can add it to the FutureAccessList so that your app can readily access that item in the future.

    While dealing with files or folders in UWP, one important rule is Skip the path: stick to the StorageFile. Thus, instead of using the path, we could use OpenStreamForWriteAsync(IStorageFolder, String, CreationCollisionOption) method to get the Stream and then use the stream in CsvWriter.