2

I'm working on Hololens 2 application in Unity 3D. I'm trying to write a configuration file to the device and access it later via the device portal in a web browser. Within the application itself, I'm being told that the file is being written to AppData/Local/Packages/[App Name]/LocalState/[File Name].txt. However, when I go and take a look at LocalAppData/[App Name] the folder LocalState/ doesn't even exist. I suspect this has to do with an error in the Package.appxmanifest file of the VS project generated by Unity.

Here is the relevant C# code I'm using to write to file:

   void ReadResolution()
    {
        string path = Path.Combine(Application.persistentDataPath, mResFilename);
        mExcept = $"No exceptions thrown by file IO while writing to \n\"{path}\".";
        if(System.IO.File.Exists(path) && new FileInfo(path).Length > 0)
        {
            try
            {
                StreamReader reader = new StreamReader(path);
                if(reader != null)
                {
                    string content = reader.ReadToEnd();
                    string[] split = content.Split(' ');
                    mWidth = Int32.Parse(split[0]);
                    mHeight = Int32.Parse(split[1]);
                }
            }
            catch (Exception e)
            {
                mExcept = "File Read Exception: " + e.Message;
            }
        }
        else
        {
            try
            {
                using (TextWriter writer = File.CreateText(path))
                {
                    string line = mWidth.ToString() + " " + mHeight.ToString();
                    writer.WriteLine(line);
                }
            }
            catch (Exception e)
            {
                mExcept = "File Write Exception: " + e.Message;
            }
        }
        Debug.Log(mExcept);
    }

I'm using the member field mExcept to record any messages or exceptions thrown during the process. It's telling me the file is written successfully, but maybe I'm just fooling myself somehow?

Here are the relevant sections from the project's Package.appxmanifest file:

<Package xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" 
         xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" 
         xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2" 
         xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" 
         xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4" 
         xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10" 
         xmlns:mobile="http://schemas.microsoft.com/appx/manifest/mobile/windows10"
         xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
         IgnorableNamespaces="uap uap2 uap3 uap4 mp mobile iot rescap" 
         xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10">

.
.
.

  <Capabilities>
    <rescap:Capability Name="broadFileSystemAccess" />
    <uap:Capability Name="documentsLibrary" />
    <Capability Name="internetClient" />
    <Capability Name="internetClientServer" />
    <Capability Name="privateNetworkClientServer" />
    <uap2:Capability Name="spatialPerception" />
    <uap3:Capability Name="remoteSystem" />
    <DeviceCapability Name="microphone" />
    <DeviceCapability Name="gazeinput" />
    <DeviceCapability Name="wiFiControl" />
    <DeviceCapability Name="webcam" />
  </Capabilities>

Any help is appreciated. Thanks in advance.

reed
  • 61
  • 1
  • 6
  • `LocalAppData/[App Name]` != `AppData/Local/Packages/[App Name]` though! ... are you sure you are searching in the correct place? – derHugo Apr 01 '21 at 18:55
  • So my understanding is that Windows screws with the path depending on where you're looking at it from (the app vs. the web portal). I'm looking in the place where files written to storage has appeared for other apps. So for some other app I'm working with, things will go in LocalAppData\[App Name]\LocalState\. The LocalState folder doesn't exist for my app. – reed Apr 01 '21 at 21:01

2 Answers2

2

Ok everybody, I figured it out.

In order to write to persistent storage, VS project generated by Unity needs to have another capability included in its Package.appxmanifest file that mine was missing. Namely, you need to declare the uap capability "removableStorage", in addition to whatever else you're using. As an example, this is what the capabilities section of my manifest file now looks like:

<Capabilities>
    <uap:Capability Name="documentsLibrary" />
    <Capability Name="internetClient" />
    <Capability Name="internetClientServer" />
    <Capability Name="privateNetworkClientServer" />
    <uap:Capability Name="removableStorage" />
    <uap:Capability Name="videosLibrary" />
    <uap:Capability Name="objects3D" />
    <uap2:Capability Name="spatialPerception" />
    <DeviceCapability Name="webcam" />
    <DeviceCapability Name="microphone" />
    <DeviceCapability Name="location" />
    <DeviceCapability Name="wiFiControl" />
  </Capabilities>

Also, make sure you uninstall completely any old build on your device before installing the fixed version. That was causing me problems too.

Have a nice day :)

reed
  • 61
  • 1
  • 6
1

I also used Application.persistentDataPath in Unity which had to work on Hololens 2.

But however it did not work, ie; the file was not getting saved because I made an error (The list which I wanted to save, was not initialized to a new one).

After I corrected it, the file with the list gets saved on the persistantDatapath, and works without adding the above capability.

holouser
  • 172
  • 11