Unity3d写入拒绝mac访问的文件路径

时间:2017-09-29 20:35:44

标签: c# macos file unity3d

我尝试将test1.csv保存到文件夹路径,Unity说拒绝访问:

如何在Mac OS Sierra上授予权限?我已经做过CMD + I并给了“大家”读取+写入文件和文件夹,但它没有帮助..谷歌也没有帮助我。

如何允许权限?提前谢谢!

TextWriter tw1 = new StreamWriter ("/Users/X/testunity/test1.csv, true);
tw1.WriteLine ("testfile");
tw1.Close ();


UnauthorizedAccessException: Access to the path '/Users/X/testunity/' 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)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path, Boolean append)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool)
LoggerM.LateUpdate () (at Assets/Scripts/LoggerM.cs:137) 

1 个答案:

答案 0 :(得分:0)

始终写入Unity中的Application.persistentDataPath+folder路径。这将保证代码与Unity支持的大多数平台兼容。

string path = Path.Combine(Application.persistentDataPath, "data");
path = Path.Combine(path, "test1.csv");

//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
    Directory.CreateDirectory(Path.GetDirectoryName(path));
}

using (StreamWriter writer = new StreamWriter(path))
{
    writer.WriteLine("testfile");
}

就个人而言,File.WriteAllBytes要写入文件,所以我建议除非你想逐行写入文件。

string path = Path.Combine(Application.persistentDataPath, "data");
path = Path.Combine(path, "test1.csv");

//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
    Directory.CreateDirectory(Path.GetDirectoryName(path));
}

byte[] data = Encoding.ASCII.GetBytes("testfile");
File.WriteAllBytes(path, data);