1

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.

Stefano Cavion
  • 641
  • 8
  • 16
  • Does this answer your question? [C# UnauthorizedAccessException to User Folder](https://stackoverflow.com/questions/45133064/c-sharp-unauthorizedaccessexception-to-user-folder) – Sinatr Aug 12 '21 at 06:55
  • Not exacly, no. As i said its working while runing as administrator. I should'n have to run as admin. I am using ProgramData folder and if i am not mistaken it should be accessible for all users for write/read. – Gökhan AKSOY Aug 12 '21 at 07:30
  • 2
    [Related](https://stackoverflow.com/q/22107812/1997232). – Sinatr Aug 12 '21 at 08:48

0 Answers0