- Home /
Why is my file path invalid?
Ok, so I had a Unity project on my mac computer and tried transferring into a windows. I've tried just using the same file and then also transferring just the assets into a new project. Neither are working and both give me this error saying my directory is no good: "IOException: The directory name is invalid System.IO.Directory.CreateDirectoriesInternal (System.String path) (at :0) System.IO.Directory.CreateDirectory (System.String path) (at :0) SaveLoad.save[T] (T pyramid, System.String key, System.String fileName) (at Assets/Scripts/SaveLoad.cs:18)..."
This program works on my mac and thought my code was universal:
public static class SaveLoad { public static string fileType = ".jesus";
public static void save<T>(T pyramid, string key, string fileName)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/" + fileName + "/";
Directory.CreateDirectory(path);
FileStream stream = new FileStream(path + key + fileType, FileMode.Create);
formatter.Serialize(stream, pyramid);
stream.Close();
}
public static T load<T>(string key, string fileName)
{
string path = Application.persistentDataPath + "/" + fileName + "/";
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path + key + fileType, FileMode.Open);
T data = (T)formatter.Deserialize(stream);
stream.Close();
return data;
}
public static bool checkFile(string key, string fileName)
{
return File.Exists(Application.persistentDataPath + "/" + fileName + "/" + key + ".jesus");
}
public static bool checkDirectory(string fileName)
{
return Directory.Exists(Application.persistentDataPath + "/" + fileName + "/");
}
public static void deleteAll(string fileName)
{
string path = Application.persistentDataPath + "/" + fileName + "/";
DirectoryInfo directory = new DirectoryInfo(path);
directory.Delete(true);
Directory.CreateDirectory(path);
}
}
Answer by Bunny83 · Oct 11, 2020 at 01:58 AM
Windows is using backslashes \` while unix / mac / linux uses forward slashes
/`. It' generally recommended to not use slashes at all but to use System.IO.Path.Combine instead.
string path = System.IO.Path.Combine(Application.persistentDataPath, fileName);
string file = System.IO.Path.Combine(path, key + fileType);
Answer by ThisLove · Oct 16, 2020 at 04:47 PM
@Bunny83 Thank you, that makes a lot of sense. When trying to create something to fix this: public static string convertPath(string fileName) { string path = Application.persistentDataPath; path = System.IO.Path.Combine(path, fileName); UnityEngine.Debug.Log(path); return path; }
public static string convertPath(string[] fileNames)
{
string path = Application.persistentDataPath;// + "/" + fileName;// + "/";
for (int i = 0; i < fileNames.Length; i++)
{
path = System.IO.Path.Combine(path, fileNames[i]);
}
UnityEngine.Debug.Log(path);
return path;
}
I added debugs to get e result and got this: "C:/Users/nopassword/AppData/LocalLow/DefaultCompany/Tiddle\Saves\Main/0:0" and "C:/Users/nopassword/AppData/LocalLow/DefaultCompany/Tiddle\Saves" along with the same error. I tried just correcting the problem by just adding my own slashes and had no suches.
Edit:I forgot to mention. This function is to get the file directory. I realize the names are confusing, I was just trying to make something that worked
Your answer

Follow this Question
Related Questions
The Semaphore Timeout Period Has Expired 0 Answers
CreateInstanceFromType is not allowed to be called during serialization 1 Answer
Error: GetComponentFastPath is not allowed? 2 Answers
UnityScript How To Deserialize? 1 Answer
I am getting a random serialization error and don't know why. How do I fix this? What is it saying? 2 Answers