Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RemDust · Jun 30, 2017 at 08:21 AM · persistentdatapathbinaryformatter

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 :/

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ShadyProductions · Jun 30, 2017 at 08:35 AM

Seems you had a typo?

 File.Create (Application.persistentDataPath + "/save/players/" + playerName);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

66 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving game data and not user data 2 Answers

Is there a way I can remove files from Application.persistantDataPath? 1 Answer

Empty Application.persisentDataPath on Android device causing UnauthorizedAccessException 1 Answer

Generic Save and Load using BinaryFormatter not working 1 Answer

C# BinaryFormatter Deserialization Issues 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges