首页 > 解决方案 > UnauthorizedAccessException:对路径“C:\QuizGame\Assets”的访问被拒绝。--团结,C#

问题描述

我是编码新手,我只有 2 个月的编码经验。所以我正在编写一个 android 问答游戏,并希望我的问答游戏从 Json 文件中加载。我正在接受这些错误:

UnauthorizedAccessException: Access to the path 'C:\QuizGame\Assets' is denied.
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 <fb001e01371b4adca20013e0ac763896>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options, System.String msgPath, System.Boolean bFromProxy, System.Boolean useLongPath, System.Boolean checkHost) (at <fb001e01371b4adca20013e0ac763896>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize, System.Boolean checkHost) (at <fb001e01371b4adca20013e0ac763896>:0)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize) (at <fb001e01371b4adca20013e0ac763896>:0)
System.IO.StreamReader..ctor (System.String path, System.Boolean detectEncodingFromByteOrderMarks) (at <fb001e01371b4adca20013e0ac763896>:0)
System.IO.StreamReader..ctor (System.String path) (at <fb001e01371b4adca20013e0ac763896>:0)
(wrapper remoting-invoke-with-check) System.IO.StreamReader..ctor(string)
System.IO.File.ReadAllText (System.String path) (at <fb001e01371b4adca20013e0ac763896>:0)
Data.Fetch (System.Boolean& result, System.String filePath) (at Assets/Scripts/Utility/GameUtility.cs:41)
Data.Fetch (System.String filePath) (at Assets/Scripts/Utility/GameUtility.cs:37)
GameManager.LoadData () (at Assets/Scripts/GameManager.cs:282)
GameManager.Start () (at Assets/Scripts/GameManager.cs:78)

这是我写的代码:

using System.IO;
using UnityEngine;

public class GameUtility
{

    public const float ResolutionDelayTime = 1;
    public const string SavePrefKey = "Game_Highscore_Value";

    public const string FileName = "Q";
    public static string FileDir
    {
        get
        {
            return Application.dataPath + "/";
        }
    }
}
[System.Serializable()]
public class Data
{
    public Question[] Questions = new Question[0];

    public Data() { }

    public static void Write(Data data, string path)
    {
        
        using (Stream stream = new FileStream(path, FileMode.Create,FileAccess.ReadWrite))
        {
            JsonUtility.ToJson(stream);
        }
    }
    public static Data Fetch(string filePath)
    {
        return Fetch(out bool result, filePath);
    }
    public static Data Fetch(out bool result, string filePath)
    {
        filePath = File.ReadAllText(Application.dataPath);
        if (!File.Exists(filePath)) { result = false; return new Data(); }

        
        using (Stream stream = new FileStream(filePath, FileMode.Open,FileAccess.ReadWrite))
        {
            filePath = Application.dataPath;
            var data = JsonUtility.FromJson<Data>(filePath);

            result = true;
            return data;
        }
    }
}

现在谢谢。如果您还有其他需要,请告诉我。

标签: c#jsonunity3d

解决方案


推荐阅读