Reading a file (xml) from StreamingAssets

I’m trying to open an XML from StreamingAssets, but when running the app on hololens, the following error occurs:

error message

UnauthorizedAccessException: Access to the path 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\HoloDemoVS.Release_x86.razie\Data\StreamingAssets\airplanes2.dat' is denied.

   at System.IO.Win32FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs)
   at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at Suffix.AppRoot.Load()
   at Suffix.AppRoot.Awake()
   at Suffix.AppRoot.$Invoke0(Int64 instance, Int64* args)
   at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
(Filename: <Unknown> Line: 0)

i understand that file permissions work differently under UWP, but i’m very new to it (uwp), and a little stumped as to how i could solve/work around this problem.

That’s weird. Can you show the code you use to open the file?

nothing special, like this (it works in the editor):

List<Dictionary<string, object>> data = new List<Dictionary<string, object>> ();
DataContractSerializer serializer = new DataContractSerializer (data.GetType (), knownTypes);

using (FileStream fs = File.Open (Path.Combine (Application.streamingAssetsPath, filename), FileMode.Open))
    data = (List<Dictionary<string, object>>) serializer.ReadObject (fs);

Does that code work when you run it as UWP on a local machine? Or do you get the same error too?

haven’t tried it yet, did now, and it gives a similar error

error message

Exception thrown: 'System.UnauthorizedAccessException' in System.IO.FileSystem.dll
UnauthorizedAccessException: Access to the path 'F:\Unity\HoloSuffix\Builds\UWP\Demo\bin\x86\Release\AppX\Data\StreamingAssets\airplanes2.dat' is denied.
   at System.IO.Win32FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs)
   at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at Suffix.AppRoot.Load()
   at Suffix.AppRoot.Awake()
   at Suffix.AppRoot.$Invoke0(Int64 instance, Int64* args)
   at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
(Filename: <Unknown> Line: 0)

Are you able to read the contents of the file using this API?

Just trying to figure out where the problem lies in.

1 Like

thanks!

using File.ReadAllbytes to read bytes into buffer, and using a MemoryStream to deserialize solved the issue.

You may need:

File.Open(path, FileMode.Open, FileAccess.Read)

Otherwise File.Open(path, FileMode.Open) tries to open the file in FileAccess.ReadWrite by default, and that path probably doesn’t have write permissions.

1 Like

It solved it for me as well.

thank you very much!!
iI have tryed many other solutions,but all not work.
this work for me.

can you give some example?