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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Trouble writing to file on HoloLens 2

Discussion in 'AR' started by unity_36C7F9B56EF1D90D0748, Apr 19, 2023.

  1. unity_36C7F9B56EF1D90D0748

    unity_36C7F9B56EF1D90D0748

    Joined:
    Oct 17, 2022
    Posts:
    2
    Hi,

    I've been trying to get my program to write to a file for two weeks now, and none of the solutions I've tried have worked. Yes, I have tried to enable every single capability, but that has not made any difference. Writing to a file shouldn't be this hard, and I need this to work in order to quantify test results for my master's thesis

    In the following code, filename is a string and spectrogram is an array of bytes. The writes are triggered by a button press event from a MRTK button.

    My first attempt was the usual System.IO stuff:

    Code (CSharp):
    1. var path = Path.Combine(Application.persistentDataPath, filename);
    2. File.WriteAllText($"{path}.png", spectrogram);
    This resulted in an error about the path not exisiting.

    After some googling I tried the UWP way:

    Code (CSharp):
    1. #if WINDOWS_UWP
    2.         private async Task SaveState(string filename, string probeData, byte[] spectrogram)
    3.         {
    4.             try
    5.             {
    6.                 // Either this
    7.                 var storageFolder = ApplicationData.Current.LocalFolder;
    8.                 // Or this
    9.                 var storageFolder = KnownFolders.CameraRoll;
    10.                 // But neither folder made any difference
    11.                 var pngFile =
    12.                     await storageFolder.CreateFileAsync($"{filename}.png", CreationCollisionOption.ReplaceExisting);
    13.                 await FileIO.WriteBytesAsync(pngFile, spectrogram);
    14.             }
    15.             catch (Exception e)
    16.             {
    17.                 Debug.LogWarning(e);
    18.             }
    19.         }
    20. #endif
    21.  
    22. // then called from a non-async method
    which gave absolutely no error messages until I added the try-catch, which printed the error you see in the attached image. Might this just be async shenanigans?

    My last attempt was using Unity's own UWP classes:

    Code (CSharp):
    1. #if WINDOWS_UWP
    2.     var path = Path.Combine(UnityEngine.Windows.Directory.localFolder, filename);
    3.     UnityEngine.Windows.File.WriteAllBytes($"{path}.png", spectrogram);
    4. #else
    5. // normal Unity file write
    to which it complained that it "failed to open C:\Data\Users\<user>\AppData\Local\Packages\<packagename>\LocalState\2023-04-19T11:47:32.png for writing", which... at least is getting somewhere
     

    Attached Files:

  2. thomas_key

    thomas_key

    Unity Technologies

    Joined:
    Dec 14, 2019
    Posts:
    32
    andyb-unity likes this.
  3. unity_36C7F9B56EF1D90D0748

    unity_36C7F9B56EF1D90D0748

    Joined:
    Oct 17, 2022
    Posts:
    2
    Found the issue eventually. The timestamp I tried to use as filename had : in it, which is invalid on Windows filesystems
     
    thomas_key likes this.