Search code examples
unity-game-engineexceptionsave

DirectoryNotFoundException: Could not find a part of the path. (C# - Unity)


I am trying to save some simple user data, following this tutorial. I am building for Android.

On the first compilations, everything worked fine. However - I came back to my project today and I got the following error: (I have done some work since then, but I cannot think what has changed to cause the problem).

DirectoryNotFoundException: Could not find a part of the path 

"C:\Users\Ben\AppData\LocalLow\Top Notch Development\Simple Swim\Log:71.test".
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:292)
System.IO.FileStream..ctor (System.String path, FileMode mode)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode)
SaveSessionSystem.SaveSessionData (.InputController inputcontroller) (at Assets/Scripts/Saving Stuff/SaveSessionSystem.cs:25)
InputController.SaveSessionData () (at Assets/Scripts/InputController.cs:152)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

The reason its 'Log71' is because every new file created is assigned a number which increments each time a new file is created.

I have tried the following solutions:


This is the code for saving a file:

string sessionNumber = PlayerPrefs.GetInt("sessionNumberKey").ToString();

    string customFileName = "/Log:" + sessionNumber + ".test";

    // Save the data
    BinaryFormatter formatter = new BinaryFormatter();
    string path = Application.persistentDataPath + customFileName; 
    FileStream stream = new FileStream(path, FileMode.Create);

    SaveSession saveSessionData = new SaveSession(inputcontroller);

    formatter.Serialize(stream, saveSessionData);
    stream.Close();

    Debug.Log("The file has been saved with the filename: " + path);

The expected result is the message 'The file has been saved with the filename: (filename)' and a new file to be created in the persistent data path (on my computer that is C:\Users\Ben\AppData\LocalLow\Top Notch Development\Simple Swim)

However, currently, I am getting the error message and no new file.


Solution

  • You cant have colon in file name in Windows.

    You should delete colon. (:)