C# Unity3d写入mac上的文件路径访问被拒绝
我尝试将test1.csv保存到文件夹路径,Unity表示拒绝访问: 如何授予Mac OS Sierra的权限?我已经做了CMD+I,并给“每个人”读写文件和文件夹,但它没有帮助。。谷歌也帮不了我 我如何允许权限?提前谢谢你C# Unity3d写入mac上的文件路径访问被拒绝,c#,macos,file,unity3d,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 (); UnauthorizedAccessEx
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)
始终写入Unity中的
应用程序.persistentDataPath+文件夹路径。这将保证代码与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.writealBytes
写入文件,因此我建议您这样做,除非您想逐行写入文件
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);
不幸的是,原始版本与Application.persistentDataPath一起使用,并导致相同的错误。我还尝试了你的代码片段1:1,并说访问被拒绝:(好的,只需使用我第一个答案中的内容。按原样复制它。它应该可以工作。如果没有,请使用第二个,但只需按原样复制它。不要更改其中的任何内容