2016-05-07 8 views
0

ファイルの読み書きに以下のコードを使用しました。iOSのファイル読み書きエラー

private void StorePuzzleData() 
{ 
    FileInfo fileInfo = new FileInfo (Application.persistentDataPath + "\\" + difficultyLevel + puzzleId + ".txt"); 

    if (fileInfo.Exists) 
     fileInfo.Delete(); 

    string fileData = string.Empty; 

    foreach (CellInformation cellInfo in cellInfoList) 
     fileData += cellInfo.RowIndex + "#" + cellInfo.ColIndex + "#" + cellInfo.number + "#" + cellInfo.CellColor + "#" + cellInfo.CellDisplayColor + "#" + (cellInfo.IsGroupComplete ? 1 : 0) + ","; 

    StreamWriter streamWriter = fileInfo.CreateText(); 
    streamWriter.WriteLine (fileData); 
    streamWriter.Close(); 

    DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime); 

} 

private void ReadPuzzleData() 
{ 
    // format: rownumber, colnumber, number, cellcolor, celldisplaycolor, isgroupcomplete 

    StreamReader streamReader = File.OpenText (Application.persistentDataPath + "\\" + difficultyLevel + puzzleId + ".txt"); 
    string fileData = streamReader.ReadLine(); 
} 

実際のiOSデバイスの実行中に次のエラーが発生しています。このコードは、アンドロイドデバイスでも同様にiMacで正しく動作します。

enter image description here

enter image description here

私がこれが正しい作るために何をする必要があるかの変更いくつかの提案をお願いします。

+0

あなたのパスの最後のスラッシュはバックスラッシュで、他のすべては前方スラッシュですが、コードのどこでも "\\"の代わりに "//"を使用してみてください –

+0

その後、Windowsでクラッシュします:) –

答えて

3

Unixライクな(Apple Mac OS)環境でWindowsスタイルのパスを使用しているようです。あなたは

システムしかし、あなたはあなたの障害パスにあなたが前方に混入していることがわかり

/var/mobile/Containers 

のようなものとバックスラッシュは、作るUnixライクなオン

C:\Users\Maxi\Desktop 

などのバックスラッシュを含むパスを持っているWindows上でいることに注意してくださいパスは無効です。

/var/mobile/Containers/Data/Application/2.....\debutan1.txt 

正しいパスを常に生成する正しい方法は、Path.Combine(string, string)関数を使用することです。これにより、適切なディレクトリパスセパレータを使用して2つのパスが結合されます。セパレータは、Path.DirectorySeparatorCharによって個別にアクセスすることもできます。これはまだ与えた場合、あなたのコードが正しいようにするために、あなたは

using System.IO; /* must be imported */ 

private void StorePuzzleData() 
{ 
    FileInfo fileInfo = new FileInfo (Path.Combine(Application.persistentDataPath, difficultyLevel + puzzleId + ".txt")); 

    if (fileInfo.Exists) 
     fileInfo.Delete(); 

    string fileData = string.Empty; 

    foreach (CellInformation cellInfo in cellInfoList) 
     fileData += cellInfo.RowIndex + "#" + cellInfo.ColIndex + "#" + cellInfo.number + "#" + cellInfo.CellColor + "#" + cellInfo.CellDisplayColor + "#" + (cellInfo.IsGroupComplete ? 1 : 0) + ","; 

    StreamWriter streamWriter = fileInfo.CreateText(); 
    streamWriter.WriteLine (fileData); 
    streamWriter.Close(); 

    DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime); 

} 

private void ReadPuzzleData() 
{ 
    // format: rownumber, colnumber, number, cellcolor, celldisplaycolor, isgroupcomplete 

    StreamReader streamReader = File.OpenText (Path.Combine(Application.persistentDataPath, difficultyLevel + puzzleId + ".txt")); 
    string fileData = streamReader.ReadLine(); 
} 

を行うだろうだから、

、それが原因のFilePermissionでなければなりませんエラー「アクセスが拒否されました」。その後、ls -la <thatpath>の出力をポストします。

+0

今私はこの提案でビルドをテストしています:http://answers.unity3d.com/questions/161526/write-to-iphone-file-system-access-denied.html – Siddharth

+0

これは完璧に機能しました... :) – Siddharth

関連する問題