Reputation: 53
I have a problem with writing to xml file, placed in my application folder( windows 8, metro style). I’m getting Unauthorized AccessException when I’m trying to open file in read/write mode. I’ve done a lot of research, but still nothing. I tried this solution:
var sf = await Package.Current.InstalledLocation.GetFileAsync(@"data.xml");
XmlDocument xmlDoc;
using (var stream = await sf.OpenAsync(FileAccessMode.ReadWrite))
{
xmlDoc = await XmlDocument.LoadFromFileAsync(sf);
XmlElement root = xmlDoc.DocumentElement;
XmlElement xe = xmlDoc.CreateElement("debt");
XmlElement id = xmlDoc.CreateElement("Id");
id.InnerText = Guid.NewGuid().ToString();
XmlElement name = xmlDoc.CreateElement("Name");
name.InnerText = d.Name;
XmlElement surname = xmlDoc.CreateElement("Surname");
surname.InnerText = d.Surname;
xe.AppendChild(id);
xe.AppendChild(name);
xe.AppendChild(surname);
root.AppendChild(xe);
}
if (xmlDoc != null)
await xmlDoc.SaveToFileAsync(sf);
But again exception occur in line where I'm opening stream.
thx for your help
Upvotes: 0
Views: 3916
Reputation: 725
Package.Current.InstalledLocation.GetFileAsync represents where you application is installed which is an area you cannot directly write files to. Use the following
Windows.ApplicationModel.Package.Current.InstalledLocation
or
Windows.Storage.ApplicationData.Current.LocalFolder.Path
I am using the former and it works fine, see http://metrorssreader.codeplex.com/SourceControl/changeset/view/18082#263004
Upvotes: 2