- Home /
Can't Create Directory
I'm trying to create a directory if it doesn't exist:
public void Save ()
{
path = Application.streamingAssetsPath + "/Settings";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string finalPath = path + "/Camera_Settings.json";
string newSettings = JsonUtility.ToJson(cameraSettings);
File.WriteAllText(finalPath, newSettings);
Debug.Log("Saved!");
}
But I'm getting this error:
UnauthorizedAccessException: Access to the path 'C:/Users/Igor/Desktop/MGS/Work/Projetos Unity 3D/Don't Die/Assets/StreamingAssets/Settings/Camera_Settings.json' 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)
CameraFollow.Save () (at Assets/Scripts/Mechanics/CameraFollow.cs:91)
Is there any way to work around that? Please, answer as fast as possible. Thanks in advance.
Answer by JedBeryll · Sep 06, 2017 at 07:08 AM
You can put your project in a folder that does not require admin rights. In many cases that's anything other than the system drive. Or you can run Unity as admin.
Yeah, I could do this, but is there no way to write files on, let's say, Program Files, which do need ad$$anonymous$$, without getting any errors?
Running as ad$$anonymous$$? Otherwise no i don't think so. That's the purpose of requiring ad$$anonymous$$ rights. If you could bypass it, any malware could.