- Home /
The question is answered, right answer was accepted
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);
}
}
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
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}");
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 :)
Follow this Question
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