- Home /
IsolatedStorageException: Could not find a part of the path
I want to be able to save a file into my executable folder. But it doesn't save the file and throws the exception "IsolatedStorageException: Could not find a part of the path..."
When I run this application using the Unity Editor it saves it onto the correct save file which is located on the Asset folder but when I run it in Build it doesn't save it.
Here is the code I have:
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Collections.Generic;
public class TrainingData : MonoBehaviour
{
string _FileLocation,_FileName;
string _data;
UserData iData = new UserData();
// When the EGO is instansiated the Start will trigger
// so we setup our initial values for our local members
void Start () {
// Where we want to save and load to and from
_FileLocation=Application.dataPath;
_FileName="SaveData.xml";
}
void Update () {}
public void Save(int userID, int x, int y, int z)
{
if (!Load())
return;
_data = string.Empty;
bool isUserFound = false;
foreach (var user in iData._users)
{
// Time to creat our XML!
if (userID == user.userID)
{
user.userID = userID;
user.x = x;
user.y = y;
user.z = z;
isUserFound = true;
}
//_data += SerializeObject(user);
}
if (!isUserFound)
{
UserData.mUsers newUser = new UserData.mUsers();
newUser.userID = userID;
newUser.x = x;
newUser.y = y;
newUser.z = z;
iData._users.Add(newUser);
}
_data = SerializeObject(iData);
// This is the final resulting XML from the serialization process
CreateXML();
Debug.Log(_data);
}
private bool Load()
{
// Load our UserData into myData
LoadXML();
if (_data.ToString() == "")
{
return false;
}
// notice how I use a reference to type (UserData) here, you need this
// so that the returned object is converted into the correct type
var data = FixData();
iData._users.Clear();
var counter = 0;
foreach (var dataToParse in data)
{
counter++;
if (counter == 1) continue;
if (string.IsNullOrEmpty(dataToParse.Trim())) continue;
var uData = (UserData)DeserializeObject(dataToParse);
iData._users.Add(uData._users[0]);
//var ud = iData._users[iData._users.Count - 1];
// just a way to show that we loaded in ok
}
return true;
}
string[] FixData()
{
var dataSplit = _data.Split(new string[] { "<mUsers>", "</mUsers>" }, System.StringSplitOptions.RemoveEmptyEntries);
dataSplit[0] = dataSplit[0].Replace("<_users>", string.Empty);
for (var indx = 1; indx < dataSplit.Length; indx++ )
{
if (string.IsNullOrEmpty(dataSplit[indx].Trim()) || !dataSplit[indx].Contains("x"))
{
dataSplit[indx] = string.Empty;
continue;
}
dataSplit[indx] = dataSplit[0] + "<_users><mUsers>" + dataSplit[indx] + "</mUsers></_users></UserData>";
Debug.Log(dataSplit[indx]);
}
return dataSplit;
}
string UTF8ByteArrayToString(byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
//// Here we serialize our UserData object of myData
string SerializeObject(UserData userData)
{
string XmlizedString = string.Empty;
XmlSerializer xs = new XmlSerializer(typeof(UserData));
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, userData);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
//foreach (var user in l_userData)
//{
//}
return XmlizedString;
}
// Here we deserialize it back into its original form
object DeserializeObject(string pXmlizedString)
{
XmlSerializer xs = new XmlSerializer(typeof(UserData));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
//XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}
// Finally our save and load methods for the file itself
void CreateXML()
{
StreamWriter writer;
FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName);
if(!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(_data);
writer.Close();
Debug.Log("File written.");
}
void LoadXML()
{
StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName);
string _info = r.ReadToEnd();
r.Close();
_data=_info;
Debug.Log("File Read");
}
}
// UserData is our custom class that holds our defined objects we want to store in XML format
public class UserData
{
public List<mUsers> _users = new List<mUsers>();
public class mUsers
{
// We have to define a default instance of the structure
public int userID;
public int x;
public int y;
public int z;
}
// Default constructor doesn't really do anything at the moment
public UserData() { }
}
Can anyone see what I am missing. I do believe that the problem is that it is not creating the xml file at all, but I am not sure. Thank you in advance.
Got the Same Problem.
Someone Please Help.
Thanks.
did you find a solution , i'm struggling with the same problem for days
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Making my XML file multiline 0 Answers
Best practice to store and load a specific set of objects? 3 Answers
How do I keep references to unique NPCs between scenes? 1 Answer
Serializing an Action as XML? 0 Answers