Im getting System.UnauthorizedAccessException error when im not running it as administrator.
var appdatapath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var xmlFilePath = Path.Combine(appdatapath, "testApp\\xml\\test.xml");
xmlFilePathHistory = new Uri(xmlFilePath).LocalPath;
This code gives me right location of xml file which is 'C:\ProgramData\testApp\xml\test.xml'
public void InsertData(string text, string name)
{
try
{
XDocument doc = XDocument.Load(xmlFilePathHistory);
if (doc != null)
{
var newElement = new XElement("History",
new XAttribute("Text", text),
new XAttribute("Name", name));
doc.Element("Histories").AddFirst(newElement);
doc.Save(xmlFilePathHistory);
doc = null;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
This is the error message I am getting:
System.UnauthorizedAccessException: 'C:\ProgramData\testApp\xml\test.xml' access to the path is denied.
When i run as administrator it works fine but i cant always run as administrator. It needs to work for all users. I gave the full permission to the file, what am i missing.
Thanks for advence.