简体   繁体   English

如何在没有System.UnauthorizedAccessException的情况下将文件从应用程序捆绑包复制到另一个目录?

[英]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.

相关问题 递归文件搜索中未经许可忽略目录(System.UnauthorizedAccessException) - Ignore directory without permission (System.UnauthorizedAccessException) in recursive file search System.IO.File.Copy抛出System.UnauthorizedAccessException - System.IO.File.Copy throws System.UnauthorizedAccessException 尝试从LocalFolder打开文件时出现“ System.UnauthorizedAccessException” - 'System.UnauthorizedAccessException' when trying to open file from LocalFolder 从异步任务创建文件时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when creating a file from Async task 抛出System.UnauthorizedAccessException:从JSON文件读取 - System.UnauthorizedAccessException has been thrown: Reading from JSON file 从Metro中的已安装位置读取文件时出现“ System.UnauthorizedAccessException” - 'System.UnauthorizedAccessException' while reading file from Installed location in metro 无法创建目录:System.UnauthorizedAccessException错误 - Cannot create directory: System.UnauthorizedAccessException Error 解压bz2文件时如何修复System.UnauthorizedAccessException? - How to fix System.UnauthorizedAccessException when decompressing bz2 file? File.OpenRead上的System.UnauthorizedAccessException - System.UnauthorizedAccessException on File.OpenRead 在 mstest 期间创建文件 - System.UnauthorizedAccessException - Create file during mstest - System.UnauthorizedAccessException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM