DirectoryNotFoundException:找不到路径的一部分。 (C#-Unity)

时间:2019-03-31 20:56:01

标签: unity3d exception save

我正在尝试按照this教程保存一些简单的用户数据。我正在为Android构建。

在第一次编译中,一切正常。但是-我今天回到我的项目,并且遇到以下错误:(从那时起我做了一些工作,但是我不认为是什么引起了问题。)

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()

之所以使用“ Log71”,是因为为每个创建的新文件分配了一个数字,该数字在每次创建新文件时都会递增。

我尝试了以下解决方案:


这是保存文件的代码:

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);

预期结果是消息“文件已用文件名({filename)保存”,并在持久数据路径中(在我的计算机C:\Users\Ben\AppData\LocalLow\Top Notch Development\Simple Swim上创建一个新文件)

但是,目前,我收到错误消息,没有新文件。

1 个答案:

答案 0 :(得分:1)

在Windows中,文件名不能包含冒号。

您应该删除冒号。 (:)