locked
ms-appx: Create File or Change File RRS feed

  • Question

  • Hi

    I have problem Read xml file and Write Xmlf file 

    xml file write path Error Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

    Help me.

    Thanks 

        public async Task SaveAsync(clsSettings _settings)
            {
                //StorageFile file = await localFolder.GetFileAsync(fxmlFileName);
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Xml_File/Settings.xml"));
                try
                {
                    XmlDeSerializer.SerializeToStream(await file.OpenStreamForWriteAsync(), _settings);
                }
                catch
                { }
            }
    
            public async Task<clsSettings> LoadAsync()
            {
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Xml_File/Settings.xml"));
                //StorageFile file = await localFolder.GetFileAsync(fxmlFileName);
                clsSettings result = null;
                try
                {
                    Stream stream = await file.OpenStreamForReadAsync();
                    StreamReader sr = new StreamReader(stream);
                    result = XmlDeSerializer.DeserializeFromString(await sr.ReadToEndAsync(), typeof(clsSettings)) as clsSettings;
                }
                catch
                { }
                return result;
            }

    Wednesday, January 8, 2014 9:39 AM

Answers

  • This is expected. The app package installs into a read only directory. If you need to write to files copy them to your app data directory and use them from there.
    Wednesday, January 8, 2014 11:40 AM
    Moderator
  • It looks like the SerializeToStream method does not dispose the stream. The following method works fine for me:

    public async Task SaveAsync(clsSettings _settings)
    {
        StorageFile file = await localFolder.CreateFileAsync(fxmlFileName, CreationCollisionOption.ReplaceExisting);
        try
        {
            Stream stream = await file.OpenStreamForWriteAsync();
            XmlDeSerializer.SerializeToStream(stream, _settings);
            stream.Dispose();
        }
        catch
        { }
    }


    Wednesday, January 8, 2014 1:09 PM

All replies

  • This is expected. The app package installs into a read only directory. If you need to write to files copy them to your app data directory and use them from there.
    Wednesday, January 8, 2014 11:40 AM
    Moderator
  • ok Thanks

    My code change now read and write app data directory  but now again Error Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

    This Errors used code

    await file.OpenStreamForReadAsync() 
         public async Task SaveAsync(clsSettings _settings)
            {
               
    
                StorageFile file = await localFolder.CreateFileAsync(fxmlFileName, CreationCollisionOption.OpenIfExists);
                try
                {
                    XmlDeSerializer.SerializeToStream(await file.OpenStreamForWriteAsync(), _settings);
                }
                catch
                { }
            }
    
            public async Task<clsSettings> LoadAsync()
            {
                StorageFile file = await localFolder.CreateFileAsync(fxmlFileName, CreationCollisionOption.OpenIfExists);
                clsSettings result = null;
                try
                {
                    result = XmlDeSerializer.DeserializeFromStream(await file.OpenStreamForReadAsync(), typeof(clsSettings)) as clsSettings;
                }
                catch
                { }
                return result;
            }

    rror Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    Wednesday, January 8, 2014 12:11 PM
  • It looks like the SerializeToStream method does not dispose the stream. The following method works fine for me:

    public async Task SaveAsync(clsSettings _settings)
    {
        StorageFile file = await localFolder.CreateFileAsync(fxmlFileName, CreationCollisionOption.ReplaceExisting);
        try
        {
            Stream stream = await file.OpenStreamForWriteAsync();
            XmlDeSerializer.SerializeToStream(stream, _settings);
            stream.Dispose();
        }
        catch
        { }
    }


    Wednesday, January 8, 2014 1:09 PM