1
votes

How can I serialize objects in a UWP application? This is my code:

Car bmw = new Car() { Make = "BMW", Model = "X5" };
XmlSerializer serializer = new XmlSerializer(typeof(Car));
using (Stream stream = new FileStream(@"XMLTEST.txt", FileMode.Append, FileAccess.Write))
{
    serializer.Serialize(stream, bmw);
}  

It generates an exception:

System.UnauthorizedAccessException: 'Access to the path 'C:\Users\annou\onedrive\documents\visual studio 2017\Projects\Hamburger6\Hamburger6\bin\x86\Debug\AppX\XMLTEST.txt' is denied.'

I do the same in a Console app and it works. Why isn't it working in UWP the way it does in a Console app?

1

1 Answers

3
votes

Writing to the install directory is not allowed in UWP. This is to ensure the integrity of the installation folder, so the platform can ensure seamless updates and clean uninstalls.

You can write the file to AppData instead.