c# - 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;
}
}
}
现在谢谢。如果您还有其他需要,请告诉我。
解决方案
推荐阅读
- chromium - 我正在使用 AWS Glue,我正在尝试使用 selenium 和 chrome 驱动器运行脚本。错误消息:“chromedriver”可执行文件需要在 PATH 中
- vue.js - 动态数据分配不适用于 Vue js
- excel - 动态命名范围/公式多久重新计算一次?
- regex - 需要在多个点后捕获任何数据
- vue.js - 使用 Vue 和捆绑的 Service Worker
- spring-boot - RequestD 继续在 Jpa 中获取 Null
- angular - Angular - 模块“ngx-intl-tel-input”没有导出成员“TooltipLabel”
- sql-server - 在单表中使用 TSQL 脚本在 SQL 数据库中进行更改跟踪/历史记录
- assembly - 使用 MASM 8086 中的 16 位寄存器将两个 32 位数字相乘的方法是什么?
- reactjs - 如何在反应中获得有条件的主页项目按钮?