How to save files?

Hi,

i use C# and the FileStream class, but i can’t get it to run. Each time i want to save a file i get the following error message:

UnauthorizedAccessException: Access to the path “/quicksave.txt” is denied.
System.IO.FileStream…ctor (System.String path, FileMode mode)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode)
GlobalData.SaveLevel ()
StartScreen.OnButtonPauseSave ()
StartScreen+TouchInterface.OnEnded (Int32 fingerId, Vector2 pos)
TouchControl.Update ()
StartScreen.Update ()

(Filename: Line: -1)

this is my code:

	public void SaveLevel()
	{
		BackupGameData();
		
		FileStream fs = new FileStream( quickSavename, FileMode.Create );
		XmlSerializer xmlSer = new XmlSerializer( typeof( GameData ) );
		xmlSer.Serialize( fs, gameData );
		fs.Close();
	}
	
	public void LoadLevel()
	{
		FileStream fs = new FileStream( quickSavename, FileMode.Open );
		XmlSerializer xmlSer = new XmlSerializer( typeof( GameData ) );
		gameData = (GameData)xmlSer.Deserialize( fs );
		fs.Close();
		
		RestoreGameData();
	}

I know to use the PlayerPrefs class to store information, but i feel more comfortable to use load and save files by my own.

What should i do?

Thanks

iPhone applications can only read and write in their own sandbox directory. (Check Apple’s documentation for more info.) You should be able to get that path by appending “/Documents/” to the Application.dataPath.

Hi Bliprob (or anyone else),

I’ve been looking for hours on how to setup a path to save my prefs for the iPhone. I use playerPrefs for my preferences. Of course it works great on the Mac but I just can’t figure out the how and where I’m supposed to set up the path. I searched the docs for Application.dataPath in xCode and I found nothing. Can you help me please?

Thanks,

Robert

If you’re using PlayerPrefs, you don’t need to know the path.

–Eric

you can not modify the .NET paths from XCode or alike.

What you need to do is retrieve the correct path in XCode and write it into an NSUserDefaults variable.
On the unity end you can retrieve the variable through PlayerPrefs again.
I think there is even a sample for that already