Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Oct 07, 2021 at 04:05 AM by DerBears for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DerBears · Oct 06, 2021 at 12:13 AM · errordirectorydatapath

Getting Directory not Found error with Binary Formatting attempt

 using UnityEngine;
 using System.IO;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 
 
 public static class SaveManager
 {
     //these set the names for the directory and file name to be stored.
     public static string directory = "SaveData";
     public static string fileName = "MySave.txt";
     
     
     
     public static void Save(SaveObject so)
     {
         //calling a binary formatter and saving it as a variable
         BinaryFormatter bf = new BinaryFormatter();
         //opening the data path to the file we are looking for and saving a new directory/filename 
         //in designated area
         FileStream file = File.Create(GetFullPath());
         //rewrite over the save if it exists already
         bf.Serialize(file, so);
         //close the data path to the file,else errors will occur
         file.Close();
         
     }
     
     public static SaveObject Load()
     {
         if( SaveExists())
         {
             try
             {
                 //Opens existing file path and deserializes the data from the BinaryFormatter
                 BinaryFormatter bf = new BinaryFormatter();
                 FileStream file = File.Open(GetFullPath(), FileMode.Open);
                 
                 SaveObject so = (SaveObject)bf.Deserialize(file);
                 file.Close();
                 
                 return so;
             }
             catch(SerializationException)
             {
                 Debug.Log("Failed to properly load file");    
             }
         }
         return null;
     }
     
     private static bool SaveExists()
     {
         return File.Exists(GetFullPath());
     }
     
     private static bool DirectoryExists()
     {
         return Directory.Exists( Application.persistentDataPath + "/" + directory);
     }
     
     private static string GetFullPath()
     {
         return (Application.persistentDataPath +"/" + directory + "/" + fileName);
     }
 }
 
 
Comment
Add comment · Show 1
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 DerBears · Oct 06, 2021 at 06:15 AM 0
Share

The exact error reads
"DirectoryNotFoundException: Could not find a part of the path "C:\Users\aw\AppData\LocalLow\DefaultCompany\TileMatchingJR\SaveData\MySave.txt". System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at :0) System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) (at :0) (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int) System.IO.File.Create (System.String path, System.Int32 bufferSize) (at :0) System.IO.File.Create (System.String path) (at :0) SaveManager.Save (SaveObject so) (at Assets/SaveManager.cs:21) SaveTest.Update () (at Assets/SaveTest.cs:17)

Which I dont understand as a newbie :P

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by justinenayat · Oct 06, 2021 at 01:20 AM

Try this when you search for the path:

 return (Application.persistentDataPath +$"\{directory}\{fileName}");

You might need to combine the path though. Here's an example on how to do that:

 var path = Path.Combine(Application.persistentDataPath, $"\{directory}\{filename}");


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 DerBears · Oct 07, 2021 at 04:04 AM

For some reason Unity threw a fit about the $, but I caught the 1 line in the tutorial I missed (my bad) that created the directory if the directory was missing. Thanks anyways though :)

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

Follow this Question

Answers Answers and Comments

169 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 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 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

Error upon loading assets, Saying access is denied (in my project save location) 1 Answer

CommandInvokationFailure: Unable to install APK to device 6 Answers

Package directory not found for DataContractSerialization 1 Answer

IOS cant move file from cache path 0 Answers

fatal error updating assets 4 Answers


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