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:
var path = Path.Combine(Application.persistentDataPath, filename);
File.WriteAllText($"{path}.png", spectrogram);
This resulted in an error about the path not exisiting.
After some googling I tried the UWP way:
#if WINDOWS_UWP
private async Task SaveState(string filename, string probeData, byte[] spectrogram)
{
try
{
// Either this
var storageFolder = ApplicationData.Current.LocalFolder;
// Or this
var storageFolder = KnownFolders.CameraRoll;
// But neither folder made any difference
var pngFile =
await storageFolder.CreateFileAsync($"{filename}.png", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(pngFile, spectrogram);
}
catch (Exception e)
{
Debug.LogWarning(e);
}
}
#endif
// 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:
#if WINDOWS_UWP
var path = Path.Combine(UnityEngine.Windows.Directory.localFolder, filename);
UnityEngine.Windows.File.WriteAllBytes($"{path}.png", spectrogram);
#else
// 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
