PickleManiac
PickleManiac

Reputation: 3

UWP C# System.UnauthorizedAccessException when saving XML file

I am trying to write some data to an already existing XML file as you can see below:

I already tried to give everyone full permission. This hasn't worked.

https://i.sstatic.net/RhG1R.jpg

Xml file I am trying to write to:

<?xml version="1.0" encoding="utf-8" ?>
<list>
<story>
<content>
Example story 12345678
</content>
<score>
10
</score>
</story>
</list>

My code for updating the XML file:

XmlDocument doc = new XmlDocument();

doc.Load(@"Scorelist.xml");
XmlNode mnodeMain = doc.CreateNode(XmlNodeType.Element, "Story", null);

XmlNode nodeStory = doc.CreateElement("Content");
nodeStory.InnerText = txt_verhaaldisplay.Text;

XmlNode nodeScore = doc.CreateElement("Score");
nodeScore.InnerText = txt_ratinginput.Text;

mnodeMain.AppendChild(nodeStory);
mnodeMain.AppendChild(nodeScore);

doc.DocumentElement.AppendChild(mnodeMain);

doc.Save(@"Scorelist.xml"); <--- This is where I am getting an error

This is the error message I am getting:

System.UnauthorizedAccessException: 'Access to the path'D:\Programs\Story Generator\bin\x86\Debug\AppX\Scorelist.xml' is denied.'

I am not sure what I am doing wrong here. I gave the full permission to the file, what could it be?

Thanks in advance!

Upvotes: 0

Views: 432

Answers (1)

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

In UWP you cannot write to the app's installation folder. This is to ensure the app folder's integrity and to allow incremental updates for the app.

To solve this, copy the file to your local appdata folder and then you can update it there.

https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions

Upvotes: 1

Related Questions