簡體   English   中英

UnauthorizedAccessException:訪問路徑被拒絕 Ios

[英]UnauthorizedAccessException: Access to the path is denies Ios

我知道這已經出現了一段時間,但我無法使用我嘗試過的任何解決方案來解決錯誤。

我剛開始測試我的應用程序 - 將屏幕截圖保存到 ios 設備代碼是 -

string GetiPhoneDocumentsPath()
{
    string path = Application.dataPath.Substring(0, Application.dataPath.Length - 5);
    path = path.Substring(0, path.LastIndexOf("/"));
    return path + "/Documents/";
}

string CreateImagesDirectory(string documentsPath) {
    //System.IO.File.SetAttributes (documentsPath, FileAttributes.Normal);
    string imagePath = documentsPath +"magicimages/";
    if (Directory.Exists(imagePath)) {return imagePath;}
    DirectoryInfo t = new DirectoryInfo(documentsPath);
    //Directory.CreateDirectory(imagePath);
    t.CreateSubdirectory("magicimages");
    System.IO.File.SetAttributes (imagePath, FileAttributes.Normal);

    return imagePath;
}

寫入文件

    Debug.Log("Do nothing actually as we need to save to persistent data path");
        string documentsPathIphone = GetiPhoneDocumentsPath();
        Debug.Log ("document path" + documentsPathIphone);
        string imagePath = CreateImagesDirectory (documentsPathIphone);
        //Path = imagePath + fileName;
        Debug.Log ("path iphone" + Path);
        Debug.Log("Appllicarion data path -->" + Application.dataPath);
        //string savepath = Application.dataPath.Replace ("game.app/Data", "/Documents/");
        System.IO.File.WriteAllBytes (System.IO.Path.Combine(Path , fileName), screenshot);

假設屏幕截圖是 bytes[]

我得到了主題中的例外。 我正在使用統一。

我得到的錯誤是 -

UnauthorizedAccessException:拒絕訪問路徑“/var/containers/Bundle/Application/9DA2D489-2037-451E-87D1-FA7354ECD0D1/Documents”。

您使用Application.persistentDataPath而不是Application.dataPath保存。 請注意,您必須創建一個文件夾並保存在該文件夾內,而不是直接保存到此路徑。

所以你保存到的最終路徑應該是:

Application.persistentDataPath+"Yourfolder/YourFileNale.extension"

要么

Application.persistentDataPath+"Documents/YourFileNale.extension"

始終使用Path.Combine來組合路徑而不是我上面使用的"+"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM