locked
System.UnauthorizedAccessException 0x80070005 in universal app when read file from Local folder

    Question

  • Hi, I am developing universal app. In my app some images are downloaded from internet and these are stored at LocalFolder.

    here is my code for download and store image.

     for (int j = 0; j < Theme.Count; j++)
                            {
                                JObject obja = (JObject)Theme[j];
                                string TargetName = (string)obja["TargetName"];
                                string TargetPath = (string)obja["TargetPath"];
                               
                                var uri = new Uri(TargetPath);
                                var downloader = new BackgroundDownloader();
                                StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(TargetName+".png",
                                    CreationCollisionOption.ReplaceExisting);
                                DownloadOperation download = downloader.CreateDownload(uri, file);
                                await download.StartAsync();
    							}

    Images are downloaded so this code is working and I can find these image at folder "C:\Users\admin\AppData\Local\Packages\Apppackagename\LocalState"

    From these images, One image is used at every pages and I have round about more than 5 pages - this image is used as background image of main grid of the page. So each five pages at load time I want to get this image from local folder and try to set as background of grid.

    Here is code for getting image.

     ImageBrush myBrush = new ImageBrush();
    							
    							string TargetName = "";
               
               
                    TargetName = "mainbg.png";
                    var imageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(TargetName);
                    var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
                    BitmapImage image = new BitmapImage();
                    image.SetSource(fs);
                    myBrush = new ImageBrush();
                    myBrush.ImageSource = image;
                    LayoutRoot.Background = myBrush;
                    myBrush = new ImageBrush();

    this code is written in every pages. When Load first page is its works fine. but when navigating second page from first one it gives me "A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll" 

    So how can I solved this error ?

    Thanks,


     


    shah

    Tuesday, January 27, 2015 7:04 AM

All replies

  • I recommend that you check the permissions on the images to see if you're missing something. You can try Process Monitor to see if any "Access Denied" shows up while running the app.

    Let us know how it goes.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, January 27, 2015 1:10 PM
    Moderator
  • Hi,

    Can you please give me details how to do / check " Process Monitor ", Sorry I don't know how to do this.

    I have check the folder where it is store , it has given full controls. And yes If i run app 10 times then errors just comes at 2 or 3 times.

    Thank you


    shah

    Wednesday, January 28, 2015 12:17 PM
  • Instructions on using Process Monitor:
    http://improve.dk/solving-access-denied-errors-using-process-monitor/


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, January 28, 2015 1:27 PM
    Moderator
  • Hi , 

    I have check as per you guide me I found error which is in below image


    shah

    Friday, January 30, 2015 4:21 AM
  • There's your problem.  You'll have to expand access to whatever user is accessing that location.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, January 30, 2015 2:08 PM
    Moderator