UnauthorizedAccessException: Access to the path is denied

Hello,

I have a problem saving my XML file;

using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;

[XmlRoot("BuildedObjectCollection")]
public class BuildedObjectContainer {

    [XmlArray("BuildedObjects")]
    [XmlArrayItem("BuildedObject")]
    public List<BuildedObject> buildedObjects;

    public void Save(string path)
    {
        var serializer = new XmlSerializer(typeof(BuildedObjectContainer));
        using (var stream = new FileStream(path, FileMode.Create))
        {
            serializer.Serialize(stream, this);
        }
    }
}

The save is called here

        public void Save()
        {
            Debug.Log(Application.dataPath);
            boContainer.Save(Application.dataPath);
        }

The full error I get is

UnauthorizedAccessException: Access to the path 'D:/Unity Projects/RenewedTTD/Assets' 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-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:259)
System.IO.FileStream..ctor (System.String path, FileMode mode)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode)
BuildedObjectContainer.Save (System.String path) (at Assets/Scripts/BuildedObjectContainer.cs:15)
Build.Save () (at Assets/Scripts/Build.cs:107)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:149)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:626)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:766)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:54)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

I’ve tried many things

  • Turned off Windows Defender
  • Used FileMode.CreateNew
  • Application.persistentDataPath
  • Made sure all maps weren’t Read Only
  • Changed path to root C:\
  • Ran Unity as admin

All to no avail, I’m with the hands in my hair here. Could someone help me out?

I’m sorry I think I was a bit too tired when I asked this question, now that I looked at the code a bit clearly It’s pretty obvious. At no point did I mention the actual name of the XML file I wanted to save.

boContainer.Save(Path.Combine(Application.persistentDataPath, "objects.xml"));

Is what solved my problem. Hope I can help future overworked programmers :slight_smile:

In order to correct the unauthorized user, I manually create a temp file and write to that. You dont have to disable any permissions that way.

Don’t make the path the drive.Mine path was D:/ and it occured problem.When I changed the root to D:/Programs my problem had been solved.

haha, awesome, just did the same and I wasn’t tired at all. So thanks for that… :wink:

In my case i was sloppy and put

Application.dataPath

instead of

Application.persistantDataPath 

in my code logic.

Go to the file explorer where the file is present and remove its read only checkmark in properties