- Home /
How to "create directories" in specific folders
Hi guys.
How can I create a Folder and a sub-folder to use binary formatter and create specific saveState ? Here is my code Directory.CreateDirectory ("save/players"); BinaryFormatter formatter = new BinaryFormatter (); FileStream saveFile = File.Create (Application.persistentDataPath + "/save/players." + playerName); formatter.Serialize(saveFile, GameManager.Instance.playersDataList[playerID]); saveFile.Close ();
The console tells me that the path can't be found :/
Answer by Bunny83 · Jun 30, 2017 at 10:09 AM
There are several problems / potential problems:
You use a relative path in CreateDirectory. This would be default use the current working directory. However you seem to want to create the directory inside the persistentDataPath, so you should use the full path to the directory inside CreateDirectory.
Like Shady already mentioned in the file path you seem to have a typo. You want to end your path with a path seperator and not a dot.
First possible problem: This code only works on mac / android / linux builds as you use forward slashes as seperators. On windows you have to use backslashes as seperators. It's recommended to use Path.Combine to form a path. That will create the path according to the current platform.
Second possible problem is that you directly use the "playername" as file name without any sanity checks. On all platforms there are certain characters which are forbidden in file or directory names. Most most notable the path seperator (either / or \ or both) as well as a few others. On windows that includes (* / \ ? " : |). You should normalize / filter the name before you use it as filename. That would include to limit the name to a certain length. On windows a complete path (filename including directories) can't be longer than 260 characters. For more information see this MSDN page
Note that the last point can have infinite side effects if not handled properly. Your game would be exploitable. A player could name himself something like "..\..\..\someImportantSystemPath\someImportantSystemfile"
and your game could overwrite the file. But in most cases it would just make your save method fail.
What you should do is:
string path = Path.Combine(Application.persistentDataPath, "save");
path = Path.Combine(path, "players");
Directory.CreateDirectory (path);
string fileName = Path.Combine(path, normalizeFileName(playerName));
using(FileStream saveFile = File.Create ( fileName ) )
{
// ...
}
The "normalizeFileName" method would do the "cleaning" of the playername. There are many sources for solutions to that issue, though non seems to be exhaustive. Dealing with user input can be quite problematic. A common solution is to reverse the logic. So you don't remove invalid characters but only keep legal ones. (a-z, A-Z,0-9). In that case you only have to check for special names like "COM1", "LPT1" and so on.
Answer by ShadyProductions · Jun 30, 2017 at 08:35 AM
Seems you had a typo?
File.Create (Application.persistentDataPath + "/save/players/" + playerName);