Krumelur
Krumelur

Reputation: 33068

How to copy a file from the app bundle to another directory without System.UnauthorizedAccessException?

I try to copy a file that is included in the app bundle as a resource to the temp folder on the iPhone. While this works on the Simulator, on the device I get an exception:

System.UnauthorizedAccessException: Access to the path "/private/var/mobile/Applications/B763C127-9882-4F76-8860-204AFEA8DD68/Client_iOS.app/testbundle.zip" is denied.

The code I use is below It cannot open the source file.

using(var sourceStream = File.Open("./demobundle.zip", FileMode.Open))
{
    sourceStream.CopyTo(targetStream);
}   

What is the correct way of copying a file into a destination stream?

Upvotes: 0

Views: 1081

Answers (1)

Krumelur
Krumelur

Reputation: 33068

Why is it that I always find the answers to my questions practically immediately after I asked here? :-)

One has to specify the file access mode. If it is set to Read, it'll work. The default seems to be some write mode and that is obviously not possible.

using(var sourceStream = File.Open("./demobundle.zip", FileMode.Open, FileAccess.Read))
{
    sourceStream.CopyTo(targetStream);
}

Upvotes: 3

Related Questions