2017-07-16 117 views
0

我知道這已經出現了一段時間,但我無法解決我嘗試的任何解決方案的錯誤。UnauthorizedAccessException:訪問路徑是拒絕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; 
} 

要WITE文件

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); 

假設截圖是字節[]

我得到主題中的例外。我正在使用Unity。

錯誤,我得到的是 -

UnauthorizedAccessException:對路徑的訪問 「的/ var /瓶/包/應用/ 9DA2D489-2037-451E-87D1-FA7354ECD0D1 /文檔」 被拒絕。

回答

1

您保存Application.persistentDataPathApplication.dataPath。請注意,您必須創建一個文件夾並保存到該文件夾​​中,而不是直接保存到此路徑。

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

Application.persistentDataPath+"Yourfolder/YourFileNale.extension"

Application.persistentDataPath+"Documents/YourFileNale.extension"

總是使用Path.Combine來合併路徑,而不是我上面使用的"+"

+0

在我可以創建目錄之前,我在CreateImagesDirectory()中得到錯誤,所以即使文件夾創建沒有發生 –

+0

如果出現錯誤並且不發佈它,您希望我做什麼? ............'if(!Directory.Exists(Path.GetDirectoryName(yourPath))) { Directory.CreateDirectory(Path.GetDirectoryName(yourPath)); }'......然後.......'File.WriteAllBytes(yourPath,yourData);' – Programmer

+0

好吧,我用代碼和錯誤更新了方法。 CreateImagesDirectory方法中提供了存在選項。你可以請現在檢查嗎? –