UNITY-UnauthorizedAccessException:对路径的访问被拒绝

时间:2019-03-29 17:00:21

标签: c# unity3d

尝试为我的游戏创建保存和加载系统时出现以下错误。我收到以下错误

UnauthorizedAccessException: Access to the path 'C:/Users/..etc' is denied. 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:259). System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
(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, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:135)
System.IO.File.Create (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:130)
SaveSystem.SavePlayer (.Player player) (at Assets/Scripts/SaveSystem.cs:19)
Player.SavePlayer () (at Assets/Scripts/Player.cs:47)
Player.Update () (at Assets/Scripts/Player.cs:13)

下面是我用来保存游戏状态的方法

public static void SavePlayer (Player player){

    BinaryFormatter formatter = new BinaryFormatter();
    string path = Application.dataPath + "game.txt";

    File.Create(Application.dataPath);

    FileStream stream = new FileStream(path, FileMode.Create);

    PlayerData data = new PlayerData(player);

    formatter.Serialize(stream, data);
    stream.Close();
}

0 个答案:

没有答案