- Home /
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?
did you try to run unity as ad$$anonymous$$istrator?
it is the only thing missing from your list of stuff you tried
I have indeed tried to do so, forgot to put it in. But this also didn't help.
This is usually down to Antivirus software on your computer. Do you have any other AV software on your computer? $$anonymous$$cAfee, Norton, etc?
Answer by Leniaal · Jan 27, 2016 at 05:42 AM
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 :)
haha, happened to me to posted a question on stack overflow about somes trange bug in advanced reflection technique in the end, i just switched 2 paramaters by accident
Lol as silly as this sounds, my goodness your answer saved me. I was trying to a save a new file without even giving the file name, I just had the directory. I'm so glad it was so simple.. thank you.
Thank you, obvious but you also saved me, and I'm not the only one for sure. Thanks for posting the solution.
At no point did I mention the actual name of the X$$anonymous$$L file I wanted to save.
I did(n't do) something similar, and went to google straight away. Solved my problem. Thanks.
Answer by codeguyross1 · Apr 05, 2017 at 09:28 PM
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.
Answer by ahmetala · Jun 07, 2020 at 06:01 PM
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.
Answer by cwk17 · Jun 28, 2021 at 03:30 PM
haha, awesome, just did the same and I wasn't tired at all. So thanks for that... ;)
Your answer
Follow this Question
Related Questions
Missing additional configuration xml files after build 0 Answers
How to retrieve XML path in standalone version 1 Answer
Is there a way to encrypt/lock a File.CreateText file? (its my highscore) 1 Answer
Write to xml file in after build 0 Answers
How do I make it so players can't edit the file but the game can? File.CreateText 2 Answers