Share via


Getting System.UnauthorizedAccessException on file modification

Question

Wednesday, June 1, 2016 9:34 PM

Hello,

I'm using Windows 10 IoT version 10.014342.1000

I am trying to modify a file that is created through my app. However, when trying to modify the file, I receive an error. The odd thing is, my app created the file in the first place so I don't understand how it can create the file but then not be able to modify it. 

The code works perfectly on the regular Win 10 IoT build but not on the insider preview.. I'm unsure what could be causing it/

Any help is greatly appreciated. Error and my code are below:

An exception of type 'System.UnauthorizedAccessException' occurred in System.Private.CoreLib.dll but was not handled in user code

Additional information: Access is denied. (Excep_FromHResult 0x80070005)

If there is a handler for this exception, the program may be safely continued.

The code I am using is:

public async void modifyXml()
        {
           
            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(CONFIG_FILE);

            using (IRandomAccessStream writeStreams = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                System.IO.Stream s = writeStreams.AsStreamForRead();

                XElement mydata = XElement.Load(s);
               
                var tests = (from el in mydata.Descendants("tempsettings")
                             where el.Attribute("type").Value == "target-day"
                             select el).Single();

               
                String thevalue = tests.Value.ToString();
               
                tests.Value = "This is the new value";
               
                s.Position = 0;
                
                mydata.Save(s);
               
                s.Flush();

                
            }
        }

All replies (7)

Friday, June 3, 2016 11:17 AM âś…Answered

Hi Ric,

I actually managed to solve the issue not too long ago.

It turns out this insider build is a lot slower than the regular release. By adding a delay of a couple of seconds between when the XML file is created and when it read/modified it solves the issue. It's not an ideal situation and hopefully Microsoft will optimize this before they release it.

Many thanks for your efforts and assistance it was very much appreciated.

But for your reference, below is how the file is created:

      public async void createXmlStructure()
       {
            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(CONFIG_FILE, CreationCollisionOption.ReplaceExisting);
            using (IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                System.IO.Stream s = writeStream.AsStreamForWrite();
                System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
                settings.Async = true;
                settings.Indent = true;
                using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(s, settings))
                {
                    //write document root
                    writer.WriteStartElement("config");                    
                    
                    writer.WriteStartElement("Settings");

                    writer.WriteStartElement("tempsettings");
                    writer.WriteAttributeString("type", "target-day");
                    writer.WriteString("0.00");
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.Flush();

                    await writer.FlushAsync();
                }
                
            }
         }

Thursday, June 2, 2016 1:35 AM

Hi rvaspi, 

Maybe the exception is from trying to write something to a stream which is ReadOnly? 

In the code you posted, the stream you were trying to write is System.IO.Stream s = writeStreams.AsStreamForRead();

It is a stream for reading.

Regards,

Ric


Thursday, June 2, 2016 9:28 AM

Hi Ric,

Many thanks for your reply & suggestion.

I changed that part of the code to AsStreamForWrite but it still throws an exception.

It actually throws the exception before it reaches that part of the code.. It breaks at:

using (IRandomAccessStreamwriteStreams =await file.OpenAsync(FileAccessMode.ReadWrite))

Any further suggestions & help are welcome.


Thursday, June 2, 2016 10:02 AM

are you sure nothing in your complete app try to access the same file at the same time?


Thursday, June 2, 2016 11:01 AM

Hi cyberh0me,

Many thanks for your reply and assistance. I do not believe there is anything else in the app working on that file at the same time. I have method which runs prior that creates the XML file and straight in a similar manner and after that method I have another method which modifies it. Creation works fine but modification throws the exception.


Thursday, June 2, 2016 11:07 PM

Hi rvaspi, 

Could you please show us how you create the file and then modify it? Since most of the FileStorage APIs are async functions, it may throw some access denied exceptions if the file creation or file modification is not setup correctly. 

Thanks,

Ric


Monday, June 6, 2016 6:03 AM

Hi rvaspi, 

Great! Glad to know you fixed it:)

Maybe it would work better if you could implement a certain locking mechanic, so the app will only read or write to one file at a time. When handling file I/O, we should always implement some locking approaches, just in case if any reading/writing process takes more time than we expect.

Regards,

Ric