[英]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. 我尝试将应用程序捆绑包中包含的文件作为资源复制到iPhone上的temp文件夹中。 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.
System.UnauthorizedAccessException:拒绝访问路径“ /private/var/mobile/Applications/B763C127-9882-4F76-8860-204AFEA8DD68/Client_iOS.app/testbundle.zip”。
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? 将文件复制到目标流的正确方法是什么?
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. 如果将其设置为
Read
,它将起作用。 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.