iPhone - 下载文件 - 访问错误

时间:2014-11-07 16:18:59

标签: c# ios xcode xamarin.ios xamarin

虽然我使用Xamarin而不是XCode,但我认为任何Xcode开发人员都能够回答这个问题。 我正在尝试下载一个文件,但是我得到一个例外,据说我没有授权。

这是我的代码:

var webClient = new WebClient();
var documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // iOS 7 and earlier
var fileNameAndPath = Path.Combine (documentsFolder, "example.xml");

if(!File.Exists(documentsFolder)){
        //For some reason the directory doesn't exist and doesn't appear that it is being created
        Directory.CreateDirectory(documentsFolder);

}

webClient.DownloadFile("http://example.com/example/example.xml", fileNameAndPath);

这是我得到的错误:

System.Net.WebException: An error occurred performing a WebClient request. ---> System.Exception: Access to the path '/private/var/mobile/Applications/AF41E8B3-BB83-45F4-89AC-5BC4FE514E95/Documents/example.xml' is denied.
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x0034b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.IO/FileStream.cs:320
  at System.IO.FileStream..ctor (System.String path, FileMode mode) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.IO/FileStream.cs:122
  at System.Net.WebClient.DownloadFileCore (System.Uri address, System.String fileName, System.Object userToken) [0x00002] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.4.0.16/src/mono/mcs/class/System/System.Net/WebClient.cs:318
  at System.Net.WebClient.DownloadFile (System.Uri address, System.String fileName) [0x00035] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.4.0.16/src/mono/mcs/class/System/System.Net/WebClient.cs:303
  --- End of inner exception stack trace ---
  at System.Net.WebClient.DownloadFile (System.Uri address, System.String fileName) [0x00047] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.4.0.16/src/mono/mcs/class/System/System.Net/WebClient.cs:307
  at System.Net.WebClient.DownloadFile (System.String address, System.String fileName) [0x00011] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.4.0.16/src/mono/mcs/class/System/System.Net/WebClient.cs:290
  at com.turboverse.scripturememory.UpdateVersesViewController.DownloadVersesButton_TouchUpInside (MonoTouch.UIKit.UIButton sender) [0x00035] in /Users/apple/Projects/com.turboverse.scripturememory/com.example.example/UpdateVersesViewController.cs:31
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:45
  at com.turboverse.scripturememory.Application.Main (System.String[] args) [0x00008] in /Users/apple/Projects/com.example.example/com.example.example/Main.cs:17

1 个答案:

答案 0 :(得分:0)

来自MSDN File.Exists

  

如果path描述目录,则此方法返回false。

来自MSDN DownloadFile

  

WebException - 该文件不存在。

所以你需要

  1. 不检查文件夹是否存在。假设它存在于iOS
  2. 中是安全的
  3. 在调用DownloadFile
  4. 之前创建文件